UNIT 4 Part-2
UNIT 4 Part-2
UNIT 4 Part-2
void main(){
struct Student stud_2; // using struct keyword
printf("Enter details of stud_1 : \n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);
Unions in C
In C programming language, the union is a collection of elements of the different data type.
The union is used to create user-defined data type in the C programming language. As the union
used to create a user-defined data type, the union is also said to be “user-defined data type in
C”.
In other words, the union is a collection of non-homogeneous elements. Using union we can
define new data types called user-defined data types that holds multiple values of the different
data type. The formal definition of a union is as follows...
Definition:
Union is a colloction of different type of elements under a single name that acts as user defined
data type in C.
Generally, unions are used to define a record in the c programming language. Unions allow us
to combine elements of a different data type into a group. The elements that are defined in a
union are called members of union.
How to create union?
To create union in c, we use the keyword called "union". We use the following syntax to create
unions in c programming language.
union <union_name>
{
data_type member1;
data_type member2, member3;
.
.
};
Following is the example of creating a union called Student which is used to hold student
record.
Creating union in C
union Student
{
char stud_name[30];
int roll_number;
float percentage;
};
void main(){
union Student stud_2; // using struct keyword
printf("Enter details of stud_1 : \n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);
typedef in C
In C programming language, typedef is a keyword used to create alias name for the existing
datatypes. Using typedef keyword we can create a temporary name to the system defined
datatypes like int, float, char and double. we use that temporary name to create a variable. The
general syntax of typedef is as follows...
typedef <existing-datatype> <alias-name>;
typedef with primitive datatypes
Consider the following example of typedef
typedef int Number
In the above example, Number is defined as alias name for integer datatype. So, we can use
Number to declare integer variables.
void main(){
clrscr() ;
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &a,&b) ;
c = a + b;
printf("Sum = %d", c) ;
}
In C programming language, typedef is also used with arrays. Consider the following
example program to understand how typedef is used with arrays.
#include<stdio.h>
#include<conio.h>
void main(){
typedef int Array[5]; // Here Array acts like an integer array type of size 5.
clrscr() ;
void main(){
stud s1;
clrscr() ;
printf("Enter the student name: ") ;
scanf("%s", s1.stud_name);
printf("Enter the student Roll Number: ");
scanf("%d", &s1.stud_rollNo);
printf("\nStudent Information\n");
printf("Name - %s\nHallticket Number - %d", s1.stud_name, s1.stud_rollNo);
}
In the above example program, stud is the alias name of student structure. We can use stud as
datatype to create variables of student structure. Here, s1 is a variable of student structure
datatype.