Why C program gives different result?

c, compiler-construction

Solution

This is an undefined behavior, so absolutely anything can happen.

The local variables might be actually printed, because on some systems `Xprintf` functions family might pop their arguments off the stack (stdarg.h facilities could be used for implementation of such functions). Your variables `a,b,c` might happen to be at the memory location from which `va_arg` macro 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 `fprintf` function:

7.19.6.1 The fprintf function

2/ The fprintf function writes output to the stream pointed to by stream, under control of the string pointed to by format that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. The fprintf function returns when the end of the format string is encountered.

Problem

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?

Original source