Output of C programs | Set 32
Predict the output of the following C programs.
1. What will be the output of following program?
Input:
1 3
C
#include<stdio.h> int main() { int a, b; if ( scanf ( "%d%d" , &a, &b)==2) printf ( "true" ); else printf ( "false" ); return 0; } |
Output:
True
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?:
C
#include<stdio.h> int main() { int a=0; a=a++ + a++ - a++ + ++a; printf ( "%d\n" , a); return 0; } |
Output:
3
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?
C
#include<stdio.h> int ar[] = {18, 23, 45, 56, 4, 6, 45}; int main() { int i, x; x = ( sizeof (ar)/ sizeof (ar[0])); for (i=-1; i<=(x-2); i++) printf ( "%d, " , ar[i+1]); } |
Output:
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[0]) 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?
C
#include<stdio.h> int count; void display(); int main() { count = 1; printf ( "Value of count is %d, " , count); display(); } void display() { extern int count; printf ( "\nValue of count is %d" , count); } |
Output:
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?
Input:
1 c
C
#include<stdio.h> int main() { int flag = 0, flag1 = 0, n; char s; flag = scanf ( "%d" , &n); flag1 = scanf ( "%d" , &s); printf ( "Value of flag is %d" , flag); printf ( "\nValue of flag is %d" , flag1); return 0; } |
Output:
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?
C
#include<stdio.h> union student { int y[34]; char var2[7]; char arr[8]; char ch[5]; }; int main() { union student st; printf ( "%d" , sizeof ( union student)); return 0; } |
Output:
136
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 contribute@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...