0

Possible Duplicate:
Behaviour of printf when printing a %d without supplying variable name

What happens if I use, for example, printf("%d %d"); ? Will it just pop the last eight bytes from the stack and print them out?

Community
  • 1
  • 1
Zakum
  • 491
  • 1
  • 5
  • 6

4 Answers4

1

In GCC - you get a warning (this is done using __attribute__ ((__warn_unused_result__))).

On x86 you don't get a stack error, as the caller will push the data to the stack, and also pop after the function returned. This is called the C calling convention, unlike pascal - in which the function will also pop the data from the stack (using ret 10 in ASM for example).

The values of the data you required will be random.

BenMorel
  • 31,815
  • 47
  • 169
  • 296
elcuco
  • 8,684
  • 9
  • 45
  • 67
0

Technically its undefined behaviour if the number of format specifiers in printf() is greater than the number of arguments.

However the following is fine

printf("%d",x,y); // y is evaluated but not printed.

Prasoon Saurav
  • 88,492
  • 46
  • 234
  • 343
0

In that case, you'll get garbage data which relies upon the compiler and its compiling option....

Akash KC
  • 15,511
  • 5
  • 36
  • 58
0

It will print garbage values but it has 'More % conversions than data arguments' warning.

Prasad G
  • 6,600
  • 7
  • 41
  • 65