Output of C programs | Set 54
1. What will be the output of the below program?
C++
#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: https://www.geeksforgeeks.org/interesting-facts-preprocessors-c/
2. What will be the output of the following program?
C++
#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 strlen() function return the length without counting \0(null) character but sizeof() function return the length of string with \0 character.
Refer: https://www.geeksforgeeks.org/difference-strlen-sizeof-string-c-reviewed/
3. Guess the output?
C++
#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?
C++
#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), i); } return (0); } |
GGGGFFFFGGGG
Options:
1. compile time error
2. GGGFFFGGG
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 first loop it prints GGG and in the 2nd loop FFF and at last in the 3rd loop GGG.
5. What will be the output of following program?
C++
#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 the conversion of 055 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 write.geeksforgeeks.org or mail your article to review-team@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.
Please Login to comment...