Predict the output of the following C programs.
1. What will be the output of following program?
Explanation: Scanf function returns integer value as its return type is integer. In the above question, if you will enter two integer values, then it will return 2. It means “if condition” becomes true, otherwise “if condition” becomes false.
2. What will be the output of following program?:
Explanation: Execution from left to right
In the first step A = (0) + (a++) - (a++) + (++a) In the second step A = (0) + (1) - (a++) + (++a) In the third step A = (0) + (1)-(2) + (++a) In the fourth step A = (0) + (1)-(2) + (4) Therefore final value of A is 3.
3. What will be the output of following program?
18, 23, 45, 56, 4, 6, 45,
Explanation: Sizeof and equal operator associativity is from right to left. Sizeof operator either returns the byte size of single element or container element sizes. Therefore, sizeof(ar) will return 4 byte as we pass single element of integer type in sizeof operator as integer take 4 byte and sizeof(ar) will return total size of array container i.e. 4*7=28.
4. What will be the output of following program?
Value of count is 1 Value of count is 1
Explanation: As in this question, count is an global variable in the program. We assign 1 to count variable in main function which will further passes to display function. “Extern int count” is a way to define global variable in any function.
5. What will be the output of following program?
Value of flag is 1
Value of flag is 0
Explanation: Scanf function will return integer value. In the above question, if it get executed successfully it will return 1 otherwise it will return 0. Therefore flag=scanf(“%d”, &n) will return 1 because it scan an integer value and flag=scanf(“%d”, &s) will return 0 because it fail to scan character value. And if you will not insert any value then it will return -1.
6. What will be the output of following program?
Explanation:Union has a property to store different data types in same memory location one at a time. It generate a maximum memory space container so that it stores the every data type value. As according to union property sizeof operator will return maximum memory allocation in union container (Maximum no. of bytes).
This article is contributed by Ashish Varshney. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.