ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

Structure Basics in Programming :: C++ Programming Structure Basics

Updated on November 2, 2008


  • Computer
  • Compiler
  • Programmer
  • Basic knowledge of programming


Collection of heterogeneous data types are known as Structure.

Structure is one of the data types of C/C++ programming languages. Several data types are grouped together in structure.The basics of structure and the initialization process will be discussed in this part.

What is Structure / Structure Programming in C/C++ language?

In C/C++ programming structure can be used. Its main goal is to group several heterogeneous data types. It can be stated as: 

  • collection of heterogeneous data types which are grouped together
  • referred as member: single unit of structure
  • each member specified by: variable name + period (.) + member name
  • definition specified by → "struct" keyword every structure  
  • block definition is finished by a semicolon (;)

Example of structure (Very Simple):

We want to build a structure based on company's employee information. Now for this the definition of structure will be: 

/* definition of structure & members declaration */
struct company{
	long int employee_id;
        int age;
	char sex;
	float salary;
/* definition of object of the following structure */
struct company employee;

General format of a structure

storage_class struct user_defined_name{
	data_type  member 1;
        data_type  member 2;
	data_type  member n;

Graphical representation

Graphical representation of structure
Graphical representation of structure

Symbolic representation

Symbolic representation of structure
Symbolic representation of structure

Representation of a structure:

(a) Graphical representation

(b) Symbolic representation  

Initialization of structure

Can be done by 2 ways:

  1. Static initialization
  2. External initialization

(a) Static initialization

All the member values are assigned here.

Static initialization :: Example

A structure contains the information of employee_id, age, sex and salary. So, the structure looks like the followings:

struct company{
     long int employee_id;
     int age;
     char sex;
     float salary;

Here "employee" is the variable tag. Initialization can be done one of the following ways:

employee.employee_id = 1000;
employee.age = 30; = 'M';
employee.salary = 30000;


struct company employee2 = {1000, 32, 'F', 20000};

Output of the program

Output of the program
Output of the program


If any member is not initialized then that member is assigned with 0 values. For example, if the initialization is:

struct company employee = {1000};

Then, the members are initialized as follows:

employee.employee_id = 1000;
employee.age = 0; = 0;
employee.salary = 0;

Output of the above code segment

Output of the above program
Output of the above program

(b) External initialization

For this case, the entire member's values are assigned by the user. For the above structure, the values are collected from the user by the following way:

	cout << "ID     :"; cin >> employee.employee_id;
	cout << "Age    :"; cin >> employee.age;
	cout << "Sex    :"; cin >>;
	cout << "Salary :"; cin >> employee.salary;

Output of the above code segment

Output of external initializations above program
Output of external initializations above program

Structue in C/C++ programming

Is this tutorial is helpful for building your basics?

See results


    0 of 8192 characters used
    Post Comment

    • profile image

      NH 5 years ago


    • profile image

      shubhkaran 6 years ago

      Selection sort Merge Sort insertion delation addition in programming in c++ data structure

    • profile image

      mohammad kamran  6 years ago

      yes, it is very helpful in learning structures

    • profile image

      Irfan Ahmad 7 years ago

      Its resly nice,it solve my problems.


    • profile image

      Urmale 7 years ago

      I found it an interesting tutorial.


    • profile image

      kibreab 7 years ago

      It is a very nice tutorial that build my basics.

      thanks in advance