Skip to content
Related Articles

Related Articles

Output of C programs | Set 29
  • Difficulty Level : Medium
  • Last Updated : 14 Aug, 2019

Question 1




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




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




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




#include <stdio.h>
  
int extern i;
  
int main()
{
    printf("%d", i);
}
  
int i = 10;
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






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




#include<stdio.h>
  
int main()
{
   printf("%s", "Geeks\nFor\nGeeks\nis\nthe\nbest");
}
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
Recommended Articles
Page :