Output of C programs | Set 45

1. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
void main()
{
    int i = 10;
    static int x = i;
    if (x == i)
        printf("equal");
    else if (x < i)))
        printf("less than");
    else
        printf("greater than");
}

chevron_right


Options:
(a)equal
(b)greater than
(c)less than
(d)compiler error
(e)none of the above

Answer: (d)Compiler error

Explanation: Here ‘x’ is a static variable and ‘i’ is an auto variable. Auto variables are run time entities, compared to static, which are load time entities. Run time variables cannot be initialized with load time variables.



2. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
void main()
{
    printf("%s", "i"
                 "am"
                 "good");
}

chevron_right


Options:
(a)i am good
(b)i
(c)good
(d)iamgood
(e)Compiler error

Answer: (d)iamgood

Explanation: In C, string constant “ab” is same as “a” “b”.

3. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
#include <string.h>
void main()
{
    printf("%d %d", sizeof("program"), strlen("program"));
}

chevron_right


Options:

(a)7 7
(b)8 8
(c)8 7
(d)7 8
(e)None of the above

Answer: (c)8 7

Explanation: strlen returns length of string without counting the null character, whereas sizeof also includes the null character when counting size of string.

4. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
void main()
{
    int colour = 2;
    switch (colour) {
    case 0:
        printf("Black");
    case 1:
        printf("Red");
    case 2:
        printf("Aqua");
    case 3:
        printf("Green");
    default:
        printf("Other");
    }
}

chevron_right


Options:
(a)Aqua
(b)AquaGreenOther
(c)AquaGreen
(d)Red
(e)none of the above

Answer: (b)AquaGreenOther

Explanation: There are no break statements in switch case, so all statements after case 2, including the default statement, will be executed.


5. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
void main()
{
    if (printf("cisgood"))
        printf("i know c");
    else
        printf("i know c++");
}

chevron_right


Options:
(a)i know c
(b)i know c++
(c)cisgoodi know c
(d)cisgoodi know c++
(e)compiler error

Answer: (c)cisgoodi know c

Explanation: The return type of printf is integer, that is the number of characters including blank spaces. So, here in if condition, printf evaluates to 7, which is non-negative. Thus follows the true condition.

This article is contributed by Nihar Ranjan Sarkar. 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

Improved By : shi143din



Article Tags :

7


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