C程序中未定义的函数引用

时间:2017-01-07 08:06:26

标签: c nested-function

我是C编程的新手。说实话,这是我使用Function的第一个程序。但它不起作用。谁能告诉我这应该是什么问题?

// Convert freezing and boiling point of water into Fahrenheit and Kelvin

#include<stdio.h>

void calculation(int);
void freezingCalculation(void);
void boilingCalculation(void);

int main (){

int temperature, fahrenheit, kelvin;


void freezingCalculation(void){
   int temperature = 0;
    calculation(temperature);
 printf("Freezing point in Fahrenheit is %d. \n", fahrenheit);
   printf("Freezing point in Kelvin is %d. \n", kelvin);
}

void boilingCalculation(void){
    int temperature = 100;
    calculation(temperature);
    printf("Boiling point in Fahrenheit is %d. \n", fahrenheit);
    printf("Boiling point in Kelvin is %d. \n", kelvin);
}

void calculation(int temperature){
    //Temperature in fahrenheit
    fahrenheit = ((temperature * 9) / 5) + 32;

    //Temperature in Kelvin
    kelvin = temperature + 273;
}

}

2 个答案:

答案 0 :(得分:2)

问题是,您正在尝试在<{em>} 函数中定义函数。纯C. Some compiler extensions allows "nested functions", but that's not part of the standard中不允许这样做。

这里发生的是,编译器看到函数声明但找不到函数的任何定义,因为它们不在文件范围中。

您需要将功能定义移出main()并根据要求从main()调用函数。

答案 1 :(得分:0)

  

你没有给你的功能打电话。

在main函数之外编写你的方法(freezingCalculation等),不要忘记返回,因为你的main函数返回类型是integer.Like -

#include <stdio.h>
int temperature, fahrenheit, kelvin;
void calculation(int);
void freezingCalculation(void);
void boilingCalculation(void);

int main (){



    freezingCalculation();
    boilingCalculation();

    return 0;
}
void freezingCalculation(void){
   int temperature = 0;
    calculation(temperature);
 printf("Freezing point in Fahrenheit is %d. \n", fahrenheit);
   printf("Freezing point in Kelvin is %d. \n", kelvin);
}

void boilingCalculation(void){
    int temperature = 100;
    calculation(temperature);
    printf("Boiling point in Fahrenheit is %d. \n", fahrenheit);
    printf("Boiling point in Kelvin is %d. \n", kelvin);
}

void calculation(int temperature){
    //Temperature in fahrenheit
    fahrenheit = ((temperature * 9) / 5) + 32;

    //Temperature in Kelvin
    kelvin = temperature + 273;
}