C Structures

Structures in C allow you to store data of different types. You can easily store variables of types, such as int, float, char, etc. For example, storing the details of employees, with the following members:

  • name: string type
  • age: int type
  • zone: string type
  • salary: float type

Now, you would be wondering about the difference between structure and arrays. Well, Arrays stores data of similar types, for example, an array of ints, whereas Structure stores data of different types.

Define a Structure

Let us understand the above concept with an example to define a structure. To define a structure, use the struct keyword:

Above,

  • The name of the structure is emp,
  • The structure members or fields are name, age, zone, and

Declare a Structure

We defined the structure above. Let us now see how to declare. Following is an example to declare a structure while defining it:

Declare a structure while defining it

The e1, e2, and e3 above are variables. We can use them to access the values.

Declare a structure with struct

We can also declare the variables e1, e2, e3 using the struct keyword. First, define it:

Declare e1, e2, and e3 of type emp:

Access Structure Members

To access a structure member in C language, use a period i.e. the member access operator. Following is the syntax:

For example, access the structure members for variable e1 i.e. the 1st employee:

Let us see an example of accessing structure members in C language:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C Recursion
C - Math Functions
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading