Advance-C/C2/Union-and-Typedef/English
Title of script: Typedef and Union in C
Author: Ashwini Patil
Keywords: Video tutorial, typedef, union, difference between structure & union
|
|
---|---|
Display Slide 1 | Welcome to the Spoken Tutorial on Typedef and Union in C. |
Display Slide 2 | In this tutorial, we will learn about
|
Display Slide 3
System Requirements |
For this tutorial I am using
|
Display Slide 4
Prerequisites
|
To follow this tutorial, you should be familiar with C tutorials.
|
I will start with an introduction to typedef keyword. | |
Display Slide 5
Introduction
|
|
Display Slide 6
Syntax: typedef existing_name alias_name ex: typedef unsigned int uint; |
Syntax:
typedef existing_name alias_name Example: typedef unsigned int uint; |
Let us go through an example code. | |
Point to the filename. | Note that our filename is pallindrome.c
In this program, we will check whether the given number is a pallindrome or not. |
typedef unsigned int uint; | We have given alias name as uint to the unsigned int datatype using typedef keyword. |
uint n, n1;
uint rev = 0; uint rem; |
Here we are using uint to declare the variables. |
Point to the logic. | This is the logic for pallindrome. |
Now let us execute the program. | |
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys. |
Compile
gcc pallindrome.c -o pallindrome |
Type: gcc pallindrome.c -o pallindrome |
Execute
Type: ./pallindrome |
Type: ./pallindrome |
121 | We see:
Enter any three digit number I will enter 121 The output is: Given number is a palindrome number |
Display Slide 7 | Now we will learn about union datatype.
Union is a collection of different datatypes grouped together. Union allocates one common storage space for all its members We can access only one member of union at a time. |
Display Slide 8-9 | Syntax1:
union union_name { members; members; } union_variable; We also have an alternate syntax. Syntax 2:
{ members; members; };
|
Let us see an example.
I have a code file let us go through it. Note that our filename is union.c
| |
union student | We have declared union named student. |
int eng;
int maths; int science; |
Here we have three variables eng, maths and science. |
union student stud; | In main function, we have declared a union variable as stud. |
stud.eng = 75;
stud.maths = 70; stud.science = 65; |
Here we can access the union members using union variable. |
total = stud.eng + stud.maths + stud.science; | Then we calculate the total marks and display it. |
gcc union.c -o union
./union |
Let us execute
On the terminal type, gcc union.c -o union ./union |
Output | The output is displayed as:
Total is 228 |
Let us see the difference between structure and union | |
Slide display 10-11-12 | Union allocates a common storage space for all its members.
Structure allocates separate storage space for all its members.
Structure occupies higher memory space.
union student{int marks;char name[6];double average;}; Memory allocation for union variable will be 8 bytes. As double data type will occupy the maximum memory space.
struct student{int mark;char name[6];double average;};
2bytes+6bytes+8bytes =16bytes. |
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 13
Summary |
In this tutorial we learnt,
|
Display Slide 14 | As an assignment,
Write a program to display records of an employee. Like name, address, salary. Define a union named employee. Give an alias name as emp using typedef. |
Display Slide 15
|
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Display Slide 16
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Display Slide 15
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay.
Thank you for joining. |