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
  • 34,448
  • 50
  • 182
  • 322
elcuco
  • 8,948
  • 9
  • 47
  • 69
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
  • 91,295
  • 49
  • 239
  • 345
0

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

Akash KC
  • 16,057
  • 6
  • 39
  • 59
0

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

Prasad G
  • 6,702
  • 7
  • 42
  • 65