Difference between C structures and C++ structures



A structures (or struct) is a user-defined data type, which is used to group and store variables of different data types inside a single name. But in C, structures can store only data members whereas C++ can store member functions, constructors, destructors, and even inheritance which is similar to classes in C++, but the only difference between struct and classes is that struct members are public by default.

In this following article we will see how structure differs in both C and C++ in detail.

Structures in C

The structures in C is a user-defined data type, which is used to group variables of different data types under a single name. In C, the struct keyword is used to declare a structure, which contains only data members (not member functions), and the size of an empty structure is 0.

Example

Here is the following example code of structure in C showcasing how only data members are allowed inside a structure (no member functions), and how structure members are initialized manually in the main() function:

#include <stdio.h>
#include <string.h>

// here defining a structure
struct Student {
    // in C, only data members are allowed (no member functions)
    char name[50];
    int age;
    float marks;
};

int main() {
    
    struct Student s1;
    // here cannot initialize structure members directly, we need to assign each member manually
    strcpy(s1.name, "Aman");
    s1.age = 21;
    s1.marks = 87.5;

    // printing the structure data value
    printf("Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

Output

Name: Aman
Age: 21
Marks: 87.50

Structures in C++

The structures in C++ is a user-defined data type, which contain both data members and member functions. It also supports constructors, static members, access specifiers (public, private) and has features like data hiding. In C++, you can directly initialize structure without using the struct keyword for variable declaration. And in this the size of an empty structure is 1 byte.

Example

Here is the following code of structure in C++, here you can assign static members manually or directly, so here we are using a constructor to initialize them directly:

#include <iostream>
using namespace std;

// here defining a structure
struct Student {
    string name;
    int age;

    // using constructor to initialize structure members
    Student(string n, int a) {
        name = n;
        age = a;
    }
};

int main() {
  
    Student s("Aman", 22);
    
    cout << "Name: " << s.name << endl;
    cout << "Age: " << s.age << endl;

    return 0;
}

Output

Name: Aman
Age: 22

Difference between C structures and C++ structures

Here is the following comparison table of structure in C and C++:

C Structure
C++ Structure
Structures in C, can hold only data members not members functions. Structures in C++ can hold both data members and member functions
We cannot initialize the structure members directly in C. We can initialize the structure members directly in C++.
In C, we have to write a 'struct' keyword to declare structure type variables. In C++, we do not need to use a 'struct' keyword for declaring variables.
C structures cannot have static members. C++ structures can have static members.
C structures cannot have a constructor.  C++ structures can have constructor.
C structures do not have access modifiers. C++ structures have access specifiers.
Data hiding feature is not available. Data hiding feature is available.
The sizeof operator will generate 0 for empty structure in C. The sizeof operator will generate 1 for empty structure in C++.
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-11T17:54:19+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements