简体   繁体   English

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

[英]Undefined reference to function in C program

I'm kind of new in C programming. 我是C编程的新手。 To be honest this is my first program where I am using Function. 老实说,这是我使用Function的第一个程序。 But it's not working. 但这不起作用。 Can anyone tell me what is supposed to be the problem? 谁能告诉我这是什么问题?

// 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;
}

}

The problem is, you are trying to define functions inside the main() function. 问题是,您正在尝试在main()函数定义函数。 This is not allowed in pure C. Some compiler extensions allows "nested functions", but that's not part of the standard . 在纯C语言中不允许这样做。 某些编译器扩展允许使用“嵌套函数”,但这不是标准的一部分

What happens here is, the compiler sees the function declarations but cannot find any definition to the functions as they are not in file scope . 这里发生的是,编译器看到了函数声明,但是找不到函数的任何定义 ,因为它们不在文件范围内

You need to move the function definitions out of main() and call the functions from main() as per the requirement. 您需要根据需要将函数定义移出main()并从main()调用函数。

you did not call your function. 您没有调用函数。

Write your method (freezingCalculation etc) outside of main function and do not forget to return as your main function return type is integer.Like-- 在主函数之外编写方法(freezingCalculation等),不要忘记返回,因为主函数的返回类型是整数。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM