Output of C programs | Set 62 (Declaration & Initialization)
Last Updated :
22 Nov, 2021
Prerequisite : Declaration & Initialization in C programming
Q1. Consider the following code:
C
#include <stdio.h>
void main()
{
extern int i;
i = 20;
printf("%d", sizeof(i));
}
What would be the output of the above code?
A. 2
B. 4
C. Would vary from compiler to compiler.
D. Error.
Output: D
Error: format '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int
Explanation:
Here, the compiler generates the error as extern int i is a declaration and not a definition.
Q2. Consider the following code:
C
#include <stdio.h>
void main()
{
extern int fun(float);
int a;
a = fun(3.14);
printf("%d", a);
}
int fun(aa) float aa;
{
return ((int)aa);
}
What would be the output of the above code?
A. Error.
B. 3.14
C. 0
D. 3
Output: A
Error: conflicting types for 'fun'
Explanation: The error occurs because we have mixed the ANSI prototype with K & R style of function definition. When we use ANSI prototype for a function and pass a float to the function it is promoted to a double. When the function accepts the double into a float a type mismatch occurs hence the error.
Q3. Consider the following code:
C
#include <stdio.h>
void main()
{
struct geeks {
char name;
int age;
float sal;
} struct geeks g = { "GEEKS" };
printf("%d %f", g.age, g.sal);
}
What would be the output of the above code?
A. Garbage Value
B. 0 0.000000
C. Would vary from compiler to compiler.
D. Error.
Output: B
0 0.000000
Explanation:
When an automatic structure is partially initialized, the remaining elements of the structure are initialized to 0.
Q4. Consider the following code:
C
#include <stdio.h>
void main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d", i);
}
What would be the output of the above code?
A. 5
B. 0
C. 1
D. 10
Output: C
1
Explanation:
Since x<y turns out to be true it is replaced by 1. This 1 is then compared with 5. Since this condition also turns out to be true it is replaced by 1. This 1 is then assigned to i
Q5. Consider the following code:
C
#include <stdio.h>
void main()
{
enum status { pass,
fail,
atkt };
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = fail;
stud3 = atkt;
printf("%d %d %d", stud1, stud2, stud3);
}
What would be the output of the above code?
A. 1 2 3
B. pass fail atkt
C. 1 2 0
D. 0 1 2
Output: D
0 1 2
Explanation:
Enum elements always take values like 0, 1, 2, 3, .... etc
Similar Reads
Output of C programs | Set 60 (Constants) Prerequisite: C Constants and StringsQ.1 What is the output of this program? C#include <stdio.h> int main() { const char *s = ""; char str[] = "Hello"; s = str; while(*s) printf("%c", *s++); return 0; } Options a) Error b) H c) Hello d) Hel ans:- c Explanation const char *s = "";The constant v
3 min read
Output of C programs | Set 61 (Loops) Prerequisite : Loops in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { int i, j, var = 'A'; for (i = 5; i >= 1; i--) { for (j = 0; j < i; j++) printf("%c ", (var + j)); printf("\n"); } return 0; } Options a)A B C D
3 min read
Output of C programs | Set 40 (File handling) Prerequisite : File handling 1. What is the output of this program by manipulating the text file? C #include <stdio.h> int main() { if (remove("myfile.txt") != 0) perror("Error"); else puts("Success"); return 0; } Options: a) Error b) Success c) Runtime Error d) C
3 min read
Output of C programs | Set 57 (for loop) Prerequisite : for loop Q.1 What is the output of this program? C #include <iostream> using namespace std; int main() { for (5; 2; 2) printf("Hello\n"); return 0; } Options a) compilation error b) Hello c) infinite loop d) none of the above ans: c Explanation : Putting a non zero value in cond
3 min read
Output of C programs | Set 35 (Loops) Short question based on c loops 1. What will be the output of the following code? C #include <stdio.h> int main() { int i = 0, j = 0; while (i<5,j<10) { i++; j++; } printf("%d %d", i, j); } options : a) 5 5 b) syntax error c) 5 10 d) 10 10 Answer: d Explanation : Here, both the
3 min read
Output of C Programs | Set 6 Predict the output of below programs Question 1 c int main() { unsigned int i=65000; while ( i++ != 0 ); printf("%d",i); return 0; } Output: 1 Explanation: It should be noticed that there's a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the c
4 min read