Output of C programs | Set 54

1. What will be the output of the bellow program?

#include <stdio.h>
#define GEEKS 100
int main()
{
#define GEEKS 100
    printf("%d", GEEKS);
    return (0);
}
Output:
- 100

Options:
1. 100
2. compile error
3. No output
4. abnormal termination

The answer is option(1).

Explanation : As GEEKS macro replace the GEEKS with 100 token.
Refer : http://www.geeksforgeeks.org/interesting-facts-preprocessors-c/

2. What will be the output of the following program?

#include <stdio.h>
#include <string.h>
int main()
{
    printf("%d%d", sizeof("GEEKSFORGEEKS"), strlen("GEEKSFORGEEKS"));
    return (0);
}

Options:
1. 1314
2. 1134
3. 1413
4. 3114

 The output is option(3).

Explanation : As we all know that sizeof() function return the length without counting \0(null) character but strlen() function return the length of string with \0 character.
Refer : http://www.geeksforgeeks.org/difference-strlen-sizeof-string-c-reviewed/

3. Guess the output?

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

Options:
1. -4
2. compile error
3. runtime error
4. 4

The answer is option(4).

Explanation : When we use unary minus(-) operator twice then the compiler treats as plus.

4. What will be the output?

#include <stdio.h>
int main()
{
    char c[] = "GFG";
    int i;
    for (i = 0; c[i]; i++) {
        printf("%c%c%c%c", c[i], *(c + i), *(i + c), i1);
    }
    return (0);
}

Options:
1. compile time error
2. GGGGFFFFGGGG
3. GFG
4. No output

 The answer is option(2).

Explanation : Here all c[i], *(c+i), *(i+c), i represent the same thing. In the first loop, all four specifiers points to G that’s why in the fist loop it prints GGGG and in the 2nd loop FFFF and at last in the 3rd loop GGGG.

5. What will be the output of following program?

#include <stdio.h>
int main()
{
    int a = 055, b = 55;
    printf("%d%d", a, b);
    return (0);
}

Options:
1. 4555
2. 5545
3. 5545
4. 5554

 The answer is option(1).

Explanation : Any integer number which is prefix with 0 indicates as Octal number. Now after conversion of 045 into decimal number we have 45 and b is 55. Therefore the answer is 4555.

This article is contributed by Bishal Kumar Dubey. 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.