NeoR NeoR - 1 month ago 13
C Question

printf() and format specifier linking

i understand quite well as to how printf() and format specifier works

NOTE:- I am using Turbo c/c++ compiler and ye si know gcc,g++,etc are way better but the problem remains same

code:-

void main()
{
clrscr();
printf("%d %d %d");
getch();
}


output:- 0 344 0

now,

code:-

void main()
{
clrscr();
printf("%f %f %f");
getch();
}


output:- printf : floating point formats not linked
Abnormal program termination.


now , also note that i have tried this on around 200 systems.

Question:-I believe that those format specifier numbers that are displayed on the output are not garbage as for every system same result is obtained and their is some link between printf() function and format specifier through file handling or pointer. So can anyone now finally tell what it might be?

note:-result may vary from IDE to IDE but is same for a compiler on every system as i have tried this on codeblocks IDE also.

alk alk
Answer

So can anyone now finally tell what it might be?

From the C11 Standard (draft) for the library function fprintf():

7.21.6.1/2

[...]

If there are insufficient arguments for the format, the behavior is undefined.

After code has invoked undefined behaviour anything could happen.