Output of C programs | Set 63

1) What is the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



2 1 3

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

3) What is the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
char A()
{
    char c = 'B';
    return c;
}
  
int main()
{
    printf("%lu", sizeof(A()));
    return 0;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Avesh_Agrawal