What is the syntax for defining the data structure in C++?

C++ Programming is basically an enhanced version of C programming language and it is used for “Object-oriented Programming”

  • C++ was developed by Bjarne Stroustrup who did the first development of the language as his first Ph.D. project.

  • He had begun this project because there were no existing programming languages used for large-scale projects.

  • It was initially called as “C with classes”

  • The programming language was first standardized in 1998 and was again issued in 2003,2007 and 2011.

  • C++ is maintained by ISO, a large standard committee.

  • C++ is widely and commonly used in embedded systems and software engineering.

  • C++ has influenced languages like PHP and C-sharp.

Data Structures

Data structures are used in C++ to group and organize the other data objects. Primitive data structures are used to represent simple and basic values and they include data types like Char, Boolean, pointers etc., non-primitive data structures store multiple values in a single variable and these include arrays, stacks, linked lists, and trees.

Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this −

struct [structure tag]{
   member definition;
   member definition; ...
   member definition;
[one or more structure variables];

The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional.

Here is the way you would declare the Book structure:

struct Books {
   char title[50];
   char author[50];
   char subject[100];
   int book_id;
} book;

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started