There was a question in an exam I took. The question was:
What will be the output of the following code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5, b=6, c=7;
printf("%d%d%d");
}
My answer : It will give a warning as printf has not been provided with the required arguments.
And if you will run it, you will get garbage values.
The teacher gave me zero. According to him, the answer is that the values will be printed in reverse order i.e the output will be “765”.
The catch is he is using the Turbo C++ compiler and I generally use GCC. Can I have some comments and explanation to get my marks back? Or is my answer really wrong?
This is an undefined behavior, so absolutely anything can happen.
The local variables might be actually printed, because on some systems
Xprintffunctions family might pop their arguments off the stack (stdarg.h facilities could be used for implementation of such functions). Your variablesa,b,cmight happen to be at the memory location from whichva_argmacro will take extract the arguments. These are the details of library implementation / execution environment and are not part of the standard, so they can vary across different target platforms.Here is a quote from c99 standard describing
fprintffunction: