-2
#include <stdio.h>
#define F(x) 32 + (x*9)/5

int main(void)
{ 
  int F,C;
  printf ("Enter temperature in celsius=");
  scanf ("%d",&C);
  F(C);

  printf (" %d fahrenheit = %d celsius\n", F, C);

  return 0;
 }

When I input 10 celsius, it comes out some as:

1798680630 fahrenheit = 10 celsius

Am I writing the formula wrongly? I can't seem to figure out the error. Just a beginner, going through my tutorials. Thank you!

qfwfq
  • 2,416
  • 1
  • 17
  • 30
belle
  • 23
  • 4
  • You forgot to add a `F=` in front of `F(C)`, because you want to assign the value to your variable `F`. http://ideone.com/Xc6c3x – mch Oct 04 '16 at 14:11
  • 1
    [Why confuse people?](https://blog.codinghorror.com/coding-for-violent-psychopaths/) – Sourav Ghosh Oct 04 '16 at 14:12
  • First of all, given that these are ints, you might lose too much information while dividing. Secondly, you shouldn't (can't) have both a variable F and a macro F, it's bound to have errors. See the answers below about why your code doesn't work – neoaggelos Oct 04 '16 at 14:13
  • @sourav hilarious! thanks :) – belle Oct 04 '16 at 15:10

3 Answers3

2

You are not storing the result of the macro expansion at all. So, F is uninitilized.

IMO, the macro isn't needed at all. Just use a variable:

#include <stdio.h>
int main(void)
{
  int C;
  float F;
  printf ("Enter temperature in celsius=");
  scanf ("%d",&C);
  F = 32 + (C*9)/5.0;
  printf (" %f fahrenheit = %d celsius\n", F, C);

  return 0;
 }

Notice that I used the literal 5.0 so that you don't perform just integer division.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • thank you! i changed it to macro as the simpler one didnt give me the right output. ill try again. – belle Oct 04 '16 at 15:07
0

You don't save output of F() to a variable. It should be:

  (...)
  F = F(C);

  printf (" %d fahrenheit = %d celsius\n", F, C);

Currently you are printing an uninitialized variable which is undefined behavior and is quite dangerous.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0

You are printing the address of the F. Also do not waste variables.

Remove

F(C);

Do your printing like :

printf (" %d fahrenheit = %d celsius\n", F(C), C);
cagdas
  • 1,634
  • 2
  • 14
  • 27