Output of C Programs | Set 2

Predict the output of below programs.

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

char *getString()
{
    char str[] = "Will I be printed?";    
    return str;
}
int main()
{
    printf("%s", getString());
    getchar();
}

chevron_right


Output: Some garbage value
The above program doesn’t work because array variables are stored in Stack Section. So, when getString returns values at str are deleted and str becomes dangling pointer.

Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    static int i=5;
    if(--i){
        main();
        printf("%d ",i);
    }   
}

chevron_right


Output: 0 0 0 0
Explanation: Since i is a static variable and is stored in Data Section, all calls to main share same i.

Question 3



filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    static int var = 5;
    printf("%d ",var--);
    if(var)
        main();                   
}

chevron_right


Output: 5 4 3 2 1
Explanation: Same as previous question. The only difference here is, sequence of calling main and printf is changed, therefore different output.

Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    int x;
    printf("%d",scanf("%d",&x)); 
    /* Suppose that input value given 
        for above scanf is 20 */
    return 1;

chevron_right


Output: 1
scanf returns the no. of inputs it has successfully read.

Question 5

filter_none

edit
close

play_arrow

link
brightness_4
code

# include <stdio.h>
int main()
{
   int i=0;
   for(i=0; i<20; i++)
   {
     switch(i)
     {
       case 0:
         i+=5;
       case 1:
         i+=2;
       case 5:
         i+=5;
       default:               
         i+=4;
         break;
     }
     printf("%d  ", i);
   }
  
   getchar();
   return 0;
}   

chevron_right


Output: 16 21
Explanation:
Initially i = 0. Since case 0 is true i becomes 5, and since there is no break statement till last statement of switch block, i becomes 16. Now in next iteration no case is true, so execution goes to default and i becomes 21.

In C, if one case is true switch block is executed until it finds break statement. If no break statement is present all cases are executed after the true case. If you want to know why switch is implemented like this, well this implementation is useful for situations like below.

 switch (c)
 {
    case 'a':
    case 'e':
    case 'i' :
    case 'o':
    case 'u':
      printf(" Vowel character");
      break;
    default : 
      printf("Not a Vowel character");; break;    
  }


My Personal Notes arrow_drop_up



Article Tags :

Be the First to upvote.


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