C – Structure

After reading this C structure topic, you will understand its syntax and also you will able to implement it in C programming.

In real life, a publisher who prints the book of various authors and generate the list of books in the order of the title of the book, so to deal such types of real-life problems, C language provides structure facility.

A structure is a collection of one or more variables of different data types under a single name.

Structure Declaration

General Form (Syntax):

The syntax of the structure declaration statement is given below,

struct name_of_structure
{
data_type member1_name;
data_type member2_name;
.
.
data_type member_name;
};
struct name_of_structure variable1, variable2,. .;

Where the struct keyword at the beginning used before writing the structure name. The members of the structure along with datatype written inside the curly brackets (i.e. {}). The member data type may be the string, integer, character, etc. and these members also called as elements of the structure. The variable1, variable2, … are the structure variables.

For example:

struct admission
{
char name[10];
int age;
char stauts;
} stud1, stud2;

Here the name of the structure is admission and name[10], age, status are the members of the structure. Structure variables are stud1 and stud2.

Example:

Program (1): To demonstrate structure declaration in C.

#include<stdio.h>
#include<conio.h>
void main(void)
{
struct language
{
char name;
int year;
};
struct language dr,gv;
dr.name='C';
gv.name='p';
dr.year=1972;
gv.year=1991;
printf("%c %d\n %c %d\n",dr.name,dr.year,gv.name,gv.year);
getch();
}

Output (1)

C 1972
 p 1991

How to access/give value to structure members?

The period operator (.) is used for accessing structure members. This period operator sometimes called as member operator or dot operator. The general form for accessing structure members is given below

structure_variable_name.member_name

For example:

Let us declare structure first:

struct date
{
int day;
int month;
int year;
};
struct date today;

If we want to set new date like 12-3-2018 in the structure variable name today so use period operator (.) as shown below:

today.day=12;
today.month=3;
today.year=2018;

Example:

Program (1): To demonstrate how to access/give value to structure members in C.

#include<stdio.h>
#include<conio.h>
void main(void)
{
struct language
{
char name;
int year;
};
struct language dr;
dr.name='C';
dr.year=1972;
printf("%c %d\n",dr.name,dr.year);
getch();
}

Output (1)

C 1972

Structure Initialization

Generally, the initialization of the structure variable is done after the structure declaration. Just after structure declaration put the braces (i.e. {}) and inside it an equal sign (=) followed by the values must be in the order of members specified also each value must be separated by commas. The example below will show how to initialize structure variable in C programming.

Example:

Program (1): To demonstrate initialization of structure in C.

#include<stdio.h>
#include<conio.h>
void main(void)
{
struct language
{
char name;
int year;
};
struct language dr={'C',1972};
printf("%c %d\n",dr.name,dr.year);
getch();
}

Output (1)

C 1972

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *