Difference between revisions of "C-and-C++/C2/Scope-Of-Variables/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 
{| border=1
 
{| border=1
|| ''Time'''
+
|| Time
|| '''Narration'''
+
|| Narration
  
 
|-
 
|-
| 00.01
+
| 00:01
 
| Welcome to the spoken tutorial on Scope of variables in C and C++.
 
| Welcome to the spoken tutorial on Scope of variables in C and C++.
  
 
|-
 
|-
| 00.08
+
| 00:08
 
|In this tutorial we will learn,
 
|In this tutorial we will learn,
  
 
|-
 
|-
| 00.11
+
| 00:11
 
| What is the Scope of variable?
 
| What is the Scope of variable?
  
 
|-
 
|-
| 00.13
+
| 00:13
 
| What is a Global variable?
 
| What is a Global variable?
  
 
|-
 
|-
| 00.16
+
| 00:16
 
| What is a Local variable?
 
| What is a Local variable?
  
 
|-
 
|-
| 00.19
+
| 00:19
 
|Few  examples.
 
|Few  examples.
  
 
|-
 
|-
| 00.22
+
| 00:22
 
|We will also see some common errors and their solutions.
 
|We will also see some common errors and their solutions.
  
 
|-
 
|-
|00.27
+
|00:27
 
| To record this tutorial, I am using
 
| To record this tutorial, I am using
  
 
|-
 
|-
| 00.30
+
| 00:30
 
|'''Ubuntu Operating System''' version 11.04, '''gcc''' and '''g++''' '''Compiler''' version 4.6.1  
 
|'''Ubuntu Operating System''' version 11.04, '''gcc''' and '''g++''' '''Compiler''' version 4.6.1  
  
 
|-
 
|-
|  00.41
+
|  00:41
 
|  Let us start with the introduction to the scope of variables.
 
|  Let us start with the introduction to the scope of variables.
  
 
|-
 
|-
| 00.47
+
| 00:47
 
|It is the region of code within which the variable can be accessed.
 
|It is the region of code within which the variable can be accessed.
  
 
|-
 
|-
| 00.54
+
| 00:54
 
|Depending on its type and place of declaration it is divided into two categories:
 
|Depending on its type and place of declaration it is divided into two categories:
  
 
|-
 
|-
| 00.59
+
| 00:59
 
|Global Variable &
 
|Global Variable &
  
 
|-
 
|-
| 01.02
+
| 01:02
 
|Local Variable.  
 
|Local Variable.  
  
 
|-
 
|-
|01.05
+
|01:05
 
| Now we will see an example,
 
| Now we will see an example,
  
 
|-
 
|-
| 01.07
+
| 01:07
 
|I have already typed the program on the editor,
 
|I have already typed the program on the editor,
  
 
|-
 
|-
| 01.10
+
| 01:10
 
|Let me open it
 
|Let me open it
  
 
|-
 
|-
| 01.14
+
| 01:14
 
|Note that our file name is '''scope.c.'''
 
|Note that our file name is '''scope.c.'''
  
 
|-
 
|-
| 01.19
+
| 01:19
 
|Let me explain the code now.
 
|Let me explain the code now.
  
 
|-
 
|-
| 01.23
+
| 01:23
 
|This is our '''header file.'''
 
|This is our '''header file.'''
  
 
|-
 
|-
|01.26
+
|01:26
 
| Here we have declared two global variables '''a and b.'''
 
| Here we have declared two global variables '''a and b.'''
  
 
|-
 
|-
| 01.32
+
| 01:32
 
|And we have '''initialized''' them by assigning values as '''5 and 2.'''
 
|And we have '''initialized''' them by assigning values as '''5 and 2.'''
  
 
|-
 
|-
| 01.39
+
| 01:39
 
|A global variable is available to all functions in your program.
 
|A global variable is available to all functions in your program.
  
 
|-
 
|-
| 01.44
+
| 01:44
 
|These are declared outside any functions above main() funtion.
 
|These are declared outside any functions above main() funtion.
  
 
|-
 
|-
| 01.51
+
| 01:51
 
|These have a Global scope.'''
 
|These have a Global scope.'''
  
 
|-
 
|-
| 01.53
+
| 01:53
 
|  Here we have declared a '''function''' '''add '''without''' arguments.'''
 
|  Here we have declared a '''function''' '''add '''without''' arguments.'''
  
 
|-
 
|-
|01.59  
+
|01:59  
 
|  Here  sum is  a local variable  it is  declared  inside the function add.
 
|  Here  sum is  a local variable  it is  declared  inside the function add.
  
Line 114: Line 114:
  
 
|-
 
|-
| 02.07
+
| 02:07
 
|A local variable is available only to the function inside which it is declared.
 
|A local variable is available only to the function inside which it is declared.
  
 
|-
 
|-
| 02.13
+
| 02:13
 
|These variables are declared inside a block.
 
|These variables are declared inside a block.
  
 
|-
 
|-
| 02.16
+
| 02:16
 
|These have '''local scope'''.
 
|These have '''local scope'''.
  
 
|-
 
|-
| 02.19
+
| 02:19
 
|Then sum of a & b will be stored in the variable sum . Here we print the sum
 
|Then sum of a & b will be stored in the variable sum . Here we print the sum
  
 
|-
 
|-
| 02.29
+
| 02:29
 
| This is our '''main function.'''
 
| This is our '''main function.'''
  
 
|-
 
|-
| 02.33
+
| 02:33
 
| The '''add''' function is called and then executed.
 
| The '''add''' function is called and then executed.
  
 
|-
 
|-
| 02.38
+
| 02:38
 
|  And This is our return statement.
 
|  And This is our return statement.
  
 
|-
 
|-
|02.40
+
|02:40
 
| Now Click on save.
 
| Now Click on save.
  
 
|-
 
|-
| 02.43
+
| 02:43
 
| Let us execute the program.
 
| Let us execute the program.
  
 
|-
 
|-
| 02.45
+
| 02:45
 
| | Please open the terminal window  by pressing '''Ctrl, ''Alt'' and T''' keys simultaneously on your keyboard.
 
| | Please open the terminal window  by pressing '''Ctrl, ''Alt'' and T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
| 02.55
+
| 02:55
 
|  To compile type,
 
|  To compile type,
  
 
|-
 
|-
| 02.56
+
| 02:56
 
|'''gcc space scope.c space hyphen o space sco''' and press enter.
 
|'''gcc space scope.c space hyphen o space sco''' and press enter.
  
 
|-
 
|-
| 03.05
+
| 03:05
 
|To execute
 
|To execute
  
 
|-
 
|-
| 03.06
+
| 03:06
 
|Type'''./sco''' (dot slash), press enter  
 
|Type'''./sco''' (dot slash), press enter  
  
 
|-
 
|-
| 03.10
+
| 03:10
 
|  the output is displayed as  
 
|  the output is displayed as  
  
 
|-
 
|-
| 03.13
+
| 03:13
 
|'''Sum of a and b is 7'''
 
|'''Sum of a and b is 7'''
  
 
|-
 
|-
| 03.16
+
| 03:16
 
| Now let us see how to execute the same program in C++.
 
| Now let us see how to execute the same program in C++.
  
 
|-
 
|-
|  03.20
+
|  03:20
 
|  Come back to our program. First press Shift ''Ctrl' & ''S'' key simultaneously on your keyboard
 
|  Come back to our program. First press Shift ''Ctrl' & ''S'' key simultaneously on your keyboard
  
 
|-
 
|-
| 03.31
+
| 03:31
 
|Now Save the file''' '''with''' an extension .cpp ''' and click on save  
 
|Now Save the file''' '''with''' an extension .cpp ''' and click on save  
  
  
 
|-
 
|-
|  03.41
+
|  03:41
 
| Let us change the header file as'''iostream.'''
 
| Let us change the header file as'''iostream.'''
  
  
 
|-
 
|-
| 03.47
+
| 03:47
 
| Now  include the '''using '''statement. Click on save .
 
| Now  include the '''using '''statement. Click on save .
  
 
|-
 
|-
| 03.58
+
| 03:58
 
| The  declaration of global variable  and local variable  is same in  C++
 
| The  declaration of global variable  and local variable  is same in  C++
  
 
|-
 
|-
| 04.03
+
| 04:03
 
|So no need to change anything  
 
|So no need to change anything  
  
 
|-
 
|-
|  04.07
+
|  04:07
 
|  Now replace the '''printf '''statement with the  '''cout''' statement
 
|  Now replace the '''printf '''statement with the  '''cout''' statement
  
Line 214: Line 214:
  
 
|-
 
|-
|  04.13
+
|  04:13
 
|  Delete the  '''format specifier''' and '\n'  
 
|  Delete the  '''format specifier''' and '\n'  
  
 
|-
 
|-
| 04.17
+
| 04:17
 
|now delete the comma .
 
|now delete the comma .
  
 
|-
 
|-
|  04.19
+
|  04:19
 
| Type two opening angle brackets
 
| Type two opening angle brackets
  
 
|-
 
|-
| 04.22
+
| 04:22
 
|Delete the closing bracket, again type two opening  angle bracket  
 
|Delete the closing bracket, again type two opening  angle bracket  
  
 
|-
 
|-
| 04.26
+
| 04:26
 
|and within the double quotes type backslash n. Now click on save
 
|and within the double quotes type backslash n. Now click on save
  
 
|-
 
|-
| 04.35
+
| 04:35
 
|Let us execute the program
 
|Let us execute the program
  
  
 
|-
 
|-
| 04.39
+
| 04:39
 
|Come back to a terminal.
 
|Come back to a terminal.
  
 
|-
 
|-
| 04.42
+
| 04:42
 
| To compile type, '''g++ space scope dot cpp space -o space sco1, '''
 
| To compile type, '''g++ space scope dot cpp space -o space sco1, '''
  
  
 
|-
 
|-
| 04.52
+
| 04:52
 
| Here we have ,'''sco1''',  because We don't want to overwrite output parameter  sco for the file scope .c  
 
| Here we have ,'''sco1''',  because We don't want to overwrite output parameter  sco for the file scope .c  
  
 
|-
 
|-
| 05.04
+
| 05:04
 
| now press enter  
 
| now press enter  
  
  
 
|-
 
|-
| 05.07
+
| 05:07
 
|  To execute type'''./sco1'''  and press enter .  
 
|  To execute type'''./sco1'''  and press enter .  
  
 
|-
 
|-
|05.14
+
|05:14
 
|The output is displayed as,  '''Sum of a and b is 7.'''
 
|The output is displayed as,  '''Sum of a and b is 7.'''
  
 
|-
 
|-
|  05.19
+
|  05:19
 
| We can see  that it is similar to our C code,  
 
| We can see  that it is similar to our C code,  
  
 
|-
 
|-
|  05.27
+
|  05:27
 
|Now we will see some common errors which we can come across.
 
|Now we will see some common errors which we can come across.
  
 
|-
 
|-
| 05.31
+
| 05:31
 
|Come back to our program, Suppose here I will declare a  variable '''a''' again,
 
|Come back to our program, Suppose here I will declare a  variable '''a''' again,
  
 
|-
 
|-
| 05.41
+
| 05:41
 
|Type '''int a ''' and a semicolon
 
|Type '''int a ''' and a semicolon
  
 
|-
 
|-
|  05.45
+
|  05:45
 
|  Click on save. We have declared  the variable ''a'' above the main function and after the add function ,
 
|  Click on save. We have declared  the variable ''a'' above the main function and after the add function ,
  
 
|-
 
|-
|  05.55
+
|  05:55
 
| Let us see what happens.
 
| Let us see what happens.
  
  
 
|-
 
|-
| 05.57
+
| 05:57
 
|Come back to our  terminal.
 
|Come back to our  terminal.
  
 
|-
 
|-
|06.01
+
|06:01
 
| Now compile as before ,
 
| Now compile as before ,
  
  
 
|-
 
|-
|06.05
+
|06:05
 
|We see errors , Redefinition of ''int''a , ''int'' a previously defined here.  Come  back to our program  
 
|We see errors , Redefinition of ''int''a , ''int'' a previously defined here.  Come  back to our program  
  
 
|-
 
|-
|06.18
+
|06:18
 
|'''a''' is a global variable.
 
|'''a''' is a global variable.
  
  
 
|-
 
|-
|06.20
+
|06:20
 
|It has a '''global scope.'''
 
|It has a '''global scope.'''
  
  
 
|-
 
|-
|06.22
+
|06:22
 
|We cannot declare the variable twice as it is already declared globally
 
|We cannot declare the variable twice as it is already declared globally
  
  
 
|-
 
|-
|06.27
+
|06:27
 
|We can only declare '''variable a''' as a local variable .
 
|We can only declare '''variable a''' as a local variable .
  
Line 324: Line 324:
  
 
|-
 
|-
|06.34
+
|06:34
 
|Let us fix the error.
 
|Let us fix the error.
  
 
|-
 
|-
|  06.36
+
|  06:36
 
| Delete  this .
 
| Delete  this .
  
 
|-
 
|-
|  06.39
+
|  06:39
 
| Click on save.
 
| Click on save.
  
 
|-
 
|-
|  06.41
+
|  06:41
 
|  Let us execute again.  
 
|  Let us execute again.  
  
  
 
|-
 
|-
|06.42
+
|06:42
 
|Come back to our terminal.
 
|Come back to our terminal.
  
 
|-
 
|-
|  06.45
+
|  06:45
 
|  Now  compile as before, execute as before.
 
|  Now  compile as before, execute as before.
  
 
|-
 
|-
| 06.49
+
| 06:49
 
|  Yes it is working.
 
|  Yes it is working.
  
  
 
|-
 
|-
|06.52
+
|06:52
 
|This brings us to the end of this tutorial.
 
|This brings us to the end of this tutorial.
  
 
|-
 
|-
|  06.56
+
|  06:56
 
| let us summarise  
 
| let us summarise  
  
 
|-
 
|-
|  06.58
+
|  06:58
 
| In this tutorial we learn't ,
 
| In this tutorial we learn't ,
 +
 
|-
 
|-
|  07.00  
+
|  07:00  
 
| Scope of variable,
 
| Scope of variable,
 +
 
|-
 
|-
|  07.02
+
|  07:02
 
| Global variable, e.g : int a=5 &
 
| Global variable, e.g : int a=5 &
 +
 
|-
 
|-
|  07.07
+
|  07:07
 
| And  local variable ,e.g:int sum
 
| And  local variable ,e.g:int sum
 +
 
|-
 
|-
|  07.12
+
|  07:12
 
|  As an assignment,
 
|  As an assignment,
  
  
 
|-
 
|-
|07.14
+
|07:14
 
|Write a program to print the difference of two numbers.
 
|Write a program to print the difference of two numbers.
  
 
|-
 
|-
| 07.19
+
| 07:19
 
| Watch the video available at the link shown below  .
 
| Watch the video available at the link shown below  .
  
  
 
|-
 
|-
|07.22
+
|07:22
 
|It summarises the Spoken Tutorial project.
 
|It summarises the Spoken Tutorial project.
  
  
 
|-
 
|-
|07.25
+
|07:25
 
|If you do not have good bandwidth, you can download and watch it.
 
|If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
| 07.30
+
| 07:30
 
| The Spoken Tutorial Project Team,  
 
| The Spoken Tutorial Project Team,  
  
  
 
|-
 
|-
|07.32
+
|07:32
 
|Conducts workshops using spoken tutorials .
 
|Conducts workshops using spoken tutorials .
  
  
 
|-
 
|-
|07.35
+
|07:35
 
|Gives certificates to those who pass an online test .
 
|Gives certificates to those who pass an online test .
  
  
 
|-
 
|-
|07.40
+
|07:40
 
|For more details, please write to,contact@spoken-tutorial.org
 
|For more details, please write to,contact@spoken-tutorial.org
  
 
|-
 
|-
| 07.47
+
| 07:47
 
|  Spoken Tutorial Project is a part of the Talk to a Teacher project.
 
|  Spoken Tutorial Project is a part of the Talk to a Teacher project.
  
  
 
|-
 
|-
|07.52
+
|07:52
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
  
 
|-
 
|-
|08.00
+
|08:00
 
|More information on this Mission is available at the link shown below
 
|More information on this Mission is available at the link shown below
 
|-
 
|-
| 08.04  
+
| 08:04  
 
|  This is Ashwini Patil from IIT Bombay signing off
 
|  This is Ashwini Patil from IIT Bombay signing off
  
  
 
|-
 
|-
|08.08
+
|08:08
 
|Thank You for watching.  
 
|Thank You for watching.  
  
 
|}
 
|}

