Output of C programs | Set 47 (Decision and Control Statements) - GeeksforGeeks

30 Aug, 2017
Decision and Loops & Control Statements
QUE.1 What is the output of this program ?
#include <stdio.h>#include <stdio.h>  void main(){    while (printf("geeks")) {    }}
OPTIONa) geeksb) infinite time geeksc) Compile time errord) No Output
Answer: b
Explanation: printf returns the number of characters of “geeks”. It returns 5 and loop runs infinite times because 5>0 and it is neither increasing nor decreasing. So it will print “geeks” infinite times
QUE.2 What is the output of this program?
#include <stdio.h>  int main(){    while (printf("geeks"))        return 0;}
OPTIONa) geeksb) infinite time geeksc) compile time errord) no output
Answer:  a
Explanation: printf returns the number of characters “geeks”. It will return 5 but when it enters int, the body of loop it will get “return 0” and terminates the program so it will print “geeks” only one time.
QUE. 3 What is the output of this program ?
#include <stdio.h>  int main(){    if (printf("geeks"))        switch (printf("for"))            while (printf("geeks"))                return 0;}
OPTIONa) geeksb) forc) geeksford) geeksforgeeks
Answer: c
Explanation: if, switch and while are condition checkers in his () and print anything written in his (). In this program, first run if() and printf return “geeks” 5 and come to 2 switch. Now switch print “for” and printf return 3 and now switch find case 3 and case 3 is not in the program and it terminates the program and print only “geeksfor”.
QUE.4 What is the output?
#include <stdio.h>  int main(){    if (printf("geeks") != 5) {      } else        printf("geeksforgeeks");    return 0;}
OPTIONa) geeksb) geeksforgeeksc) geeksgeeksforgeeksd) Compile Errors
Answer: c
Explanation: First time, the if block is checked after printing geeks. Then check condition if(printf(“geeks”)!=5). Here condition is false then go to else part and print “geeksforgeeks” also. Then, go to the else part and print then output is geeksgeeksforgeeks.
QUE.5 What is output of this program?
#include <stdio.h>#define int n = printf("geeks")int main(){    int n = 10;    printf("%d", n);    return 0;}
OPTIONa) geeksb) 10c) geeks 10d) Compile Errors
Answer: d
Explanation:Error: Invalid InitializationYou cannot define a printf as a int n=printf(). The data definition has no type or storage class.
Related Article : Quiz on Loop and Control structures
This article is contributed by Ajay Puri. 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.
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of C++ programs | Set 34 (File Handling)
Result of sizeof operator
Program to reverse columns in given 2D Array (Matrix)
Output of C++ Program | Set 1
Output of Java program | Set 26
Output of C++ programs | Set 22
unsigned specifier (%u) in C with Examples
Output of C Programs | Set 7
Output of C Programs | Set 6
Output of C programs | Set 8