Output of C Program | Set 21

Predict the output of following C programs.

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Be careful when dealing with macros. Extra spaces may lead to problems.



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int i = 20,j;
    i = (printf("Hello"), printf(" All Geeks "));
    printf("%d", i);
  
    return 0;
}

chevron_right


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



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.