Output of C Programs | Set 2 Last Updated : 23 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Predict the output of below programs. Question 1 c #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } 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 C #include<stdio.h> int main() { static int i=5; if(--i){ main(); printf("%d ",i); } } Output0 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 c #include<stdio.h> int main() { static int var = 5; printf("%d ",var--); if(var) main(); } Output5 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 C #include<stdio.h> int main() { int x; printf("%d",scanf("%d",&x)); /* Suppose that input value given for above scanf is 20 */ return 1; } Output1 scanf returns the no. of inputs it has successfully read.Question 5 C # 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; } Output16 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. Before starting the next iteration, i becomes 17 due to i++. 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; } Comment More infoAdvertise with us Next Article Output of C programs | Set 32 K kartik Follow Improve Article Tags : C Language C-Output Similar Reads Output of C programs | Set 42 QUE.1 What is the output of following program? C #include <stdio.h> int main() { int x = 10, *y, **z; y = &x; z = &y; printf("%d %d %d", *y, **z, *(*z)); return 0; } a. 10 10 10 b. 100xaa54f10 c. Run time error d. No Output Answer : a Explanation: Because y contains the addre 3 min read Output of C programs | Set 32 Predict the output of the following C programs.1. What will be the output of following program? Input: 1 3 C #include<stdio.h> int main() { int a, b; if(scanf("%d%d", &a, &b)==2) printf("true"); else printf("false"); return 0; } Output: True Explanation: S 3 min read Output of C programs | Set 52 1. What will be the output of following program? CPP #include <stdio.h> int main() { int a = 5, *b, c; b = &a; printf("%d", a * *b * a + *b); return (0); } Options: 1. 130 2. 103 3. 100 4. 310 The answer is the option(1). Explanation: Here the expression a**b*a + *b uses pointer 3 min read Output of C++ programs | Set 42 Prerequisite : Pointers and References Q.1 What Is The Output Of this program? CPP #include <iostream> using namespace std; void fun(int& a, int b) { a += 2; b += 1; } int main() { int x = 10, y = 2; fun(x, y); cout << x << " " << y << " "; fun(x 4 min read Output of C Programs | Set 1 Predict the output of below programs. Question 1c#include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }Output: Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2c #include<stdio.h 2 min read Output of C programs | Set 8 Predict the output of below C programs. Question 1: c #include<stdio.h> int main() { int x = 5, p = 10; printf("%*d", x, p); getchar(); return 0; } Output: 10 Explanation: Please see standard printf function definition int printf ( const char * format, ... ); format: String that cont 3 min read Like