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
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.
why there is i+1 in the for loop??
kindly tell me plz
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”
You are correct Ammy. That is the reason why I used “i+1”. Starting from “Animal 1” makes more sense than “Animal 0”
@ Unknown :-
Bcoz if i+1 is not assigned it will ask in output :-
Enter the age of animal 0
Hope it helped 🙂
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
what do you mean by “clrscr()”
clrscr() simply clears the output screen
@ 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…??
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
Good