Difference Between C Structures and C++ Structures - GeeksforGeeks

02 Mar, 2022
Let’s discuss what are the differences between structures in C and structures in C++? In C++, structures are similar to classes.
C Structures
C++ Structures
Similarities Between the C and C++ Structures
Both in C and C++, members of the structure have public visibility by default.
Lets discuss some of the above mentioned differences and similarities one by one:
1. Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.
C
// C Program to Implement Member// functions inside structure  #include <stdio.h>  struct marks {    int num;      // Member function inside Structure to    // take input and store it in "num"    void Set(int temp) { num = temp; }      // function used to display the values    void display() { printf("%d", num); }};  // Driver Programint main(){    struct marks m1;    // calling function inside Struct to    // initialize value to num    m1.Set(9);      // calling function inside struct to    // display value of Num    m1.display();}
Output
This will generate an error in C but no error in C++. 
C++
// C++ Program to Implement Member functions inside// structure  #include <iostream>using namespace std;  struct marks {    int num;      // Member function inside Structure to    // take input and store it in "num"    void Set(int temp) { num = temp; }      // function used to display the values    void display() { cout << "num=" << num; }};  // Driver Programint main(){    marks m1;      // calling function inside Struct to    // initialize value to num    m1.Set(9);      // calling function inside struct to    // display value of Num    m1.display();}
  
num=9
 
2. Static Members: C structures cannot have static members but are allowed in C++. 
 
C
C++
// C program with structure static member  struct Record {    static int x;};  // Driver programint main() { return 0; }
// C++ program with structure static member  struct Record {    static int x;};  // Driver programint main() { return 0; }
This will generate an error in C but not in C++. 
3. Constructor creation in structure: Structures in C cannot have a constructor inside a structure but Structures in C++ can have Constructor creation.
C
C++
// C program to demonstrate that// Constructor is not allowed  #include <stdio.h>  struct Student {    int roll;    Student(int x) { roll = x; }};  // Driver Programint main(){    struct Student s(2);    printf("%d", s.x);    return 0;}
// CPP program to initialize data member in c++#include <iostream>using namespace std;  struct Student {    int roll;    Student(int x) { roll = x; }};  // Driver Programint main(){    struct Student s(2);    cout << s.roll;    return 0;}
This will generate an error in C.
Output in C++:
2
4. Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++. 
C
C++
// C program to demonstrate that direct// member initialization is not possible in C  #include <stdio.h>  struct Record {    int x = 7;};  // Driver Programint main(){    struct Record s;    printf("%d", s.x);    return 0;}
// CPP program to initialize data member in c++#include <iostream>using namespace std;  struct Record {    int x = 7;};  // Driver Programint main(){    Record s;    cout << s.x << endl;    return 0;}
This will generate an error in C.
Output in C++: 
7
5. Using struct keyword: In C, we need to use a struct to declare a struct variable. In C++, a struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct, and using ‘Record‘ only would work.
6. Access Modifiers: C structures do not have access modifiers as these modifiers are not supported by the language. C++ structures can have this concept as it is inbuilt in the language.  
7. Pointers and References: In C++, there can be both pointers and references to a struct in C++, but only pointers to structs are allowed in C. 
8. sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.  
C
C++
// C program to illustrate empty structure  #include <stdio.h>  // empty structurestruct Record {};  // Driver Codeint main(){    struct Record s;    printf("%lu\n", sizeof(s));    return 0;}
// C++ program to illustrate empty structure#include <iostream>using namespace std;  // empty structurestruct Record {};  // Driver programint main(){    struct Record s;    cout << sizeof(s);    return 0;}  // This code is contributed by Shubham Sharma
Output in C: 
0
Output in C++: 
1
NOTE:   The default type of  sizeof  is long unsigned int , that’s why  “%lu” is used instead of “%d” in printf function.
9. Data Hiding: C structures do not allow the concept of Data hiding but are permitted in C++ as it is an object-oriented language whereas C is not.  Related Article: Structure vs Class in C++This article is contributed by Shubham Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
gyanendra371
iamsamyak
shubhamsharma8337
w1ckd3
23603vaibhav2021
anshikajain26
Spider_man
nehakumariintern
cpp-structure
C Language
C++
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
Function Pointer in C
Substring in C++
rand() and srand() in C/C++
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects