Demo on Structures in C

The Structure is a special type of C data type. A structure contains a number of data types grouped together. Structure in C allows multiple data types to be grouped together.
The below mentioned program is to demonstrate structures in C Programming Language.

#include
#include
void main(){
    struct animal{
        int age;
        char name[10];
        char gender;
    }a[10];
    
    int no,i;
    clrscr();
    
    printf("Enter the number of animals: ");
    scanf("%d",&no);
    
    for(i=0;i<no;i++){
        printf("\nEnter the age of animal %d: ", i+1);
        scanf("%d",&a[i].age);
        printf("\nEnter the name of animal %d: ",i+1);
        scanf("%s",&a[i].name);
        printf("\nEnter the gender(M/F) of animal %d: ",i+1);
        scanf("%s",&a[i].gender);
    }
    
    for(i=0;i<no;i++){
        printf("\n%d",a[i].age);
        printf("\t%s",a[i].name);
        printf("\t%c",a[i].gender);
    }
    
    getch();
}

Output:

Enter the number of animals: 2

Enter the age of animal 1: 06

Enter the name of animal 1: Sam

Enter the gender(M/F) of animal 1: M

Enter the age of animal 2: 05

Enter the name of animal 2: Maxie

Enter the gender(M/F) of animal 2: F

6       Sam     M
5       Maxie   F

Also See:
Structures in C Programming Language

You may also like...

12 Responses

  1. Ankit says:

    oh woh i am very happy to see this site. as i wanted than i see in this. thanks for that person who created this site.

  2. Unknown says:

    why there is i+1 in the for loop??
    kindly tell me plz

    • Ammy says:

      the value of variable i is initialized to 0.
      if there would have been i instead of i+1 the printed statement would be
      “Enter the age of animal 0”

  3. Manoj Rathod says:

    @ Unknown :-

    Bcoz if i+1 is not assigned it will ask in output :-

    Enter the age of animal 0

    Hope it helped 🙂

  4. Anonymous says:

    to clear understand

    #include
    void main()
    {
    int i;
    for(i=0;i<5;i++)
    {
    printf(“\a%d”,i+1);
    }
    return 0;
    }
    see what heppen, where \a for beep allert..
    becoz;
    i=0
    after a loop
    i++ as i=1
    again
    i++ as i=1+1 2
    again
    i++ as i=1+1+1 3
    again
    i++ as i=1+1+1+1 4
    and so on up to 5

  5. KIFA AKIFF says:

    what do you mean by “clrscr()”

  6. LearnCOnline says:

    clrscr() simply clears the output screen

  7. Anonymous says:

    @ KIFA AKIFF…we use clrscr(); to clear the screen so that there’s no scrolling in cmd console……but if the next step of program is lengthy then there will be scrollin again…….. my question is …..why you used conio.h instead of string.h…..what is its purpose…??

  8. Anonymous says:

    1 more thing to ask…u said to include array and used strings in the demo…..if i enter a name its ok….but when i enter full name…with spaces..it just skips to program stop…..string.h is not included….y

  1. March 1, 2014

    […] Also See: Program to demonstrate Structures in C Programming Language […]

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