Output of C++ programs | Set 41 (Structure and Union)
Last Updated : 29 Aug, 2017
Prerequisite:
Structures and
Union QUE.1 What will be the output of this code? CPP #include <iostream> using namespace std; typedef struct s1 { int a; float b; union g1 { char c; double d; }; } new1; int main() { cout << sizeof(new1); return 0; }
(a)17 (b)16 (c)8 (d)9 Output:
(c) 8
Explanation : In Struct block, it adds the size of float and int so the size is 8 then In union it only declare But not initialize so it gives 8 in the Output.
QUE.2 What will be the output of this Question ? CPP #include <iostream> using namespace std; typedef struct s1 { int a; float b; union g1 { char c; double d; } new2; } new1; int main() { cout << sizeof(new1); }
(a)17 (b)16 (c)8 (d)9 Output:
(b)16
Explanation : In Struct block, it adds the size of float and int so the size is 8 then In union we Define and Declare both in this block so union gives 8 so it gives 16 in the Output.
QUE.3 What will be the output of this question? CPP #include <iostream> using namespace std; typedef struct s1 { int a; float b; union g1 { double c; double d; } new2; } new1; int main() { cout << sizeof(new1) << endl; new1 obj; obj.new2.d = 17; obj.new2.c = 20; cout << obj.new2.d; return 0; }
(a)17 20 (b)16 20 (c)16 17 (d)24 20 Output:
(b) 16 20
Explanation: In this Question: In union, Block Contain maximum size data type like double so the memory contain 8 byte and when we initialize ‘d’ and ‘c’ then ‘d’ value is replace by ‘c’ because memory blocks are same so when we print ‘d’ and ‘c’ value, it will be print 20 and about size I explained in above examples so it will print 16.
QUE.4 What will be the output of this question? CPP #include <iostream> using namespace std; typedef struct s1 { int a; float b; struct g1 { double c; double d; }; } new1; int main() { cout << sizeof(new1) << endl; return 0; }
(a)8 (b)16 (c)4 (d)24 Output:
(a)8
Explanation: In Struct block it adds the size of float and int so the size is 8 then In struct g1 block it only declare But not initialize so overall it gives 8 in the Output.
QUE.5 What will be the output of this question? CPP #include <iostream> using namespace std; typedef struct s1 { int a; float b; struct g1 { double c; double d; } new2; } new1; int main() { cout << sizeof(new1) << endl; return 0; }
(a)8 (b)16 (c)4 (d)24 Output:
(d)24
Explanation : In Struct block it adds the size of float and int so the size is 8 then In struct g1 block that time it is declared and initialized both so it add size of ‘c’ and ‘d’ so is contain total size 24 so the output Is 24.
Related Articles : Similar Reads
Output of C programs | Set 44 (Structure & Union) Prerequisite: Structure and UnionQUE.1 What is the output of this program? C #include <stdio.h> struct sample { int a = 0; char b = 'A'; float c = 10.5; }; int main() { struct sample s; printf("%d, %c, %f", s.a, s.b, s.c); return 0; } OPTION a) Error b) 0, A, 10.5 c) 0, A, 10.500000
2 min read
Output of C programs | Set 34 Ques 1. Assume that the size of an integer is 4 bytes and size of character is 1 byte. What will be the output of following program? C #include <stdio.h> union test { int x; char arr[8]; int y; } u; int main() { printf("%u", sizeof(u)); return 0; } options : A)12 B)16 C)8 D)4 Answer
3 min read
Output of C++ Program | Set 1 Predict the output of below C++ programs. Question 1 CPP // Assume that integers take 4 bytes. #include<iostream> using namespace std; class Test { static int i; int j; }; int Test::i; int main() { cout << sizeof(Test); return 0; } Output: 4 (size of integer) static data members do not c
1 min read
Output of C++ programs | Set 35 1. What will be the output of following program? CPP #include<iostream> using namespace std; int main() { int x = 5; if(x==5) { if(x==5) break; cout<<"Hello"; } cout<<"Hi"; } Options A) Compile error B) Hi C) HelloHi D) Hello Answer : A Explanation: Compile erro
3 min read
Output of C Program | Set 17 Predict the output of following C programs. Question 1 C #include<stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf("%d", sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de-
2 min read