Skip to content
Related Articles

Related Articles

Output of C programs | Set 63
  • Difficulty Level : Easy
  • Last Updated : 20 Dec, 2017

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :