Output of C Programs | Set 7

Predict the output of below programs

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    int i = 0;
    while (i <= 4)
    {
       printf("%d", i);
       if (i > 3)
        goto inside_foo;
       i++;
    }  
    getchar();
    return 0;
}
  
void foo()
{
   inside_foo:
     printf("PP");
}

chevron_right


Output: Compiler error: Label “inside_foo” used but not defined.



Explanation: Scope of a label is within a function. We cannot goto a label from other function.

Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#define a 10
int main()
{
  #define a 50
  printf("%d",a);
    
  getchar();
  return 0;
}

chevron_right


Output: 50



Preprocessor doesn’t give any error if we redefine a preprocessor directive. It may give warning though. Preprocessor takes the most recent value before use of and put it in place of a.

Now try following

filter_none

edit
close

play_arrow

link
brightness_4
code

#define a 10
int main()
{
  printf("%d ",a);  
  #define a 50
  printf("%d ",a);
    
  getchar();
  return 0;
}

chevron_right



Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
     char str[] = "geeksforgeeks";
     char *s1 = str, *s2 = str;     
     int i;
       
     for(i = 0; i < 7; i++)
     {
        printf(" %c ", *str);
        ++s1;     
     }
       
     for(i = 0; i < 6; i++)
     {
        printf(" %c ", *s2);
        ++s2;     
      }
      
      getchar();
      return 0;
}

chevron_right


Output
g g g g g g g g e e k s f

Explanation
Both s1 and s2 are initialized to str. In first loop str is being printed and s1 is being incremented, so first loop will print only g. In second loop s2 is incremented and s2 is printed so second loop will print “g e e k s f ”

Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    char str[] = "geeksforgeeks";
    int i;
    for(i=0; str[i]; i++)
        printf("\n%c%c%c%c", str[i], *(str+i), *(i+str), i[str]);
     
    getchar();
    return 0;
}

chevron_right


Output:
gggg
eeee
eeee
kkkk
ssss
ffff
oooo
rrrr
gggg
eeee
eeee
kkkk
ssss



Explanaition:
Following are different ways of indexing both array and string.

arr[i]
*(arr + i)
*(i + arr)
i[arr]

So all of them print same character.

Question 5

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
    char *p;
    printf("%d %d ", sizeof(*p), sizeof(p));
     
    getchar();
    return 0;
}

chevron_right


Output: Compiler dependent. I got output as “1 4”

Explanation:
Output of the above program depends on compiler. sizeof(*p) gives size of character. If characters are stored as 1 byte then sizeof(*p) gives 1.
sizeof(p) gives the size of pointer variable. If pointer variables are stored as 4 bytes then it gives 4.



My Personal Notes arrow_drop_up


Article Tags :

1


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