Difference between C structures and C++ structures

2.3

In C++, struct and class are exactly the same things, except for that struct defaults to public visibility and class defaults to private visibility.
Some important differences between the C and C++ structures:

  1. Member functions inside structure: Structures in C cannot have member functions inside structure but Structures in C++ can have member functions along with data members.
  2. Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++.

    C

    // C program to demonstrate that direct
    // member initialization is not possible in C
    #include<stdio.h>
    
    struct Record
    {
       int x = 7;
    };
    
    // Driver Program
    int main()
    {
        struct Record s;
        printf("%d", s.x);
        return 0;
    }
    /* Output :  Compiler Error
       6:8: error: expected ':', ',', ';', '}' or 
      '__attribute__' before '=' token
      int x = 7;
            ^
      In function 'main': */
    

    C++

    // CPP program to initialize data member in c++
    #include<iostream>
    using namespace std;
    
    struct Record
    {
        int x = 7;
    };
    
    //  Driver Program
    int main()
    {
        Record s;
        cout << s.x << endl;
        return 0;
    }
    // Output
    // 7
    


    Output:

    7
  3. Using struct keyword: In C, we need to use struct to declare a struct variable. In C++, 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.
  4. Static Members: C structures cannot have static members but is allowed in C++.

    C

    // C program with structure static member
    struct Record
    {
        static int x;
    };
    
    // Driver program
    int main()
    {
        return 0;
    }
    /* 6:5: error: expected specifier-qualifier-list 
       before 'static'
         static int x;
         ^*/
    

    C++

    // C++ program with structure static member
    
    struct Record
    {
        static int x;
    };
    
    // Driver program
    int main()
    {
        return 0;
    }


    This will generate an error in C but no error in C++.
  5. sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.
    // C program to illustrate empty structure
    #include<stdio.h>
    
    //empty structure
    struct Record
    {
    };
    
    //Driver program 
    int main()
    {
        struct Record s;
        printf("%d\n",sizeof(s));
        return 0;
    }
    

    Output in C:

    0
    

    Output in C++:

    1
  6. Data Hiding: C structures does not allow concept of Data hiding but is permitted in C++ as C++ is an object oriented language whereas C is not.
  7. Access Modifiers: C structures does not have access modifiers as these modifiers are not suppoted by the language. C++ structures can have this concept as it is inbuilt in the language.

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 contribute@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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 21 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.