Revision as of 11:50, 23 June 2014

Time Narration
00:01 Welcome to the spoken tutorial on Scope of variables in C and C++.
00:08 In this tutorial we will learn,
00:11 What is the Scope of variable?
00:13 What is a Global variable?
00:16 What is a Local variable?
00:19 Few examples.
00:22 We will also see some common errors and their solutions.
00:27 To record this tutorial, I am using
00:30 Ubuntu Operating System version 11.04, gcc and g++ Compiler version 4.6.1
00:41 Let us start with the introduction to the scope of variables.
00:47 It is the region of code within which the variable can be accessed.
00:54 Depending on its type and place of declaration it is divided into two categories:
00:59 Global Variable &
01:02 Local Variable.
01:05 Now we will see an example,
01:07 I have already typed the program on the editor,
01:10 Let me open it
01:14 Note that our file name is scope.c.
01:19 Let me explain the code now.
01:23 This is our header file.
01:26 Here we have declared two global variables a and b.
01:32 And we have initialized them by assigning values as 5 and 2.
01:39 A global variable is available to all functions in your program.
01:44 These are declared outside any functions above main() funtion.
01:51 These have a Global scope.
01:53 Here we have declared a function add without arguments.
01:59 Here sum is a local variable it is declared inside the function add.


02:07 A local variable is available only to the function inside which it is declared.
02:13 These variables are declared inside a block.
02:16 These have local scope.
02:19 Then sum of a & b will be stored in the variable sum . Here we print the sum
02:29 This is our main function.
02:33 The add function is called and then executed.
02:38 And This is our return statement.
02:40 Now Click on save.
02:43 Let us execute the program.
02:45 Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
02:55 To compile type,
02:56 gcc space scope.c space hyphen o space sco and press enter.
03:05 To execute
03:06 Type./sco (dot slash), press enter
03:10 the output is displayed as
03:13 Sum of a and b is 7
03:16 Now let us see how to execute the same program in C++.
03:20 Come back to our program. First press Shift Ctrl' & S key simultaneously on your keyboard
03:31 Now Save the file with an extension .cpp and click on save


