Structures in C Programming Language

Structure is a special type of C data type. Structures contains a number of data types grouped together. Structures in C is one of the excellent functionality provided in C. Structures makes C Programming language easy and simpler up to certain extent. Structure is most widely used in Programming.

Structures in C allows multiple data types to be grouped together. As a programmer I have used structures in C a lot and find this feature interesting too. Just go through this article and you too will find structures in C interesting.

Let us suppose you want to store basic information about animal such as age, name, gender and birth place. There are two approaches that can be followed.
1) Use one array each to store properties of animal as shown below.

int age[10];
char name[10][20] ;
char gender[10];
char bPlace[10][20];

This approach is very tedious as number of characteristics related to animal might increase. Also, we need to keep the track of index of each and every variable. Hence, it becomes very difficult to handle such C programs.

2) Second approach is using structures in C.
At the end of this article you will understand the ease of using structures and its importance.
Declaring a Structure in C:
Structure in C is declared using the keyword ‘struct’.

Syntax:

struct struct-name
{
    Element 1;
    Element 2;
    Element 3;
    .
    .
    .
    Element n;
};

Please don’t forget to include semicolon (;) at the end of structure declaration as shown above.

For example, we can declare structure as follows:

struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};

Here,
animal is the name given to structure.
age, name, gender and bPlace are the elements of the structure animal. We can call it as properties of the structure animal.

Declaring Structure variables:
Once the new structure data type has been defined, we can declare one or more variables of that type as shown below:

struct animal a1, a2;

This statement set aside space in memory.

Following are the ways in which we can declare structure variables.
1)

struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
}a1,a2;

2)

struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1, a2;

3)

struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1={12,”Sam”,’M’,”North America”};
struct animal a2={18,”Maxie”,’M’,”Amsterdam”};

Accessing structure Elements:
Now is the time to access structure elements after declaration of structure type and structure variables.

Syntax:

Consider,

struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a1,a2;

Now, element ‘age’ for the variable ‘a1’ can be accessed using the statement,

a1.age;

Similarly, element ‘name’ for the variable ‘a1’ can be accessed using the statement,

a1.name;

Array of Structures:
We can create array of structure variables as follows:

struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a[100];

The above declaration can store information of not more than 100 animals.
The values can be accessed as follows:

a[0].age =10;

The above statement will assign value 10 to the element ‘age’ of structure variable a[0].
Similarly,
We can assign values to the elements of other structure variables too.

a[1].age=12;

We can print the values as follows:

printf(“%d”,a[1].age);

The above statement will display 12.

Also See:
Program to demonstrate Structures in C Programming Language

You may also like...

32 Responses

  1. Anonymous says:

    what is the file concept in c..?

  2. Anonymous says:

    File Concept in C has not been explained here .

  3. LearnCOnline says:

    Sure… We are working on the same. Will publish the article on File concepts soon. Subscribe to our posts so that you are updated. Thanks.

  4. maheshwar says:

    sir,what is ment by data type,lenear and binary search .do u have data structure tutorial in online

  5. Hafeez Abdul says:

    y r u using * in name and bplace???

  6. Anonymous says:

    Strings in C are handled in 2 ways:
    1. Char pointer
    2. Character array

    If we have used char array, we need to define the array size; this would cause problem if the size of string exceeds the array size. So, pointers are always preferred in this case,as they are pointing to the string address where length of string is not an issue.

  7. Anonymous says:

    i want briefly explanation about struture.

  8. Anonymous says:

    Respected sir or madam, plz give examples of programs where really structures are useful .Also plz tell about the input files and output files.

  9. ASRAAR AHMAD says:

    It is really helpful !

    Thanks !

  10. Anonymous says:

    good one

  11. Aakash says:

    * is nothing but a pointer to character here..ie.it is used to contain the address of a variable of character data type.We can use array of characters ..but with memory specification,pointer is more useful..

  12. Anonymous says:

    its a gud one..thnks 4 ur help

  13. Anonymous says:

    Waaao…. very worthy

  14. LearnCOnline says:

    Thank you everyone for the wonderful comments. I hope this website is helping every one out there.

    If you like this website, kindly share it with everyone.

    Thanks,
    LearnCOnline Team

  15. good .. . .

    thank for it . . .

  16. Anonymous says:

    very gud one guys………..

  17. Anonymous says:

    sir u missed sme of the topics lik union and enum .can i get them??

  18. Anonymous says:

    thns for the helpful questions and answers

  19. Anonymous says:

    Nice

  20. Write a program to find out the sum of two integers using structure….pls answer this question pls

  21. Anonymous says:

    oh it was very helpul to meh tnx a lot…

  22. alekhya rupa says:

    why to use semicolumn after struct ?

  23. LearnCOnline says:

    Hello,

    That is the syntax that needs to be followed. It is defined that way. We can’t redefine the syntax. 🙂

    Thanks,
    LearnCOnline Team

    To Learn C++ Online visit http://www.learncpponline.com

  24. luket says:

    Thanks for the awesome guide. However it would be good if you include the file and typedef concept.

  25. Anonymous says:

    Would you be able to provide some guidance on putting structs as members of other structs (structs within structs)?

  26. Chinna Sankar says:

    Including the concepts like adding the typedef, file will elaborate this guide, anyways good for beginners.

  27. manikantjha says:

    i want to ask you that why an empty structure allocates 1 byte of memory

  28. sehrish says:

    Sir,
    What is switch and break statements.I need full detail.How they work and how they are used

  29. Mohamed says:

    i want to know HISTORY of structure c programming language

  30. Akash says:

    Input-string

    Output-srn tig

  31. Akash says:

    How this output we get?

Leave a Reply

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

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet