Structure in C
In C, a structure is a collection of different data types that are grouped together and each element in a structure is called as a member. Data types can be int, char, float, double and long double and etc.
- In order to access structure members in C, the structure variable should be declared.
- Structure variables can be declared for same structure and the memory will be allocated for each variable separately.
- Initialize a structure to null while declaring, when if we don’t assign any values to structure members.
Syntax:
struct structure_name {
data_type member 1;
data_type member 2;
.
.
data_type member n;
} [one or more structure variables];
Example:
#include <stdio.h>
#include <string.h>
struct student
{ int id;
char name[50];
int mark1,mark2;
}s1,s2,s3;
void main( )
{
s1.id=101;
strcpy(s1.name, "Arunkumar");
s1.mark1=89;
s1.mark2=58;
s2.id=105;
strcpy(s2.name, "Manivannan");
s2.mark1=69;
s2.mark2=88;
s3.id=107;
strcpy(s3.name, "Deepalakshmi");
s3.mark1=98;
s3.mark2=78;
printf( "Student 1 id : %d\n", s1.id);
printf( "Student 1 name : %s\n", s1.name);
printf( "Student 1 mark1 : %d\n", s1.mark1);
printf( "Student 1 mark2 : %d\n", s1.mark2);
printf( "Student 2 id : %d\n", s2.id);
printf( "Student 2 name : %s\n", s2.name);
printf( "Student 2 mark1 : %d\n", s2.mark1);
printf( "Student 2 mark2 : %d\n", s2.mark2);
printf( "Student 3 id : %d\n", s3.id);
printf( "Student 3 name : %s\n", s3.name);
printf( "Student 3 mark1 : %d\n", s3.mark1);
printf( "Student 3 mark2 : %d\n", s3.mark2);
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com