#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f);
}
I expect the error in output, but the actual output is 0 0 0.000000 (null)
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f);
}
I expect the error in output, but the actual output is 0 0 0.000000 (null)
Your format string expects 4 arguments but you only pass 3. Doing so invokes undefined behavior, meaning you can't predict the behavior of the program.
In this case the string "(null)" is printed, but your code could print some random sequence of characters, no extra characters, or it could crash. There's no guarantee.
What probably happened is that the %s
format specifier attempted to read the next 8 bytes on the stack to get a pointer, and those 8 bytes happened to all be 0, so the string "(null)" is printed because it read a NULL pointer. But again, there's no guarantee of that.
Also see, What is the behavior of printing NULL with printf's %s specifier?.