Advance-C/C2/Union-and-Typedef/English-timed
From Script | Spoken-Tutorial
Revision as of 17:42, 4 December 2015 by Sandhya.np14 (Talk | contribs)
|
|
---|---|
00:01 | Welcome to the Spoken Tutorial on Typedef and Union in C. |
00:07 | In this tutorial, we will learn about:
with the help of some examples. |
00:17 | For this tutorial, I am using:
|
00:29 | To follow this tutorial, you should be familiar with 'C' tutorials. |
00:36 | If not, for relevant tutorials, please visit our website which is as shown. |
00:43 | I will start with an introduction to typedef keyword. |
00:49 | Typedef keyword is used to give symbolic name to an existing type or user-defined datatypes. |
00:58 | It is a way to define alias to the commands. |
01:03 | It helps to provide clarity to the code. |
01:07 | It makes the code easier to understand and change. |
01:12 | Syntax: typedef existing_name alias_name. Example: typedef unsigned int uint; |
01:24 | Let us go through an example code. |
01:28 | Note that our filename is 'pallindrome.c'. |
01:34 | In this program, we will check whether the given number is a palindrome or not. |
01:41 | We have given alias name as 'uint' to the unsigned int datatype using typedef keyword. |
01:52 | Here we are using uint to declare the variables. |
01:59 | This is the logic for palindrome. |
02:03 | Now, let us execute the program. |
02:06 | Open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard. |
02:16 | Type: gcc space pallindrome dot c space hyphen o space pallindrome. Press Enter. |
02:29 | Type: dot slash pallindrome |
02:34 | We see: "Enter any three digit number". |
02:38 | I will enter 121. |
02:42 | The output is: "Given number is a palindrome number". |
02:47 | Now we will learn about union datatype. |
02:52 | Union is a collection of different datatypes grouped together. |
02:57 | Union allocates one common storage space for all its members. |
03:03 | We can access only one member of union at a time. |
03:08 | Syntax1:
union union_name within curly brackets members; after curly brackets union_variable and a semicolon. |
03:21 | We also have an alternate syntax.
Syntax 2: union union_name within curly brackets members; after curly brackets semicolon union union_name union_variable; |
03:39 | Let us see an example. |
03:41 | I have a code file; let us go through it. |
03:47 | Note that our filename is "union dot c". |
03:52 | We have declared union named student. |
03:56 | Here, we have three variables- english, maths and science. |
04:02 | In main() function, we have declared a union variable as stud. |
04:09 | Here we can access the union members using a union variable:
|
04:21 | Then we calculate the total marks and display it. |
04:26 | Let us execute. On the terminal, type: gcc space union dot c space hyphen o space union
Type: dot slash union |
04:44 | The output is displayed as: "Total is 228". |
04:50 | Let us see the difference between structure and union. |
04:55 | Union allocates a common storage space for all its members. |
05:01 | Structure allocates separate storage space for all its members. |
05:07 | Union occupies lower memory space. |
05:11 | Structure occupies higher memory space. |
05:14 | Example for union:
union student{int marks;char name[6];double average;}; |
05:27 | Memory allocation for union variable will be 8 bytes.
As double datatype will occupy the maximum memory space. |
05:39 | Exampls for structure:
struct student{int mark;char name[6];double average;}; |
05:48 | Memory allocation for structure variable will be: 2bytes+6bytes+8bytes =16bytes. |
06:00 | This brings us to the end of this tutorial. |
06:04 | Let us summarize. |
06:06 | In this tutorial, we learnt:
|
06:14 | As an assignment, |
06:17 | write a program to display records of an employee. |
06:21 | Like name, address, salary. |
06:25 | Define a union named employee. |
06:29 | Give an alias name as emp using typedef. |
06:35 | Watch the video available at the link shown below. |
06:39 | It summarizes the Spoken Tutorial project. |
06:42 | If you do not have good bandwidth, you can download and watch it. |
06:47 | The Spoken Tutorial project team: * Conducts workshops using spoken tutorials. |
06:53 | * Gives certificates to those who pass an online test. For more details, please write to contact@spoken-tutorial.org |
07:04 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
07:08 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
07:16 | More information on this mission is available at: |
07:22 | This is Ashwini Patil from IIT Bombay.
Thank you for joining. |