Like Tree1Likes
  • 1 Post By grumpy

expression

This is a discussion on expression within the C Programming forums, part of the General Programming Boards category; hi all, I have a code Code: #include<stdio.h> main(){ int i = 0; float f; printf("%d %d %d\n",i>0,i<0,i==0); printf("%d %d ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Question expression

    hi all,

    I have a code
    Code:
    #include<stdio.h>
    main(){
    int i = 0;
    float f;
    printf("%d %d %d\n",i>0,i<0,i==0);
    printf("%d %d %f\n",i>0,i<0,i==0);
    f = (i==0);
    printf("f=%f\n",f);
    printf("%d %d %f\n",i>0,i<0,i==0);
    }
    the output is

    0 0 1
    0 0 0.000000
    f=1.000000
    0 0 1.000000

    Why are results diff 2nd and 4th line in above ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    That's an interesting result to remind you that the behaviour is undefined if an argument's type does not match the corresponding conversion specifier. That is, %f expects a double argument, so you should have written (in both cases):
    Code:
    printf("%d %d %f\n", i > 0, i < 0, (double)(i == 0));
    By the way, you should explicitly specify the return type of main as int, and explicitly return 0 if you are compiling with respect to pre-C99.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    I tried that, I get a compile error

    error: expected expression before ‘double’

    I cant understand why the results are different in the two similar printf statements.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Quote Originally Posted by Amit Choubey
    I tried that, I get a compile error

    error: expected expression before ‘double’
    Probably due to a typographical error on your part. This compiles perfectly fine for me:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 0;
        float f;
        printf("%d %d %d\n", i > 0, i < 0, i == 0);
        printf("%d %d %f\n", i > 0, i < 0, (double)(i == 0));
        f = (i == 0);
        printf("f=%f\n", (double)f);
        printf("%d %d %f\n", i > 0, i < 0, (double)(i == 0));
        return 0;
    }
    Quote Originally Posted by Amit Choubey
    I cant understand why the results are different in the two similar printf statements.
    Because you did not pass arguments of the expected type to printf, there is undefined behaviour. The results could have been the same, or the compiler could have refused to compile the program, or the compiler could have inserted malicious code to destroy your computer, etc. Realistically, what happened is that some kind of pre-condition assumption made in the implementation of printf no longer held true, and this in turn affected the output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Quote Originally Posted by Amit Choubey View Post
    I tried that, I get a compile error

    error: expected expression before ‘double’
    Then you left something out of what laserlight specified. Such as brackets you deemed unnecessary, but were actually necessary.

    Quote Originally Posted by Amit Choubey View Post
    I cant understand why the results are different in the two similar printf statements.
    Because %f format specifier directs printf() to assume the corresponding argument is of type double. You supplied an int, which means anything is allowed to happen, whether it makes sense to you or not.

    In practice, a double consumes more memory than an int. To illustrate what is probably happening, let's assume an int is represented using 4 bytes and a double using 8 bytes (the actual sizes are compiler-dependent). So, by using the %f format, you have directed printf() to assume the third argument consists of 8 bytes, but only supplied 4. printf() will simply interpret 8 bytes (the 4 in the int, and 4 bytes immediately after) as if they represent a floating point value. If those 4 additional bytes happen to be part of the variable f, then that would explain the effect you are seeing.

    Of course, since the behaviour is undefined, the code could have reformatted your hard drive. That is the nature of undefined behaviour - any result is permitted.

    Either way, you should NEVER use the %f format to print an int.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the value of this expression?
    By theju112 in forum C Programming
    Replies: 16
    Last Post: 08-12-2011, 12:29 PM
  2. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 04:19 PM
  3. Replies: 2
    Last Post: 11-25-2009, 06:38 AM
  4. problem with this expression: (n%k)/k
    By smoking81 in forum C Programming
    Replies: 13
    Last Post: 01-30-2008, 11:51 AM
  5. S-Expression
    By stimpyzu in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2004, 07:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21