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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
! <center>Visual Cue</center>
+
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
! <center>Narration</center>
+
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 +
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Scope of variables in C and C++.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Scope of variables in C and C++'''.
  
 
|-
 
|-
Line 22: Line 23:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
  
Scope of variable.
+
What is the Scope of variables.
  
Global variable.
+
What is a Global variable.
  
Local variable.
+
What is a Local variable.
  
We will do this with the help of example.
+
Few examples.
  
 
We will also see some common errors and their solutions.
 
We will also see some common errors and their solutions.
Line 41: Line 42:
 
'''Ubuntu Operating System''' version 11.04,
 
'''Ubuntu Operating System''' version 11.04,
  
'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on Ubuntu
+
'''gcc''' and '''g++''' '''Compiler''' version 4.6.1
  
 
|-
 
|-
Line 52: Line 53:
  
 
Global Variable.
 
Global Variable.
 
+
And
 
Local Variable.  
 
Local Variable.  
  
Line 65: Line 66:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see an example.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see an example.
  
I have already typed the code on the editor,
+
I have already typed the program on the editor,
  
Let me open it
+
Let me open it.
  
 
|-
 
|-
Line 91: Line 92:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared two global variables '''a and b.'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared two global variables '''a and b.'''
  
And we have '''initialized''' them by assigning value as '''5 and 2.'''
+
And we have '''initialized''' them by assigning values as '''5 and 2.'''
  
 
A global variable is available to all functions in your program.
 
A global variable is available to all functions in your program.
Line 159: Line 160:
  
 
'''Ctrl, Alt and T''' keys simultaneously
 
'''Ctrl, Alt and T''' keys simultaneously
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal by pressing '''Ctrl, Alt and T''' keys simultaneously
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal by pressing '''Ctrl, Alt and T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 171: Line 172:
  
 
'''./sco'''
 
'''./sco'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type,
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type,
  
'''gcc scope.c -o sco'''
+
'''gcc space scope.c space -o space sco'''
  
'''Press Enter'''
+
And '''press Enter'''
  
 
To execute
 
To execute
Line 183: Line 184:
 
'''./sco'''
 
'''./sco'''
  
'''Press Enter'''
+
'''press Enter'''
  
 
|-
 
|-
Line 212: Line 213:
  
 
|-
 
|-
| 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;"| Highlight header file
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let me change a few things here
+
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Delete
 
 
<nowiki><stdio.h></nowiki>
 
 
Type
 
  
'''<nowiki><iostream></nowiki>'''
 
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change the header file as
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change the header file as
  
Line 245: Line 238:
  
 
'''<nowiki>cout <<</nowiki>'''
 
'''<nowiki>cout <<</nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with the '''cout''' statement
  
  
Line 283: Line 276:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile Type,  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile Type,  
  
'''g++ scope.cpp -o sco1'''
+
'''g++ space scope.cpp space -o space sco1'''
  
 
Here we have''' sco1 '''because we don't want to''' '''overwrite the output parameter '''sco''' for the file''' scope.c'''
 
Here we have''' sco1 '''because we don't want to''' '''overwrite the output parameter '''sco''' for the file''' scope.c'''
  
press''' Enter.'''
+
Now press''' Enter.'''
  
 
|-
 
|-
Line 299: Line 292:
 
'''./sco1'''
 
'''./sco1'''
  
press''' Enter.'''
+
And press''' Enter.'''
  
 
|-
 
|-
Line 307: Line 300:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as,  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as,  
  
'''Sum of a and b is 9.'''
+
'''Sum of a and b is 7.'''
  
 
We can see that it is similar to our''' C '''code.
 
We can see that it is similar to our''' C '''code.
Line 319: Line 312:
 
'''int a = 2;'''
 
'''int a = 2;'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see some common errors which we can come across.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see some common errors which we can come across.
 +
Come back to our program.
  
 
Suppose here I will declare variable '''a''' again,
 
Suppose here I will declare variable '''a''' again,
Line 324: Line 318:
 
Type  
 
Type  
  
'''int a = 2;'''
+
'''int a''' and a '''(semicolon) ;'''
  
 
|-
 
|-
Line 345: Line 339:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile as before.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile as before.
  
We see an errors
+
We see errors
  
 
Redifinition of '''int a'''  
 
Redifinition of '''int a'''  
Line 383: Line 377:
 
|-
 
|-
 
| 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="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Nocompile as before
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile as before
  
 
Execute as before.
 
Execute as before.
Line 392: Line 386:
  
 
This brings us to the end of this tutorial.
 
This brings us to the end of this tutorial.
 
 
  
  
Line 406: Line 398:
 
Global variable
 
Global variable
  
eg. int a =5;
+
eg. int a = 5;
 +