03:41 Let us change the header file asiostream.


03:47 Now include the using statement. Click on save .
03:58 The declaration of global variable and local variable is same in C++
04:03 So no need to change anything
04:07 Now replace the printf statement with the cout statement


04:13 Delete the format specifier and '\n'
04:17 now delete the comma .
04:19 Type two opening angle brackets
04:22 Delete the closing bracket, again type two opening angle bracket
04:26 and within the double quotes type backslash n. Now click on save
04:35 Let us execute the program


04:39 Come back to a terminal.
04:42 To compile type, g++ space scope dot cpp space -o space sco1,


04:52 Here we have ,sco1, because We don't want to overwrite output parameter sco for the file scope .c
05:04 now press enter


05:07 To execute type./sco1 and press enter .
05:14 The output is displayed as, Sum of a and b is 7.
05:19 We can see that it is similar to our C code,
05:27 Now we will see some common errors which we can come across.
05:31 Come back to our program, Suppose here I will declare a variable a again,
05:41 Type int a and a semicolon
05:45 Click on save. We have declared the variable a above the main function and after the add function ,
05:55 Let us see what happens.


05:57 Come back to our terminal.
06:01 Now compile as before ,


06:05 We see errors , Redefinition of inta , int a previously defined here. Come back to our program
06:18 a is a global variable.


