Output of C Program | Set 21
Predict the output of following C programs.
Question 1
#include<stdio.h> #define fun (x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i < t; i++) printf("GeesforGeeks\n"); return 0; } |
Output: Compiler Error
There is an extra space in macro declaration which causes fun to be raplaced by (x). If we remove the extra space then program works fine and prints “GeeksforGeeks” 50 times. Following is the working program.
#include<stdio.h> #define fun(x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i < t; i++) printf("GeesforGeeks\n"); return 0; } |
Be careful when dealing with macros. Extra spaces may lead to problems.
Question 2
#include<stdio.h> int main() { int i = 20,j; i = (printf("Hello"), printf(" All Geeks ")); printf("%d", i); return 0; } |
Output: Hello All Geeks 11
The printf() function returns the number of characters it has successfully printed. The comma operator evaluates it operands from left to right and returns the value returned by the rightmost expression (See this for more details). First printf(“Hello”) executes and prints “Hello”, the printf(” All Geeks “) executes and prints ” All Geeks “. This printf statement returns 11 which is assigned to i.
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above
Recommended Posts:
- Output of C Program | Set 29
- Output of C++ Program | Set 20
- Output of C++ Program | Set 19
- Output of C++ Program | Set 11
- Output of C++ Program | Set 2
- Output of C++ Program | Set 3
- Output of C Program | Set 18
- Output of C Program | Set 28
- Output of C Program | Set 19
- Output of C Program | Set 26
- Output of C program | Set 25
- Output of C Program | Set 24
- Output of C++ Program | Set 12
- Output of C++ Program | Set 15
- Output of C++ Program | Set 14



