Skip to content
Related Articles

Related Articles

Improve Article

Output of C Program | Set 21

  • Difficulty Level : Medium
  • Last Updated : 28 Sep, 2018

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :