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?
There are many things within C that are implementation-defined. This means that the people who create the compilers can choose how they want to handle those situations. In general, for portability it is best in most cases to not rely on undefined behavior, even when most or all compilers handle it the same way.
C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation. C implements algorithms and data structures swiftly, facilitating faster computations in programs.
C is completely flexible when it comes to reading and writing arbitrary memory locations. This flexibility comes at a great cost, and has been the cause of many bugs across the software spectrum. Most notable and affecting the entire internet include such bugs in web servers, mail servers and ftp servers.
C/C++ is relatively fast as compared to Python because when you run the Python script, its interpreter will interpret the script line by line and generate output but in C, the compiler will first compile it and generate an output which is optimized with respect to the hardware.
Mismatch of arguments in printf
and giving no arguments (as in this question )is
undefined behaviour
It may get garbage
or may get those values a,b,c
.
It's not defined by language standard.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With