Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Output of C++ programs | Set 41 (Structure and Union)
Next article icon

Output of C++ programs | Set 41 (Structure and Union)

Last Updated : 29 Aug, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report
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 :
  • Output of C programs | Set 44 (Structure & Union)
  • Quiz on Structure and Union
  • Difference between C structures and C++ structures

Next Article
Output of C++ programs | Set 41 (Structure and Union)

D

Devanshu Agarwal
Improve
Article Tags :
  • C++
  • cpp-structure
  • C-Output
  • C-Structure & Union
Practice Tags :
  • CPP

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences