0% found this document useful (0 votes)
24 views3 pages

Day16 (Structure and Union)

A structure in C allows grouping of different data types together under one name. It defines a new user-defined data type. To create a structure, the struct keyword is used followed by the structure name and a list of member variables of different data types. Variables of a structure data type can then be declared, and members accessed using the dot operator. For example, a structure called StudentData is defined to store the name, id and age of a student. A variable of this type is declared and members assigned values and printed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Day16 (Structure and Union)

A structure in C allows grouping of different data types together under one name. It defines a new user-defined data type. To create a structure, the struct keyword is used followed by the structure name and a list of member variables of different data types. Variables of a structure data type can then be declared, and members accessed using the dot operator. For example, a structure called StudentData is defined to store the name, id and age of a student. A variable of this type is declared and members assigned values and printed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Structure and Union

Definition of Structure
Array is a collection of same type of elements but in many real life applications we
may need to group different types of logically related data. For example, if we want
to create a record of a person that contains name, age, and height of that person, then
we can’t use array because all three data elements are of different types.

How to create a structure in C Programming


We use struct keyword to create a structure in C. The struct keyword is a short
form of structured data type.
struct struct_name
{
DataType member1_name;
DataType member2_name;
DataType member3_name;

};
Here struct_name can be anything of your choice. Member data type can be same or
different. Once we have declared the structure we can use the struct name as a data
type like int, float etc.
First we will see the syntax of creating struct variable, accessing struct members etc
and then we will see a complete example.

How to declare variable of a structure?


struct struct_name var_name;
or
struct struct_name
{
DataType member1_name;
DataType member2_name;
DataType member3_name;

} var_name;

How to access data members of a structure using a struct variable?


var_name.member1_name;
var_name.member2_name;

How to assign values to structure members?
There are three ways to do this.
1) Using Dot(.) operator
var_name.memeber_name = value;
2) All members assigned in one statement
struct struct_name var_name =
{value for memeber1, value for memeber2 …so on for all the members}
3) Designated initializers – We will discuss this later at the end of this post.
Example of Structure in C
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;

/*Assigning the values of each struct member here*/


student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;

/* Displaying the values of struct members */


printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}
Output:
Student Name is: Steve
Student Id is: 1234
Student Age is: 30
Example-2
/*Program to define a structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
struct student
{
char name[20];
int rollno;
float marks;
};
struct student s;
printf("Enter name:\t");
scanf("%s",&s.name);
printf("Enter rollno:\t");
scanf("%d",&s.rollno);
printf("Enter marks:\t");
scanf("%f",&s.marks);
printf("Name=%s Rollno=%d Marks=%f \n",s.name,s.rollno,s.marks);
getch ();

You might also like