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
filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int a, b;
    if(scanf("%d%d", &a, &b)==2)
        printf("true");
    else
        printf("false");
    return 0;
}

chevron_right


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?:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int a=0;
    a=a++ + a++ - a++ + ++a;
    printf("%d\n", a);
    return 0;
}

chevron_right


Output:

3

Explanation: Prefix and postfix increment/decrement operator associativity is from Right to Left. So, in the above question it start executing from right side i.e. (++a).

In the first step 
A = (a++) + (a++)-(a++) + (1)
In the second step 
A = (a++) + (a++)-(1) + (1)
In the third step 
A = (a++) + (2)-(1) + (1)
In the fourth step 
A = (3) + (2)-(1) + (1)
Therefore final value of A is 3.

3. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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]);
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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);
}

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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.



My Personal Notes arrow_drop_up


Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.