Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Difference between Structure and Array in C
Next article icon

Difference between Struct and Enum in C/C++ with Examples

Last Updated : 05 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Structure in C++

A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The ‘struct’ keyword is used to create a structure. 

Syntax:

struct structureName{

    member1;

    member2;

    member3;

    .

    .

    .

    memberN;

};

Below is the implementation:

C++
#include<stdio.h>  struct Point { int x, y; };  int main() { struct Point p1 = {0, 1};  // Accessing members of point p1 p1.x = 10; printf ("x = %d, y = %d", p1.x, p1.y);  return 0; } 

Output
x = 10, y = 1

Structures in C++ can contain two types of members:  

  • Data Member: These members are normal C++ variables. We can create a structure with variables of different data types in C++.
  • Member Functions: These members are normal C++ functions. Along with variables, we can also include functions inside a structure declaration.

Enum in C++

Enum is the short name for enumeration. It is a user-defined data type. It is used to define a list of options that can be selected. Once, enum is declared we cannot change its value, the compiler will throw an error. Two enumerations cannot share the same names.

enum enumName{

    member1;

    member2;

    member3;

    .

    .

    .

    memberN;

};

Below is the implementation:

C++
#include <bits/stdc++.h> using namespace std;  // Defining enum Year enum year {     Jan,     Feb,     Mar,     Apr,     May,     Jun,     Jul,     Aug,     Sep,     Oct,     Nov,     Dec };  // Driver Code int main() {     int i;      // Traversing the enum year     for (i = Jan; i <= Dec; i++)         cout << i << endl;      return 0; } 

Output
0 1 2 3 4 5 6 7 8 9 10 11


Difference between Struct and Enum

S No.StructEnum
1The "struct" keyword is used to declare a structureThe "enum" keyword is used to declare enum.
2The structure is a user-defined data type that is a collection of dissimilar data types.Enum is to define a collection of options available.
3A struct can contain both data variables and methods. Enum can only contain data types.
4A struct supports a private but not protected access specifier.Enum does not have private and protected access specifier.
5Structure supports encapsulation.Enum doesn't support encapsulation.
6When the structure is declared, the values of its objects can be modified.Once the enum is declared, its value cannot be changed, otherwise, the compiler will throw an error.
7

Struct only contains parameterized constructors and no destructors. 

The compiler does not generate a default constructor for a struct.

Enum does not contain constructors and destructors.
8The values allocated to the structure are stored in stack memory.The memory to enum data types is allocated in the stack.

Next Article
Difference between Structure and Array in C

R

rn540
Improve
Article Tags :
  • Difference Between
  • C Language
  • C++
  • C-Struct-Union-Enum
Practice Tags :
  • CPP

Similar Reads

  • Difference Between Structure and Class in C++
    In C++, a structure works the same way as a class, except for just two small differences. The most important of them is hiding implementation details. A structure will by default not hide its implementation details from whoever uses it in code, while a class by default hides all its implementation d
    3 min read
  • Difference Between Structure and Union in C
    In C programming, both structures and unions are used to group different types of data under a single name, but they behave in different ways. The main difference lies in how they store data. The below table lists the primary differences between the C structures and unions: Parameter Structure Union
    4 min read
  • Difference Between C Structures and C++ Structures
    Let's discuss, what are the differences between structures in C and structures in C++? In C++, structures are similar to classes. Differences Between the C and C++ StructuresC Structures C++ Structures Only data members are allowed, it cannot have member functions.Can hold both: member functions and
    6 min read
  • Difference between Swift Structures and C Structure
    Swift structures are a basic building block in the Swift programming language. They are used to group related data together in a single unit. They are similar to classes, but differ in several key ways, including: Value Types: Structures are value types, meaning that when you pass a structure to a f
    4 min read
  • Difference between Structure and Array in C
    Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array AR
    2 min read
  • Difference Between EnumMap and EnumSet in Java
    EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are so
    3 min read
  • Difference Between EnumMap and HashMap
    EnumMap and HashMap both are the classes that implement the Map interface. But there are some differences that exist between them. So we have tried to list out the differences between EnumMap and HashMap. 1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types.
    4 min read
  • What are the differences between C and Embedded C?
    C Language C is a general-purpose programming language, which is widely used to design any type of desktop-based applications. It was developed by Dennis Ritchie as a system programming language to develop the operating system. The main features of C language include low-level access to memory, a si
    3 min read
  • Difference between Linear and Non-linear Data Structures
    Linear Data Structure: Data structure where data elements are arranged sequentially or linearly where each and every element is attached to its previous and next adjacent is called a linear data structure. In linear data structure, single level is involved. Therefore, we can traverse all the element
    5 min read
  • Difference Between STL and Standard Library in C++
    In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+
    3 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