-1

I'm kind of new in C programming. To be honest this is my first program where I am using 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;
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can you update your answer with the actual details of the error message, line number or function that is not defined? – miltonb Jan 07 '17 at 08:11
  • 2
    You might want to get a [a good book on C](http://stackoverflow.com/a/562377/253056) before you write any more code - studying it will save you a lot of time and grief. – Paul R Jan 07 '17 at 08:31
  • Thanks for the suggestion. @ Paul R – Rubel Hosen Jan 07 '17 at 08:50
  • There is no error massage now in the code. It is just not printing the statements. @ miltonb – Rubel Hosen Jan 07 '17 at 08:52

2 Answers2

2

The problem is, you are trying to define functions inside the main() function. This is not allowed in pure C. Some compiler extensions allows "nested functions", but that's not part of the standard.

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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks. The problem is solved. Though it's not printing what i expected. It seems still i have some errors in the code. – Rubel Hosen Jan 07 '17 at 08:17
  • @RubelHosen that is because, you're performing integer divisions. see [this answer](http://stackoverflow.com/a/39818803/2173917). – Sourav Ghosh Jan 07 '17 at 08:24
0

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--

#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;
}
Mohammad Sadiqur Rahman
  • 5,379
  • 7
  • 31
  • 45