Output of C programs | Set 63

2.5

1) What is the output of the following program?

#include <stdio.h>
#include <string.h>
int main(void)
{
    char* p = "geeks";
    printf("%lu %lu %lu ", sizeof(p), sizeof(*p), sizeof("geeks"));
    printf("%lu %lu", strlen(p), strlen("geeks"));
    return 0;
}

Output:

8 1 6 5 5

Explanation : p is a pointer so sizeof returns sizeof(char*), *p is of type char, sizeof(“geeks”) returns the number of characters including the null character, strlen(p) returns length of string pointed by p without null character and strlen returns number of characters without null character.

2) What is the output of the following program?

#include <stdio.h>
int main()
{
    int array[] = {[1] = 1, [0] = 2, [2] = 3 };
    printf("%d %d %d", array[0], array[1], array[2]);
    return 0;
}

Output:

2 1 3

Explanation : The above initialization technique is unique but allowed in C.

3) What is the output of the following program?


#include <stdio.h>
char A()
{
    char c = 'B';
    return c;
}

int main()
{
    printf("%lu", sizeof(A()));
    return 0;
}

Output:

1

Explanation : Even if this is function pointer, the sizeof will get the return type and returns the size of that data type. Here, the return type is char, so the output is 1.

4) What is the output of the following program?


#include <stdio.h>
int main()
{
    int n;
    n = (int)sizeof(!2.3);
    printf("%d", n);
    return 0;
}

Output:

4

Explanation: The ‘!’ operator takes an argument and return 1 if the value is 0 and 0 otherwise. So, !2.3 is 0. To sizeof 0 is an integer and so the output is 4.

5) What is the output of the following program?


#include <stdio.h>
#define declare(x) int x;
int main()
{
    int a;
    declare(a)
        printf("declare(a)");
    return 0;
}

Output:

Compilation error

Explanation: The macro declare will be replaced before the compilation and the result code will have “int a” declaration twice in the program which will cause compilation error.

This article is contributed by Avesh_Agrawal. 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:



2.5 Average Difficulty : 2.5/5.0
Based on 2 vote(s)










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