Effect of one control specification on another in printf statement.

CASE A :
#include
#include
int main()
{ int y = (8,9);
    printf("%d ,%f , %lf", 8.76, 8.987654);
    getch();
    return 0;
}
CASE B:
#include
#include
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 .

Replies

  • Koushal Patel
    Koushal Patel
    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

You are reading an archived discussion.

Related Posts

Dear my friends, followers, motivators and mentors of Crazyengineers. I am proud to share with you that I have finally managed to win my first International Contest title, the IoT...
I had been searching and learning about clocks and specifically the escapements. Quite frankly I didn't know one escapement from the other. But YouTube tracked me and when I logged...
If you have been worrying about all those international calls you have to make to your clients and relatives staying abroad every month, here's an app that ought to really...
In a bid to gain foothold on mobile platform, Twitter has announced its first worldwide tour, Flock and a global contest titled Hatch for mobile developers who are willing to...
1. What types of documents would you need for QA, QC, and Software Testing? 2. What did you include in a test plan? 3. Describe any bug you remember ?...