Difference between revisions of "Advance-C/C2/Union-and-Typedef/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:
  
 
* '''typedef keyword'''
 
* '''typedef keyword'''
* '''union '''with the help of examples.
+
* '''union keyword'''  
 
+
* with the help of some examples.
 
+
  
 
|-
 
|-
Line 30: Line 29:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
  
* '''Ubuntu Operating System version 11.10'''
+
* '''Ubuntu''' Operating System version 11.10
* '''gcc Compiler version 4.6.1 on Ubuntu'''
+
* '''gcc compiler''' version 4.6.1 on '''Ubuntu'''
 
+
 
+
  
 
|-
 
|-
Line 42: Line 39:
  
 
'''spoken-tutorial.org '''
 
'''spoken-tutorial.org '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial you should be familiar with C tutorials.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial, you should be familiar with C tutorials.
  
  
If not, for relevant tutorials please visit''' spoken hyphen tutorial dot org.'''
+
If not, for relevant tutorials please visit our website which is as shown.
  
 
|-
 
|-
Line 58: Line 55:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * '''Typedef '''keyword is used to give symbolic name to an existing type or user defined datatypes.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
* It is a way to define alias to the commands.
+
* '''Typedef '''keyword is used to give symbolic name to an existing '''type''' or '''user-defined datatypes'''.
 +
* It is a way to define '''alias''' to the commands.
 
* It helps to provide clarity to the code.
 
* It helps to provide clarity to the code.
 
* It makes the code easier to understand and change.  
 
* It makes the code easier to understand and change.  
 
 
  
 
|-
 
|-
Line 79: Line 75:
 
'''typedef existing_name alias_name'''
 
'''typedef existing_name alias_name'''
  
ex:
+
Example:
  
 
'''typedef unsigned int uint;'''
 
'''typedef unsigned int uint;'''
 
 
'''typedef''' is the '''keyword'''.
 
 
'''unsigned int''' is the '''datatype.'''
 
 
'''uint''' is the alias name given for '''unsigned int.'''
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through an example code.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through an example code.
 
 
 
  
 
|-
 
|-
Line 101: Line 87:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''pallindrome.c'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''pallindrome.c'''
  
In this program, we will check whether the given number is a pallindrome or not.
+
In this program, we will check whether the given number is a '''pallindrome''' or not.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| typedef unsigned int uint;  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| typedef unsigned int uint;  
  
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have given '''alias '''name as '''uint '''to the''' unsigned int datatype '''using''' typedef''' keyword.
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have given '''alias '''name as '''uint '''to the''' unsigned int datatype '''using''' typedef.'''
+
  
 
|-
 
|-
Line 120: Line 104:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the logic.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the logic.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the logic for pallindrome.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the logic for '''pallindrome'''.
  
 
|-
 
|-
Line 128: Line 112:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the terminal by pressing '''Ctrl+Alt+T''' keys.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 136: Line 120:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc pallindrome.c -o pallindrome'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc pallindrome.c -o pallindrome'''
  
 
+
Press '''Enter'''.
 
+
 
+
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Execute'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Execute'''
Line 161: Line 143:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will learn about '''union datatype.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will learn about '''union datatype.'''
  
'''Union''' is a collection of different datatypes grouped together.
+
*'''Union''' is a collection of different datatypes grouped together.
  
'''Union '''allocates one common storage space for all its members
+
*'''Union '''allocates one common storage space for all its members.
  
We can access only one member of union at a time.  
+
*We can access only one member of union at a time.  
  
 
|-
 
|-
Line 173: Line 155:
 
'''union union_name'''
 
'''union union_name'''
  
'''{'''
+
within curly brackets
  
 
'''members;'''
 
'''members;'''
  
'''members;'''
+
after curly brackets
  
'''} union_variable;'''
+
'''union_variable'''
  
'''We also have an alternate syntax.'''
+
and a '''semi-colon'''.
 +
 
 +
 
 +
 
 +
We also have an alternate syntax.
  
 
'''Syntax 2:'''
 
'''Syntax 2:'''
 
  
 
'''union union_name'''
 
'''union union_name'''
  
'''{'''
+
within curly brackets
  
 
'''members;'''
 
'''members;'''
  
'''members;'''
+
after curly brackets '''semi-colon'''
 
+
'''};'''
+
 
+
  
 
'''union union_name union_variable;'''
 
'''union union_name union_variable;'''
Line 203: Line 185:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
  
I have a code file let us go through it.
+
I have a code file; let us go through it.
  
 
Note that our filename is '''union.c'''
 
Note that our filename is '''union.c'''
 
 
 
  
 
|-
 
|-
Line 220: Line 199:
  
 
'''int science;'''
 
'''int science;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have three variables '''eng''', '''maths''' and''' science.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have three variables '''english''', '''maths''' and''' science.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''union student stud;'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''union student stud;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In main function, we have declared a '''union''' variable as '''stud.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In '''main function''', we have declared a '''union''' variable as '''stud.'''
  
 
|-
 
