#include<stdio.h>
int main()
{
    int i=10,j=30;

   printf("%d %d",i,j);          // this line printing value of i and j...works fine
   printf("%d %d");             // this line again printing value of i and j 
  printf("%d");                     // this line printing value of i

 return 0;
}

On gcc compiler, 2nd and third printf() call taking value of i and j automatically..
but if in first printf() call ,i don't give i and j as argument then that prints garbage value..
can nyone explain it plz..

one more thing...
if i use scanf("%2d %4f",&g,&f); then what will happen...i normally don't use numeric number with %d..
so plz explain both questions.

If you specify %d in the format string of the printf() function, you also have to specify a corresponding integer value to print.

General rule: For each format specifier you include in the format string, you also have to supply a corresponding value (of the same type as the format specifier dictates).

And if I'm not wrong, if you use the printf-function like this: [B]printf("%d");[/B] , then this will lead to undefined behavior.

Comments
Yes, very undefined behaviour!

On gcc compiler, 2nd and third printf() call taking value of i and j automatically..
but if in first printf() call ,i don't give i and j as argument then that prints garbage value..
can nyone explain it plz..

It happens to work out that way with how printf and varargs work on gcc. varargs probably look down the call stack unconditionally, and the call stack probably doesn't physically change until values are overwritten. The second call does not overwrite the two values and they stay the same for the sequential calls.

It's really pointless to explain how undefined behavior works because it could be different the next time you run the program, or in the next revision of the compiler, and certainly on a different compiler. Just don't do it. ;)

Like tux4life said, you need at least as many values as there are format specifiers in the format string. If there are fewer values than format specifiers, it's undefined behavior. There can be more values, and the extra values will be ignored without it being UB, but that will confuse people reading your code.

Comments
You clarified my doubt :)
This article has been dead for over six months. Start a new discussion instead.