06:20 It has a global scope.


06:22 We cannot declare the variable twice as it is already declared globally


06:27 We can only declare variable a as a local variable .


06:34 Let us fix the error.
06:36 Delete this .
06:39 Click on save.
06:41 Let us execute again.


06:42 Come back to our terminal.
06:45 Now compile as before, execute as before.
06:49 Yes it is working.


06:52 This brings us to the end of this tutorial.
06:56 let us summarise
06:58 In this tutorial we learn't ,
07:00 Scope of variable,
07:02 Global variable, e.g : int a=5 &
07:07 And local variable ,e.g:int sum
07:12 As an assignment,


07:14 Write a program to print the difference of two numbers.
07:19 Watch the video available at the link shown below .


07:22 It summarises the Spoken Tutorial project.


07:25 If you do not have good bandwidth, you can download and watch it.
07:30 The Spoken Tutorial Project Team,


07:32 Conducts workshops using spoken tutorials .


07:35 Gives certificates to those who pass an online test .


07:40 For more details, please write to,contact@spoken-tutorial.org
07:47 Spoken Tutorial Project is a part of the Talk to a Teacher project.


07:52 It is supported by the National Mission on Education through ICT, MHRD, Government of India.


08:00 More information on this Mission is available at the link shown below
08:04 This is Ashwini Patil from IIT Bombay signing off


08:08 Thank You for watching.

Contributors and Content Editors

Ashwini, Kavita salve, Krupali, PoojaMoolya, Pratik kamble, Sandhya.np14