scope of variables
TRANSCRIPT
Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner
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.
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
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;
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”.
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.
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);
}
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
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;
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.
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
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