Effect of one control specification on another in printf statement.

radha gogia

radha gogia

@radha-BTDzli Oct 12, 2024
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 .

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Koushal Patel

    Koushal Patel

    @koushal-C6mEgp Jan 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