1

I wrote some lines of following code.

#include "stdio.h"

int main(){
    float t,res;
    char c;
    scanf("%f",&t); 
    getchar();
    scanf("%s",&c);
    if (c=='R') res = 4/5 * t;
    else if (c=='F') res = (9/5 * t) + 32;
    else if (c=='K') res = t + 273;
    printf("%.2f",&res);
    return 0;
}

I have no idea why the output shown 0.00 when I give t = 25 and c = 'R'. The console looks like this.

25
R
0.00

Would any body give me an explanation?

2501
  • 25,460
  • 4
  • 47
  • 87
  • 4
    `printf()` expects `res`, not `&res`. – Jonathan Leffler Aug 25 '16 at 06:40
  • 2
    `scanf("%s",&c);` --> `scanf(" %c",&c);` – LPs Aug 25 '16 at 06:40
  • 2
    "when I give t = 25 and c = 'R'" How? If you enter `R` from the standard input, not your debugger, you will invoke *undefined behavior* by accessing out-of-range. `%c` should be used instead of `%s` to read one character. – MikeCAT Aug 25 '16 at 06:41
  • Perhaps `c = getchar();` needed instead of `scanf("%s",&c);` – VolAnd Aug 25 '16 at 06:42
  • I've just tried your advise, but sorry it still doesn't work – Paskahlis Anjas Aug 25 '16 at 06:42
  • 3
    Integer division: `res = 4/5 * t;` should be `res = 4.0/5.0 * t;` or something similar to force floating point division. Using floating point constants in what you want to be floating point arithmetic — using integers invites problems. Also, it's a good idea to end print operations with a newline. – Jonathan Leffler Aug 25 '16 at 06:47
  • Do not edit questions using suggestions and fixes posted in the answers, as this makes the entire post incomprehensible for future readers. – 2501 Aug 25 '16 at 08:47
  • when compiling, always enable all the warnings, then fix those warnings. Then the incorrect syntax for the `printf()` parameters would have been caught (and fixed by you) – user3629249 Aug 26 '16 at 23:29

4 Answers4

5
  • scanf("%s",&c); has a big chance to cause out-of-range access, which invokes undefined behavior. Use scanf("%c",&c); (%c instead of %s) to read only one character.
  • printf("%.2f",&res); will invoke undefined behavior by passing data having wrong type to printf(). Use printf("%.2f",res); (without &) to print data having type double (or float, which will automatically converted to double in variable-length arguments)
  • The expressions res = 4/5 * t and res = (9/5 * t) + 32 may not do what you want because 4/5 and 9/5 are integer divisions, in which remainders are truncated. Try using res = 4.f/5 * t and res = (9.f/5 * t) + 32
  • You should check whether scanf()s are successful and the data read is valid in order not to invoke undefined behavior by using values of uninitialized variables having automatic-storage duration, which are indeterminate.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2
  • First of all, you use printf in the following way :

    printf("%.2f",&res);
    

    but according to what you want to print, you should change it to :

    printf("%.2f", res);
    
  • Secondly, as you are reading a simple character, modify this line :

    scanf("%s",&c);
    

    to :

    scanf("%c", &c);
    
  • Finally and most importantly, the statement :

    res = 4/5 * t;
    

    will compute the integer quotient (which will give 0 in case of 4/5), but you want to do the float division. So you should change it to :

    res = (float)4/5 * t;
    

    or :

    res = 4/(float)5 * t;
    

    as one of the two members of the division needs to be float to get the result you want.

    Of course instead of casting you could also do :

    res = 4.0/5 * t;
    

    or :

    res = 4/5.0 * t;
    

    The same should also be applied to 9/5*t, which should change to 9.0/5*t or 9/5.0*t.

Marievi
  • 4,951
  • 1
  • 16
  • 33
2

Of course,

printf("%.2f",res);  // without &

and

4.0/5   9.0/5  // .0 makes literal of double type

or better

4.0F/5   9.0F/5  // .0F makes literal of float type

in the expressions.

But my second advise is for the following code

scanf("%f",&t); 
getchar(); // cleaning the input stream from one `'\n'`
scanf("%s",&c);

It is better to write this part as

scanf("%f",&t);
while (getchar() != '\n'); // cleaning the input stream from everithing
c = getchar(); // reading one character

this approach allows to read correct character after input something like

1.34abdfsk

R

In that case abdfsk as well as \n will be skipped by loop while (getchar() != '\n');

Community
  • 1
  • 1
VolAnd
  • 6,367
  • 3
  • 25
  • 43
1

Change

scanf("%s",&c);

to

scanf("%c",&c); // %c format specifier for character

Change

printf("%.2f",&res);

to

printf("%.2f",res); // No address of operator required

In

res = 4/5 * t;

the / & * have the same precedence, hence the expression is evaluated left to right. So operator occurrence matters.

Since both 4 & 5 are evaluated as integers the division yields zero and that is the reason you've got zero as the result. To force compiler to treat 4/5 as a float, you could explicitly mention at least one value as float:

res = 4/5.0F * t;
sjsam
  • 21,411
  • 5
  • 55
  • 102