Home Theory The keyword ‘struct’ is optional in C++

The keyword ‘struct’ is optional in C++

by nikoo28
0 comment

In C, struct keyword must be used for declaring structure variables, but it is optional in C++.

For example, following program gives error in C and works in C++.

struct node
{
   int x;
   node *next; // Error in C, struct must be there. Works in C++
};
 
int main()
{
   node a;  // Error in C, struct must be there. Works in C++
}

And following program works in both C and C++.

struct node
{
   int x;
   struct node *next;  // Works in both C and C++
};
 
int main()
{
   struct node a;  // Works in both C and C++
}
0 comment

You may also like

Enclose codes in [code lang="JAVA"] [/code] tags

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More