And
  
 
Local variable
 
Local variable
Line 454: Line 447:
 
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.
  
More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro.
+
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro.
  
 
|-
 
|-

Latest revision as of 16:42, 5 February 2014

Title of script: Scope of variables

Author: Ashwini R. Patil

Keywords: Scope of variables, global variable, local variable, Video tutorial


Visual Cue
Narration


Slide 1 Welcome to the spoken tutorial on Scope of variables in C and C++.
Slide 2


In this tutorial we will learn,

What is the Scope of variables.

What is a Global variable.

What is a Local variable.

Few examples.

We will also see some common errors and their solutions.

Slide 3


To record this tutorial, I am using

Ubuntu Operating System version 11.04,

gcc and g++ Compiler version 4.6.1

Slide 4 Let us start with the introduction to the scope of variables.

It is the region of code within which the variable can be accessed.

Depending on its type and place of declaration it is divided into two catagories:

Global Variable. And Local Variable.

Type:


#include<stdio.h>


Now we will see an example.

I have already typed the program on the editor,

Let me open it.

Point the cursor

scope.c

Note that our filename is scope.c.

Let me explain the code now.

Highlight

#include <stdio.h>

This is our header file.
Highlight

int a=5;

int b=2;

Here we have declared two global variables a and b.

And we have initialized them by assigning values as 5 and 2.

A global variable is available to all functions in your program.

These are declared outside any functions above main() funtion.

These have a Global scope.

Highlight

void add()

Here we have declared a function add without arguments.
Highlight

int sum;

sum = a+b;

Here sum is a local variable.

It is declared inside the function add.

A local variable is available only to the function inside which it is declared.

These variables are declared inside a block.

These have local scope.

The sum of a and b will be stored in the variable sum.

Highlight

printf("Sum is %d\n",c);

Here we print the sum.
Highlight

int main()

This is our main function.
Highlight

add();

The add function is called and then executed.
Highlight

return 0;

And this is our return statement.
Click on save Now Click on save.
Let us execute the program.
Open the terminal

Ctrl, Alt and T keys simultaneously

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
Type

gcc scope.c -o sco

To execute

Type

./sco

To compile type,

gcc space scope.c space -o space sco

And press Enter

To execute

Type

./sco

press Enter

Highlight

Output

The output is displayed as

Sum of a and b is 7

On the editor NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++

Come back to our program

Go to File menu

Click on Save as option

Type

.cpp

First press shfit+ctrl and s keys simultaneously on your keyboard.

Now Save the file with an extension. .cpp and click on SAVE

Highlight header file


Let us change the header file as

iostream.

Type

using namespace std;

Now include the using statement.

Click on Save.

The declaration of global variable and local variable is same in C++.

So no need to change anything.

Type

cout <<

Now replace the printf statement with the cout statement



Delete

%d\n

Delete the format specifier and '\n'

Now delete the comma.

Type

<<

Type two opening angle brackets.Delete the closing bracket.

Again type two opening angle brackets and within the double quotes type backslash n.

Now click on Save.

Let us execute the program.

On the terminal Come back to the terminal.
Type

g++ scope.cpp -o sco1


To compile Type,

g++ space scope.cpp space -o space sco1

Here we have sco1 because we don't want to overwrite the output parameter sco for the file scope.c

Now press Enter.

Type

./sco1

To execute,

Type,

./sco1

And press Enter.

Highlight

output

The output is displayed as,

Sum of a and b is 7.

We can see that it is similar to our C code.

Errors


Type

int a = 2;

Now we will see some common errors which we can come across.

Come back to our program.

Suppose here I will declare variable a again,

Type

int a and a (semicolon) ;

On the terminal Click on save.

We have declared the variable a above the main function.

And after the add function.

Let us see what happens.

Come back to the terminal.

Highlight


Error

Now compile as before.

We see errors

Redifinition of int a

int a previously defined here.

Come back to our program.

a is a global variable.

It has a global scope.

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

We can only declare variable a as a local variable.

On the editor Let us fix the error.
Delete

int a = 2;

Delete this.
Click on save Click on save.
On the terminal Let us execute again.

Come back to our terminal.

Now compile as before

Execute as before.

Yes it is working.

This brings us to the end of this tutorial.


Slide 5 Let us summarize,

In this tutorial we learnt,

Scope of variables

Global variable

eg. int a = 5; And

Local variable

eg. int sum;

Slide 6 As an assignment,

Write a program to print the difference of two numbers.

Slide 7

About the Spoken Tutorial Project

Watch the video available at the link shown .

It summarises the Spoken Tutorial project.

If you do not have good bandwidth, you can download and watch it.

Slide 8

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

Slide Number 9


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 the link shown below: http://spoken-tutorial.org\NMEICT-Intro.

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini