Output of C programs | Set 63
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.
Recommended Posts:
- Output of C programs | Set 37
- Output of C programs | Set 65 (If-Else)
- Output of C++ programs | Set 42
- Output of C++ programs | Set 44
- Output of C++ programs | Set 24 (C++ vs C)
- Output of C programs | Set 46
- Output of C++ programs | Set 23
- Output of C programs | Set 45
- Output of C++ programs | Set 21
- Output of C programs | Set 63
- Output of C programs | Set 48
- Output of C programs | Set 52
- Output of C programs | Set 54
- Output of C++ Programs | Set 49
- Output of C++ programs | Set 45
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

