C-and-C-Plus-Plus/C2/Scope-of-Variables-in-C-and-C-Plus-Plus/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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,

Scope of variable.

Global variable.

Local variable.

We will do this with the help of example.

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 on Ubuntu

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.

Local Variable.

Type:


#include<stdio.h>


Let us see an example,

I have already typed the code on the editor,

So i will open it

Point the cursor

scope.c

I have saved the file with the name 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 value of 3 and 5.

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 c;

c = a+b;

Then we have declared a local variable c.

The sum of a and b will be stored in c.

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.

Highlight

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

Then 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;

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
Type

gcc scope.c -o sco

To execute

Type

./sco

To compile the program type,

gcc scope.c -o sco

To execute

Type

./sco

Highlight

Output

Here the output is displayed as

Sum of a and b is 8

Click on save Click on save.

Let us execute.

On the terminal Come back to our terminal.

Now compile and execute as earlier.

Highlight

Output

Here 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

Now click on File menu.

click on Save as option.

Save the file with .cpp extension.

Now let me change a few things here
Delete

<stdio.h>

Type

<iostream>

First we will change the header file as

iostream.

Type

using namespace std;

We will include the using statement here.
The function declaration in C++ is same as in C,

So no need to change anything here.

Type

cout <<

Now replace the printf statement with cout statement

As we use cout<< function to print a line in C++.

Delete

%d\n

We don't need the format specifier and '\n' here,

So delete it.

Type

<<

Now type two opening angle brackets here

Then type sum two opening angle brackets again,

and within the double quotes backslash n.

Delete the closing bracket.

On the terminal Let us compile.

Come back to the terminal.

Type

g++ scope.cpp -o sco1

Type

./sco1

Type,

g++ scope.cpp -o sco1,

and press Enter.

To execute,

Type,

./sco1

Highlight

output

Here the output is displayed as,

Sum of a and b is 9.

Errors


Type

int a = 2;

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

Suppose here I will declare variable a again,

Type

int a = 2;

On the terminal Click on save.

Come back to the terminal.

Highlight


Error

Let us compile,

We see an error that declaration of int a shadows a parameter.

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 now.

On the editor Come back to the program.

Let us fix the error.

Delete

int a = 2;

Remove this declaration.
Click on save Click on save.
On the terminal Let us execute.

Come back to our terminal.

Let us compile and execute as before.
Yes it is working.

Now come back to our slides.

Slide 7 As an assignment,

Write a program to print the difference of two numbers.

Slide 8

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 9

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 10


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 signing off

Thank You for joining.

Contributors and Content Editors

Ashwini, Chandrika