scope of variables

14

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 12-Jul-2015

120 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Scope of variables
Page 2: Scope of variables

SCOPE OF VARIABLE

SOBIN [email protected]

Sobin jose

Sobin jose

Page 3: Scope of variables

Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner

Page 4: Scope of variables

What are Variables in C

Variables in C have the same meaning as variables in algebra

Eg: x = a + bz + 2 = 3(y - 5)

variables in algebra are represented by a single alphabetic character.

Page 5: Scope of variables

Variable names in c 1. May only consist of letters, digits,

and underscores

2. May be as long as you like, but only the first 31 characters are significant

3. May not begin with a number

Page 6: Scope of variables

Naming Conventions

Begin variable names with lowercase letters

Use meaningful identifiers

Separate “words” within identifiers with underscores or mixed upper and lower case.

Eg: int int_a; char str_a;

Page 7: Scope of variables

There are two places where variablescan be declared in C programminglanguage:

1. Inside a function or a block which iscalled “local variables”

2. Outside of all functions which is called“global variables”.

Page 8: Scope of variables

Local Variable

Variables that are declaredinside a function are called localvariables. They can be used only bystatements that are inside thatfunction . Local variables are notknown to functions outside theirown.

Page 9: Scope of variables

Eg:#include <stdio.h>Void main (){

/* local variable declaration */int int_ x, int_ y;Int int_ c;

/* actual initialization */int_ x = 5;int_y = 20;int_ c = int_x + int_ y;

printf ("value of x = %d, b = %d and c= %d\n", int_x, int_y, int_ c);

}

Page 10: Scope of variables

Global variables are defined outside of afunction, usually on top of the program. Theglobal variables will hold their value through outthe lifetime of your program and they can beaccessed inside any of the functions defined forthe program.

A global variable can be accessed by anyfunction. That is, a global variable is available foruse through out your entire program after itsdeclaration.

Global Variables

Page 11: Scope of variables

Eg: #include <stdio.h>/* global variable declaration */int int_ g;Int main (){/* local variable declaration */int int_ a, int_b;/* actual initialization */int_ a = 10;int_ b = 20;int_ g = int_ a + int_ b;printf ("value of a = %d, b = %d and g = %d\n", int_ a, int_ b, int_ g);return 0;

Page 12: Scope of variables

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designedto make a quick, good software professional out of anybody who holds a passion for coding.

Page 13: Scope of variables

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 14: Scope of variables

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Contact Us