Difference between revisions of "Advance-C/C2/Union-and-Typedef/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
{| Border=1 | {| Border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
|- | |- | ||
Line 12: | Line 11: | ||
| In this tutorial, we will learn about: | | In this tutorial, we will learn about: | ||
− | + | '''typedef keyword''' | |
− | + | '''union keyword''' | |
with the help of some examples. | with the help of some examples. | ||
Line 19: | Line 18: | ||
| 00:17 | | 00:17 | ||
| For this tutorial, I am using: | | For this tutorial, I am using: | ||
− | + | '''Ubuntu''' Operating System version '''11.10'''. | |
− | + | '''gcc compiler''' version '''4.6.1''' on '''Ubuntu'''. | |
|- | |- | ||
Line 32: | Line 31: | ||
|- | |- | ||
| 00:43 | | 00:43 | ||
− | | I will start with an introduction to '''typedef | + | | I will start with an introduction to '''typedef''' keyword. |
|- | |- | ||
Line 60: | Line 59: | ||
|- | |- | ||
| 01:28 | | 01:28 | ||
− | | Note that our filename is ''' | + | | Note that our filename is '''"pallindrome.c"'''. |
|- | |- | ||
| 01:34 | | 01:34 | ||
− | | In this program, we will check whether the given number is a ''' | + | | In this program, we will check whether the given number is a '''palindrome''' or not. |
|- | |- | ||
| 01:41 | | 01:41 | ||
− | | We have given '''alias '''name as 'uint' to the''' unsigned int datatype '''using''' typedef''' keyword. | + | | We have given '''alias '''name as 'uint' to the''' unsigned int datatype ''', using''' typedef''' keyword. |
|- | |- | ||
| 01:52 | | 01:52 | ||
− | | Here we are using | + | | Here we are using 'uint' to '''declare''' the '''variables.''' |
|- | |- | ||
| 01:59 | | 01:59 | ||
− | | This is the logic for ''' | + | | This is the logic for '''palindrome'''. |
|- | |- | ||
Line 92: | Line 91: | ||
|- | |- | ||
| 02:29 | | 02:29 | ||
− | | Type:''' dot slash pallindrome''' | + | | Type: '''dot slash pallindrome''' |
|- | |- | ||
|02:34 | |02:34 | ||
− | | We see: '''"Enter any three digit number"'''. | + | | We see: '''"Enter any three digit number:"'''. |
|- | |- | ||
Line 104: | Line 103: | ||
|- | |- | ||
| 02:42 | | 02:42 | ||
− | | The output is: ''' | + | | The output is: '''Given number is a palindrome number'''. |
|- | |- | ||
| 02:47 | | 02:47 | ||
− | | Now we will learn about '''union | + | | Now we will learn about '''union''' datatype. |
|- | |- | ||
| 02:52 | | 02:52 | ||
− | | ''' | + | | '''union''' is a collection of different datatypes grouped together. |
|- | |- | ||
| 02:57 | | 02:57 | ||
− | | ''' | + | | '''union '''allocates one common storage-space for all its members. |
|- | |- | ||
| 03:03 | | 03:03 | ||
− | | We can access only one member of union at a time. | + | | We can access only one member of '''union''' at a time. |
|- | |- | ||
| 03:08 | | 03:08 | ||
| Syntax1: | | Syntax1: | ||
− | '''union union_name''' within curly brackets '''members;''' after curly brackets '''union_variable''' | + | '''union union_name''' within curly brackets '''members;''' after curly brackets '''union_variable'''and a semicolon. |
− | and a semicolon. | + | |
|- | |- | ||
Line 144: | Line 142: | ||
|- | |- | ||
| 03:47 | | 03:47 | ||
− | | Note that our filename is | + | | Note that our filename is '''union dot c'''. |
|- | |- | ||
| 03:52 | | 03:52 | ||
− | | We have declared '''union''' named '''student | + | | We have declared '''union''' named '''student'''. |
|- | |- | ||
| 03:56 | | 03:56 | ||
− | | Here we have three variables- '''english''', '''maths''' and''' science.''' | + | | Here, we have three variables- '''english''', '''maths''' and''' science.''' |
|- | |- | ||
Line 162: | Line 160: | ||
| Here we can access the '''union''' members using a '''union''' variable: | | Here we can access the '''union''' members using a '''union''' variable: | ||
− | + | '''stud dot english''' | |
− | + | '''stud dot maths''' | |
− | + | '''stud dot science''' | |
|- | |- | ||
Line 172: | Line 170: | ||
|- | |- | ||
| 04:26 | | 04:26 | ||
− | | Let us execute | + | | Let us execute on the '''terminal'''. Type: '''gcc space union dot c space hyphen o space union''' |
Type: '''dot slash union''' | Type: '''dot slash union''' | ||
|- | |- | ||
|04:44 | |04:44 | ||
− | | The output is displayed as: ''' | + | | The output is displayed as: '''Total is 228'''. |
|- | |- | ||
Line 185: | Line 183: | ||
|- | |- | ||
| 04:55 | | 04:55 | ||
− | | ''' | + | | '''union''' allocates a common storage-space for all its members. |
|- | |- | ||
| 05:01 | | 05:01 | ||
− | | ''' | + | | '''structure''' allocates separate storage-space for all its members. |
|- | |- | ||
| 05:07 | | 05:07 | ||
− | | ''' | + | | '''union''' occupies lower '''memory space'''. |
|- | |- | ||
| 05:11 | | 05:11 | ||
− | | ''' | + | | '''structure''' occupies higher memory space. |
|- | |- | ||
Line 206: | Line 204: | ||
|- | |- | ||
| 05:27 | | 05:27 | ||
− | | '''Memory''' allocation for '''union''' variable will be | + | | '''Memory''' allocation for '''union''' variable will be 8 bytes as '''double datatype''' will occupy the maximum '''memory space'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 05:39 | | 05:39 | ||
− | | | + | | Example for '''structure:''' |
'''struct student{int mark;<nowiki>char name[6];</nowiki>double average;};''' | '''struct student{int mark;<nowiki>char name[6];</nowiki>double average;};''' | ||
|- | |- | ||
| 05:48 | | 05:48 | ||
− | | '''Memory''' allocation for '''structure''' variable will be: | + | | '''Memory''' allocation for '''structure''' variable will be: 2bytes+6bytes+8bytes =16bytes. |
|- | |- | ||
Line 230: | Line 226: | ||
| 06:06 | | 06:06 | ||
| In this tutorial, we learnt: | | In this tutorial, we learnt: | ||
− | + | '''typedef ''' | |
− | + | '''union''' and | |
− | + | Difference between '''union''' and '''structure.''' | |
− | + | ||
|- | |- | ||
Line 241: | Line 236: | ||
|- | |- | ||
| 06:17 | | 06:17 | ||
− | | write a program to display records of an employee | + | | write a program to display records of an employee |
|- | |- | ||
| 06:21 | | 06:21 | ||
− | | | + | | like '''name, address, salary'''. |
|- | |- | ||
Line 253: | Line 248: | ||
|- | |- | ||
| 06:29 | | 06:29 | ||
− | | Give an '''alias''' name as '''emp''' using '''typedef.''' | + | | Give an '''alias''' name as "'''emp'''" using '''typedef.''' |
|- | |- | ||
Line 273: | Line 268: | ||
|- | |- | ||
| 06:53 | | 06:53 | ||
− | | | + | | Gives certificates to those who pass an online test. For more details, please write to contact@spoken-tutorial.org |
|- | |- | ||
| 07:04 | | 07:04 | ||
− | | '''Spoken Tutorial''' | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
Line 285: | Line 280: | ||
|- | |- | ||
| 07:16 | | 07:16 | ||
− | | More information on this mission is available at: | + | | More information on this mission is available at the link shown below:http://spoken-tutorial.org\NMEICT-Intro |
− | http://spoken-tutorial.org\NMEICT-Intro | + | |
|- | |- | ||
| 07:22 | | 07:22 | ||
− | | This is Ashwini Patil from IIT Bombay. | + | | This is Ashwini Patil from '''IIT Bombay''', signing off.Thank you for joining. |
− | Thank you for joining. | + | |
|} | |} |
Latest revision as of 15:32, 20 February 2017
Time | Narration |
00:01 | Welcome to the Spoken Tutorial on Typedef and Union in C. |
00:07 | In this tutorial, we will learn about:
typedef keyword union keyword with the help of some examples. |
00:17 | For this tutorial, I am using:
Ubuntu Operating System version 11.10. gcc compiler version 4.6.1 on Ubuntu. |
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_variableand 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:
stud dot english stud dot maths stud dot science |
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 | Example 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:
typedef union and Difference between union and structure. |
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 the link shown below:http://spoken-tutorial.org\NMEICT-Intro |
07:22 | This is Ashwini Patil from IIT Bombay, signing off.Thank you for joining. |