CrazyEngineers
  • Effect of one control specification on another in printf statement.

    radha gogia

    radha gogia

    @radha-BTDzli
    Updated: Oct 12, 2024
    Views: 1.1K
    CASE A :
    #include<stdio.h>
    #include<conio.h>
    int main()
    { int y = (8,9);
        printf("%d ,%f , %lf", 8.76, 8.987654);
        getch();
        return 0;
    }
    CASE B:
    #include<stdio.h>
    #include<conio.h>
    int main()
    { int y = (8,9);
        printf("%d, %f, %lf",8 , 8.987654);
        getch();
        return 0;
    }
    Output in A:
    -1202590843,-38367267551084304000000000000000000000000000.000000,0.000000

    Output in B:
    8,8.987654,0.000000
    how is the value to be printed differing in both cases that is when i give wrng value fr %d conversion specification in CASE A ,then it also prints wrong value of its immediate format string ,so here %f tries to print some wiered value each time while the other % lf coreesponds to print the value 0.000000 so hw is it that this %d format string affecting the values to be printed corresponding to other format strings ,becoz when in CASE B ,on giving correct value for %d remove,i m getting crect answers .
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Koushal Patel

    MemberJan 28, 2015

    Statement is correct, but to make this conversion you must cast variable you wish to convert, not just assign it to another int variable.

    This causes problems also in the printf statement, since you are printing a float variable with int specifier %d without a cast.

    Fix you're code like this:

    printf("%d\n", (int) x);
    ^
    | this is the cast operation
    and it will correctly print integer value for 8.76
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register