STRUCTURE
STRUCTURE
STRUCTURES
OUTPUT
#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;
} var2;
EXAMPLE PROGRAM FOR NESTED STRUCTURE
#include<stdio.h>
struct student // OUTER STRUCTURE
{
struct person // INNER STRUCTURE
{
char name[20];
INNER STRUCTURE
int age; MEMBERS
char dob[10];
}info; //INNER STRUCTURE VARIABLE
int roll_no;
float marks; OUTER STRUCTURE
MEMBERS
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
void main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM – PASSING
STRUCTURE TO FUNCTION IN C BY
ADDRESS:
In this program, the whole structure is passed
to another function by address.
It means only the address of the structure is
passed to another function.
The whole structure is not passed to another
function with all members and their values.
So, this structure can be accessed from called
function by its address.
EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
Void main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
}
void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM TO DECLARE A
STRUCTURE VARIABLE AS GLOBAL IN C:
Structure variables also can be declared as
global variables as we declare other variables in
C.
So, When a structure variable is declared as
global, then it is visible to all the functions in a
program.
In this scenario, we don’t need to pass the
structure to any function separately.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
UNION
C Union
Like structure, Union in c language is a user-defined data type that is used to store
the different type of elements.
At once, only one member of the union can occupy the memory.
In other words, we can say that the size of the union in any instance is equal to the
size of its largest element.
SYNTAX FOR UNION:
union
union_name
{
data_type member1;
data_type member2;
data_type memeberN;
};
Example program for C union:
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
// assigning values to record1 union variable
strcpy(record1.name, "Raju");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
printf("Union record1 values example\n");
printf(" Name : %s \n", record1.name);
printf(" Subject : %s \n", record1.subject);
printf(" Percentage : %f \n\n", record1.percent);
// assigning values to record2 union variable
printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name); Output:
strcpy(record2.subject, "Physics"); Union record1 values example
printf(" Subject : %s \n", record2.subject); Name :
record2.percentage = 99.50; Subject :
printf(" Percentage : %f \n", record2.percentage); Percentage : 86.500000;
return 0; Union record2 values example
} Name : Mani
Subject : Physics
Percentage : 99.500000
THANK YOU