×

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance of the struct keyword and its necessity to declare the type variables, why C++ supports the access modifiers while C is not supporting it. Some code to see the working of the member functions inside the structure, declaring the Structures accessing them etc.

Table depicting the differences between C and C++ Structures.

C Structures  C++ Structures  
In C Structures, we cannot have static members in it.  In C++ Structures, we can have static members in it.  
In C structures Data handling concept is not possible.  In C++ structures Data handling concept is not possible.  
In C Structures, only the pointers to struct are allowed in it.  In C++ Structures, it can have both the pointers and the references attached to the struct.  
In C Structures, they do not have access modifiers. Before implementing any structure functionality in C structures, it is necessary to use the 'struct' keyword to declare the structure type of variables.  In C++ Structures, it supports the access modifiers. In C++ structures, before we implement any Structure functionality, it is not necessary to use the 'struct' keyword to declare the structure type of variables.  
In C structures, direct Initialization of the data members is not possible.  In C++ structures there is direct Initialization of the data members is possible.  
In C structures, they cannot have a constructor inside a structure  In C++, systems can have a constructor inside a structure  
In C structures, only the data members are allowed; it cannot have the member functions.  In C++ Structures, it can hold both the member functions and the data members.  
In C structures, the data hiding feature is not available.  In C++ structures, the data hiding feature is available.  
In C structures, the sizeof() operator will generate 0 for an open system.In C++ structures the sizeof() operator will generate 1 for empty structure.  

C code to Demonstrate C Structures

Code -1

#include<stdio.h>
struct a{


    int data_1;
    int data_2;
};
int main()
{
    struct a s_data = {1227, 1324,12121,2211,788776,2221,12};
    printf("%d %d", s_data.data_1,s_data.data_2, s_data.data_3, s_data.data_4, s_data.data_5, s_data.data_6, s_data.data_7);
    return 0;
}

Output

1227 1324 12121 2211 788776 2221 12

Code-2

//C code to create a structure 
struct myStruct {
  int mynum;
  char myletter;
};


int main() {


  struct myStruct s1pro;


  s1pro.mynum = 3;
  s1pro.myletter = 'C';


  printf("number: %d\n", s1pro.mynum);
  printf("letter: %c\n", s1pro.myletter);


  return 0;
}

Output

number: 3
letter: C

Code-2

#include <stdio.h>
#include <string.h>
 
struct books {
   char title[50];
   char author[50];
};
 
int main( ) {


   struct books book1;        
   struct books book2;        
 
   strcpy( book1.title, "Coding in Java");
   strcpy( book1.author, "Ali zuha"); 
   
   strcpy( book2.title, "Bill");
   strcpy( book2.author, " zara Ali");
  
   printf( "book 1 title : %s\n", book1.title);
   printf( "book 1 author : %s\n", book1.author);
   
   printf( "book 2 title : %s\n", book2.title);
   printf( "book 2 author : %s\n", book2.author);
   
   return 0;
}

Output

book 1 title : Coding in Java
book 1 author : Ali zuha
book 2 title : Bill
book 2 author :  zara Ali

C++ Code to Demonstrate C++ Structures

Code  

#include <iostream>
using namespace std;
 struct Point {
    int x, y;
};
 
int main(){
    struct Point p1 = { 110, 221 };
    //code for accessing members of the point p1
    p1.x = 40;
    cout << "x = " << p1.x << ", y = " << p1.y;
    return 0;
}

Output

x = 40, y = 221

C++ Code-2

#include <iostream>
using namespace std;
 
struct Point {
    int x, y, a, b, c, d, e, f, g, h;
};
 
int main()
{
    //code to create an array of structures
    struct Point arr[20];
 
    //code to access array members
    arr[0].x = 10;
    arr[0].y = 20;
    arr[0].a = 30;
    arr[0].b = 40;
    arr[0].c = 50;
    arr[0].d = 60;
    arr[0].e = 70;
    arr[0].f = 80;
    arr[0].g = 90;
    arr[0].h = 100;
 
    cout << arr[0].x <<endl;
    cout << arr[0].y <<endl;
    cout << arr[0].a <<endl;
    cout << arr[0].b <<endl;
    cout << arr[0].c <<endl;
    cout << arr[0].d <<endl;
    cout << arr[0].e <<endl;
    cout << arr[0].f <<endl;
    cout << arr[0].g <<endl;
    cout << arr[0].h <<endl;
    
    
    return 0;
}

Output

10
20
30
40
50
60
70
80
90
100

Related Topics

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.