2

This program:

#include <stdio.h> #include <conio.h> int main() { printf("%d %d %d",1) ; getch(); return 0; } 

gives me result 1 0 0 instead of warning or error or 1

Could you please tell me the logic behind it ?

I'm using Visual Studio 2010 to compile this code.

2
  • 2
    Both GCC and clang will tell you about mismatches like this if you turn on appropriate warnings (-Wall is sufficient, IIRC). If your compiler won't tell you about the problem, it may be time to get a better compiler. Commented Feb 1, 2016 at 4:47
  • @JonathanLeffler Thanks, I used Visual Studio 2010, in case GCC and Clang gives warning, how about the result and the reason ? Commented Feb 1, 2016 at 5:06

3 Answers 3

1

This is undefined behavior and not something you should rely on. While using printf () if sufficient and appropriately matching arguments are not provided, like in your case printf("%d %d %d",1), C does not define what should happen in that case and so the behavior is not standard or defined.

It is possible that this could cause your program to crash (if the next memory addresses from where printf () read values are not accessible or non-existant).

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

Comments

0

With printf(), if insufficient matching arguments are not provided, the result is undefined behavior.

C does not define what should happen in this case.

... If there are insufficient arguments for the format, the behavior is undefined. ... C11 §7.21.6.1 2

OP's code obviously printed 2 additional int with the value of 0. Why 0 today - look at the compiled assembly language. Might another compilation of the code has the same result - maybe - maybe not. It is not defined by the language.

Comments

0

printf doesn't care if there aren't enough arguments. "doesn't care" means that it doesn't actually check the number of items in the format list to make sure that a sufficient number of arguments have been specified.

if there are not enough arguments, printf will just access computer memory (the memory that would have been used, had sufficient arguments been specified) and use whatever data is there.

this is "undefined behavior" and the results will vary. it's possible that this could cause your program to crash (if the memory at the location is inaccessible or non-existent).

you'll find that this sort of thing is very common in languages like C, where you can do things should be considered "invalid" but that are simply accepted by the compiler.

5 Comments

What does "doesn't care" mean in computer language ?
Not only "it doesn't actually check" — there isn't any way for printf() to check — unless the implementation has some non-standard means of determining the number of arguments pushed. And beware that pushing a mixture of types can mean that a simple count isn't sufficient.
@JonathanLeffler: It depends entirely on the ABI -- for exampe in the x86_64 POSIX standard ABI, the number of words of arguments is passed in %al, so printf could in theory check the number of arguments. But I'm not aware of any implementation that actually does.
@ChrisDodd: Apologies — I should have said "there isn't any portable way in pure C code to do the check".
@ChrisDodd: The ABI doc I have says "When a function taking variable-arguments is called, %rax must be set to the total number of floating point parameters passed to the function in vector registers." That falls somewhat short of a count of the number of arguments, since it only counts floating point arguments and only the ones in registers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.