1
int main(){ // Comment the next line, Garbage value is printed. However, this shows an integer value. int x=32; printf("%d"); return 0; } 

Any reason for this behaviour, or is it random??

0

2 Answers 2

3

You told printf() how to print something (the format specifier %d), but you did not tell printf() what to print.

To elaborate, you forgot to supply the required argument for the supplied format specifier %d.

C standard says, if there are insufficient argument for supplied format specifier, the behaviour is undefined.

FWIW, just specifying the format specifier won't magically consider the argument for it. You need to write something like

 printf("%d", x); 

to print the value of x.

Sign up to request clarification or add additional context in comments.

Comments

3

The printf() has the below prototype

int printf(const char *,...); 

What you pass is %d to the printf() and since this is a format specifier to print out int printf() looks for the parameter which needs to be printed out since you don't pass any this is undefined behavior

1 Comment

Thanks for the clarification. Unfortunately, stackoverflow doesn't allows me to accept 2 answers !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.