Output of C Program | Set 29
Question 1
How many times main() will get called?
C
main() { printf ( "\n main Called Again" ); main(); } |
Answer : Infinite loop
Description : There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times.
Question 2
Guess the output of the following program :
C
#include<stdio.h> int main() { int x = 10; float y = 10.0; if (x == y) printf ( "x and y are equal" ); else printf ( "x and y are not equal" ); } |
Answer : x and y are equal
Description : if (x == y) here we are comparing if (10 == 10.0) hence this condition is satisfied. Because we cannot compare int
and float so the int is converted to float and then compared. Hence it prints “x and y are equal”.
Question 3
what will be the output of the following code?
C
#include<string.h> main() { char *str1 = "abcd" ; char str2[] = "abcd" ; printf ( "%d %d %d" , sizeof (str1), sizeof (str2), sizeof ( "abcd" )); } |
Answer : 8 5 5
Description :
First size of character pointer is printed which is 8 on the machine this program is run. Then the size of string str2 whose size is 5 (including the ‘/0’ termination character) is printed. The third is similar to second one.
Question 4
What will be output of the following c program?
C
#include "stdio.h" int main() { int _ = 18; int __ = 38; int ___; ___ = _ + __; printf ( "%i" , ___); return 0; } |
Answer : 56
Description :Variable name can have only underscore.
Question 5
What will be printed as the result of the operation below:
C
main() { int x = 41, y = 43; x = y++ + x++; y = ++y + ++x; printf ( "%d %d" , x , y); } |
Answer : 86 130
Description : Its actually compiler dependent. After x = y++ + x++, the value of x becomes 85 and y becomes 44, And y = ++y + ++x will be computed as y = (44) + (86). After computation y becomes 130.
Question 6
What will be printed as the result of the operation below:
C
main() { int x = 7; printf ( "%d, %d, %d" , x, x<<5, x>>5); } |
Answer : 7, 224, 0
Description : As x = 7 so first %d gives 7, second %d will take value of x after left shifting it five times, and shifting is done after converting the values to binary, binary value of 7 (000111) will be left shifted twice to make it binary 224(11100000), so x<<5 is 224 and as left shifting does not effect the original value of x its still 5 so third %d will also show 0.
Question 7
What will be the output?
C
main() { if (1, 0) printf ( "True" ); else printf ( "False" ); } |
Answer : False
Description :comma(,) operator returns the value which at the right hand side of , and thus if statement become if(0).
Question 8
What is the output?
C
#include<stdio.h> int main() { printf ( "%d" , printf ( "%d" , 12345678)); return 0; } |
Answer : 123456788
Description : Notice the extra 8 at the end. printf() returns the number of characters successfully printed on the screen.
Question 9
What is the output?
C
void main() { printf ( "2 + 3 = %d" , 2+3); } |
Answer : 2 + 3 = 5
Description : Since the format specifier is %d and both are integers (2, 3) it will add and print integer value.
Question 10
What is the output?
C
#include<stdio.h> main() { int i = 0; if (i = 55, 0, 10, 0) printf ( "Test Skills %d" , i); else printf ( "C Programming %d" , i); } |
Answer : C Programming 55
Description : i=55 is evaluated first and its value discarded. 0 evaluated then and discarded. 10 evaluated then and discarded. After then rightmost 0 is evaluated and it will be the value of entire expression (but not the value of sub-expressions) in if condition and make the condition false.
Question 11
what is the output ?
C
#include<stdio.h> int main() { char arr[5] = "World is beautiful" ; printf ( "%s" , arr); return 0; } |
Answer : World
A warning is also printed “4:19: warning: initializer-string for array of chars is too long [enabled by default]”
Description : Size of any character array cannot be less than the number of characters in any string which it has assigned. Size of an array can be equal (excluding null character) or greater than but never less than.
Question 12
What is the output of following program?
C
#include<stdio.h> void main() { int a = 2; switch (a) { case 4: printf ( "A" ); break ; case 3: printf ( "B" ); default : printf ( "C" ); case 1 : printf ( "D" ); break ; case 5 : printf ( "E" ); } } |
Answer : CD
Description : In switch statement default should be at mentioned after all the switch cases. In this case, it gets executed in between and all cases after default are executed before a break statement.
Question 13
What is the output?
C
#include<stdio.h> int main() { int x = 100, y = 200; if ( ++x || ++y) printf ( "x = %d, y = %d" , x, y); else printf ( "condition failed" ); return 0; } |
Answer : x = 101, y = 200
Description : In the if condition first part (ie., ++x ) is executed first, here x value becomes 101.
Question 14
Consider the following program:
C
#include<stdio.h> main() { int i, j; for ( i = 0, j = 5; j>0, i<10 ; i++, j--) printf ( "\GeeksforGeeks.org" ); } |
How many times “GeeksforGeeks.org” will get printed?
Answer : 10
Description : Condition part of for loop ( j>0, i<10 ) is separated by commas which means that compiler will use the value which is at right hand side of comma i.e of i<10 so the loop will execute
till the value of i is less than 10.
Question 15
What is the output?
C
#include<stdio.h> #define SQR( x ) ( x * x ) int main() { int b = 5; int a = SQR(b+2); printf ( "%d\n" , a); return 0; } |
Answer : 17
Description : As we know Macros will blindly substitute, doesn’t calculate. It will first substitute the value then calculate later. So, after substitution the Macro will become SQR( x + 2) = ( x + 2 * x + 2 ) in this case x value is 5. So,the final substitution will be SQR(5 + 2 ) = (5 + 2 * 5 + 2). since ‘*’ is having more priority than ‘+’ it will become (5 + 10 + 2). The value of a will be 17 in this case
This article is contributed by Harish Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...