|-
Line 232: Line 211:
  
 
'''stud.science = 65;'''
 
'''stud.science = 65;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can access the union members using union variable.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can access the '''union''' members using a '''union''' variable:
 +
 
 +
*'''stud dot english'''
 +
*'''stud dot maths'''
 +
*'''stud dot science'''
  
 
|-
 
|-
Line 242: Line 225:
  
 
'''./union'''
 
'''./union'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute.
  
On the terminal type,
+
On the '''terminal''', type,
  
 
'''gcc union.c -o union'''
 
'''gcc union.c -o union'''
  
'''./union'''
+
 
 +
Type '''./union'''
  
 
|-
 
|-
Line 258: Line 242:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see the difference between '''structure '''and''' union'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see the difference between '''structure '''and''' union'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide display 10-11-12'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide display 10-11-12'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Union allocates a common storage space for all its members.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Union''' allocates a common storage space for all its members.
  
Structure allocates separate storage space for all its members.
+
'''Structure''' allocates separate storage space for all its members.
  
  
Union occupies lower memory space.
+
'''Union''' occupies lower memory space.
  
Structure occupies higher memory space.
+
'''Structure''' occupies higher memory space.
  
  
Ex for '''union:'''
+
Example for '''union:'''
  
union student{int marks;<nowiki>char name[6];</nowiki>double average;};
+
'''union student{int marks;<nowiki>char name[6];</nowiki>double average;};'''
  
Memory allocation for union variable will be''' 8 bytes.'''
+
Memory allocation for '''union''' variable will be''' 8 bytes.'''
  
As double data type will occupy the maximum memory space.
+
As '''double datatyp'''e will occupy the maximum memory space.
  
  
Ex for '''structure:'''
+
Exampls for '''structure:'''
  
struct student{int mark;<nowiki>char name[6];</nowiki>double average;};
+
'''struct student{int mark;<nowiki>char name[6];</nowiki>double average;};'''
  
  
Line 304: Line 288:
 
* '''typedef '''
 
* '''typedef '''
 
* '''union'''
 
* '''union'''
* '''Difference between union and structure.'''
+
* Difference between '''union''' and '''structure.'''
 
+
 
+
  
 
|-
 
|-
Line 312: Line 294:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
  
Write a program to display records of an employee.
+
#Write a program to display records of an employee.
 
+
#Like '''name, address, salary'''.
Like name, address, salary.
+
#Define a '''union''' named '''employee'''.
 
+
#Give an alias name as emp using '''typedef.'''
Define a union named employee.
+
 
+
Give an alias name as emp using '''typedef.'''
+
  
 
|-
 
|-

Latest revision as of 17:32, 17 October 2014

Title of script: Typedef and Union in C

Author: Ashwini Patil

Keywords: Video tutorial, typedef, union, difference between structure & union


Visual Cue
Narration
Display Slide 1 Welcome to the Spoken Tutorial on Typedef and Union in C.
Display Slide 2 In this tutorial, we will learn about
  • typedef keyword
  • union keyword
  • with the help of some examples.
Display Slide 3

System Requirements

For this tutorial I am using
  • Ubuntu Operating System version 11.10
  • gcc compiler version 4.6.1 on Ubuntu
Display Slide 4

Prerequisites


spoken-tutorial.org

To follow this tutorial, you should be familiar with C tutorials.


If not, for relevant tutorials please visit our website which is as shown.

I will start with an introduction to typedef keyword.
Display Slide 5

Introduction


  • Typedef keyword is used to give symbolic name to an existing type or user-defined datatypes.
  • It is a way to define alias to the commands.
  • It helps to provide clarity to the code.
  • It makes the code easier to understand and change.
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 Open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard.
Compile

gcc pallindrome.c -o pallindrome

Type: gcc pallindrome.c -o pallindrome

Press Enter.

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

within curly brackets

members;

after curly brackets

union_variable

and a semi-colon.


We also have an alternate syntax.

Syntax 2:

union union_name

within curly brackets

members;

after curly brackets semi-colon

union union_name union_variable;

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 english, 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 a union variable:
  • stud dot english
  • stud dot maths
  • stud dot science
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


Type ./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.


Union occupies lower memory space.

Structure occupies higher memory space.


Example for union:

union student{int marks;char name[6];double average;};

Memory allocation for union variable will be 8 bytes.

As double datatype will occupy the maximum memory space.


Exampls for structure:

struct student{int mark;char name[6];double average;};


Memory allocation for structure variable will be:

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,
  • typedef
  • union
  • Difference between union and structure.
Display Slide 14 As an assignment,
  1. Write a program to display records of an employee.
  2. Like name, address, salary.
  3. Define a union named employee.
  4. 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.

Contributors and Content Editors

Ashwini, Nancyvarkey