structure in C Programming
C Programming c programming notes for gate C programming Tutorials gate study material for cse

Structure in C Programming

Structure in C Programming

Structure in C Programming is a collection of variables of different types under the same name.

In simple words, structure in c programming is used to store different types of data elements.

In this tutorial, we will learn about structure in c programming. We will learn the following concepts about structure in C Programming.

  • What are Structures in C with example?
  • How to define structure in C?
  • Accessing Structure members in C.

Declaration and initialization of structure data members are discussed with examples here in this tutorial.

What are Structures in C?

In C Programming Structure is a collection of different types of data elements. The main limitation of the array is that we can store only similar types of data elements in the array.

Consider a situation where we have to store information about name, roll number, and percentage of students.

Since the name is a character type array, the roll number is an integer type and the percentage may be of float type. So here all these three are of different types. In this case, structure is used to store the information of these three data elements.

How to define Structure in C ?

We use the struct keyword to define structure in C Programming. The general syntax for defining a structure is as follows –

struct StructureName
{
dataType member1;
dataType member2; …
};

For example,

struct Employee

{

char name[50];

int empid;

float salary;

};

Here we have created an Employee structure having three data members empid, name, and salary each data member has different types of values empid is a number, salary is in decimal and name is a combination of alphabets.

Creating Structure Variables

In this section, we have discussed How can be create a structure variable in c.

  • When a struct type is declared, no storage or memory is allocated.
  • To allocate memory of a given structure type and work with it, we need to create structure variables.

We can create structure variables in two ways

Method 1 – Inside the main() function

void main()

{

struct Person person1, person2;

}

Method 2 – At the time of defining the structure

struct Person

{

char name[50];

int citNo;

float salary ;

} person1, person2 ;

In both cases, person1 and person2 are variables of structure named as Person.

Initialization of Structure Members

  • Structure members cannot be initialized like other variables inside the structure definition. This is because when a structure is defined, no memory is allocated to the structure’s data members at this point.
  • To initialize a structure’s data member, create a structure variable. This variable can access all the members of the structure and modify their values. Consider the following example which initializes a structure’s members using the structure variables.
  • Structure members can be initialized in the main method within the program using the dot and assignment operator or at run time by accepting the value from the user through the scanf() function in the program.

Accessing Members of Structure in C

Members of Structure are accessed using dot operator. Basic Program for creating Structure and accessing  Structure members

Program – C Program to declare, initialize structure and access the the structure members is given here 

#include <stdio.h>

#include <string.h>

// create struct with person1 variable

struct Person

{

char name[50];

int citNo;

float salary;

} person1;

void main()

{

// assign value to name of person1

strcpy(person1.name, “George Orwell”);

// assign values to other person1 variables

person1.citNo = 1984;

person1. salary = 2500;

// print struct variables

printf(“Name: %s\n”, person1.name);

printf(“Citizenship No.: %d\n”, person1.citNo);

printf(“Salary: %.2f”, person1.salary);

getch();

clrscr();

}

When we execute the above program we get the following output
Output
Name: George Orwell
Citizenship No.: 1984
Salary: 2500.00

Conclusion and Summary

    • In this tutorial, we have discussed the basic concepts of structure such as what is a structure in C Programming , and how to define a structure, how to initialize structure variable variables in c and how can we access the structure members.
    • Structure program in c is also discussed here in this structure in c programming tutorial.


Leave a Reply

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