Output of C programs | Set 42
QUE.1 What is the output of following program?
C
#include <stdio.h> int main() { int x = 10, *y, **z; y = &x; z = &y; printf ( "%d %d %d" , *y, **z, *(*z)); return 0; } |
a. 10 10 10
b. 100xaa54f10
c. Run time error
d. No Output
Answer : a
Explanation: Because y contains the address of x so *y print 10 then **z contains address of y of x so print the value of x 10 and 3rd *(*z) contains address of y of x that’s why print 10. So, final output is 10 10 10.
QUE.2 What is the output of following program?
C
#include <stdio.h> int main() { // initialize the val=1 int val = 1; do { val++; ++val; } while (val++ > 25); printf ( "%d\n" , val); return 0; } |
a) 25
b) 50
c) 12
d) 4
Answer : d
Explanation: Here, do while loop executes once and then it will check condition while will be false meanwhile value will be increased 3 times (two times in do while body and once while checking the condition);
Hence value will be 4.
QUE.3 What is the output of following program in the text file?
C
#include <stdio.h> int main() { int a = 1, b = 2, c = 3; char d = 0; if (a, b, c, d) { printf ( "enter in the if\n" ); } printf ( "not entered\n" ); return 0; } |
a) enter in the if
b) not entered
c) run time error
d) segmentation fault
Answer : b
Explanation: In this program, We are check the if condition all (a, b, c)>0 but d = 0 so condition is false didn’t enter in the if so print not entered.
QUE.4 What is the output of following program?
C
#include <stdio.h> int main() { char str[10] = "Hello" ; printf ( "%d, %d\n" , strlen (str), sizeof (str)); return 0; } |
a) 5, 10
b) 10, 5
c) 10, 20
d) None of the mentioned
Answer: a
Explanation: strlen gives length of the string that is 5; sizeof gives total number of occupied memory for a variable that is 8; since str is a pointer so sizeof(str) may be 2, 4 or 8.
It depends on the computer architecture.
QUE.5 What is the output of following program?
C
#include <stdio.h> int main() { if (0) ; printf ( "Hello" ); printf ( "Hi" ); return 0; } |
OPTION
a) Hi
b) HelloHi
c) run time error
d) None of the mentioned
Answer : b
Explanation: There is a semicolon after the if statement, so this statement will be considered as a separate statement; and here printf(“Hello”); will not be associated with the if statement. Both printf statements will be executed.
This article is contributed by Ajay Puri(ajay0007). 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...