Output of C programs | Set 29

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int i;
    for (i = 0; i<5; i++)
    {
        int i;
        i = 10;
        printf("%d ", i) ;
    }
    return 0;
}

chevron_right


Output:

10 10 10 10 10

Explanation:
Here, it would seem that, as ‘i’ got assigned a value 10, so the loop will run only one time because the loop condition will fail as ‘i’ becomes 11 the next time it enters the loop. But the output is quite different than what we expected. The reason behind this is the scope of variable is within the method. As soon as the loop exits , i is counted according to i of for loop which makes the condition true upto 5 times (0-4). See scope rules for more details.
Here, the loop runs according to the ‘i’ declared outside the loop, and hence it will run five times and the ‘i’ inside the loop will be printed five times.



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int i = 10;
  
    static int x = i;
  
    if (x==i)
        printf("x and i are Equal\n");
  
    return 0;
}

chevron_right


Output:

x and i are Equal

Explanation:
As x and i are equal in value, the if condition satisfies and prints “x and i are equal”.
Note: In C this same program will produce a compile-time error as, static variables are load time entity while auto variables are run-time entity and we can not initialize any load time variable by the run time variable.

Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <string.h>
  
int main()
{
    printf("GEEKS size = %ld \n\n", sizeof("GEEKS"));
    printf("GEEKS length = %ld \n", strlen("GEEKS"));
    return 0;
}

chevron_right


Output:

GEEKS size = 6 

GEEKS length = 5

Explanation:
sizeof() function returns the size of the string including null character while strlen() function returns length of the string excluding null character.

Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int extern i;
  
int main()
{
    printf("%d", i);
}
  
int i = 10;

chevron_right


Output:

10

Explanation:
With the help of extern keyword, the declaration of variable can be done any where in the program. Here, i is a variable which is being declared after the printf statement outside the main. Still, it can be accessed by the printf statement as it is an extern variable. For more details on the keyword extern

Question 5



filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
   int a = 10;
   printf("%o %x", a, a);
   return 0;
}

chevron_right


Output:

12 a

Explanation:
As, %o is used to print the number in octal number format and %x is used to print the number in hexadecimal number format. And if we convert decimal 10 to octal , we get 12 and in hexadecimal it will give a

Question 6

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int main()
{
   printf("%s", "Geeks\nFor\nGeeks\nis\nthe\nbest");
}

chevron_right


Output:

Geeks
For
Geeks
is
the
best

Explanation:
Instead of getting output as Geeks\nFor\nGeeks\nis\nthe\nbest, the output is something different. %s treats \n as ‘new line’ even if it is inside a string.

This article is contributed by Mazhar Imam Khan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Ravirao1981, guptamax84



Article Tags :

5


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