Scope of variables

251 views
189 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
251
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scope of variables

  1. 1. Sreyachandran.v Sreya.chandran31@gmail.com www.facebook.com/sreyachandran twitter.com/username in.linkedin.com/in/profilename Scope of variable in C
  2. 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  3. 3. Scope of variable •Refers to where variables can be referenced •There are three places where variables can be declared in c programming language 1. Local variable 2. Global variable 3. Formal parameters
  4. 4. Local variable • Variables that are declared inside a function or block are called local variables. • They can be used only by statements that are inside that function or block of code. function_1() { data_type a; }
  5. 5. • Example #include<stdio.h> main() { int a=5; ++a; printf("%d", a); increment(); return 0; } void increment() { ++a; printf("%d", a); } Error : function is trying to access local variable of main()
  6. 6. Global variable • Global variables in c have their declaration outside the function definition of all functions used within the program. • They remains in the memory as long as the program is executing.
  7. 7. • Example #include<stdio.h> int a=5; main() { ++a; printf("%dn", a); increment(); return 0; } void increment() { ++a; printf("%dn",a); } Output 6 7
  8. 8. Nested scope • Local variables of same name can be nested inside global variables. int total = 5; int main ( ) { int total = 4; …. } int sub1 ( ) { int i = total; } Nested scope
  9. 9. • Example #include<stdio.h> int f1(); int a=10; main() { int a=20; int b=f1(); printf(“%dn % d”, a , b); } f1(int x) { x=a; return x; } Using local variable ‘a’ Using global variable ‘a’ Output 20 10
  10. 10. 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 designed to make a quick, good software professional out of anybody who holds a passion for coding.
  11. 11. 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
  12. 12. Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com Contact Us

×