Day#25 'C' Coding Challenge

Day#25 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> int var1 = 5; void main() { printf("%f, %d , %o, %u", var1); }

----------------------------------------------------------

The above tests below scenarios:

1. Format specifier
2. What if there are more than one format specifiers in the printf and only one variable

------------------------------------------------------------

format specifier helps the compiler to understand the type of variable
In the code,%f, %o, %u are used
%f - float
%o - octal
%u - unsigned int
%d - signed integer

However, there is only 1 variable which is var1 and compiler treats it as %f and prints 5.00000
The others print an undefined variable as no variable is associated to them.

To get correct values printf statement must be
printf("%f, %d , %o, %u", var1, var1, var1, var1);

Post a Comment

0 Comments