Basic Scope Rules in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −

  • Inside a function or a block which is called local variables.

  • Outside of all functions which is called global variables.

  • In the definition of function parameters which are called formal parameters.

Let us understand what are local and global variables, and formal parameters.

Local Variables

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. Local variables are not known to functions outside their own. The following example shows how local variables are used. Here all the variables a, b, and c are local to main() function.

#include <stdio.h>

int main () {

/* local variable declaration */
int a, b;
int c;

/* actual initialization */
a = 10;
b = 20;
c = a + b;

printf ("value of a = %d, b = %d and c = %d
", a, b, c);

return 0;
}

Global Variables

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how global variables are used in a program.

#include <stdio.h>

/* global variable declaration */
int g;

int main () {

/* local variable declaration */
int a, b;

/* actual initialization */
a = 10;
b = 20;
g = a + b;

printf ("value of a = %d, b = %d and g = %d
", a, b, g);

return 0;
}

A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example −

#include <stdio.h>

/* global variable declaration */
int g = 20;

int main () {

/* local variable declaration */
int g = 10;

printf ("value of g = %d
", g);

return 0;
}

When the above code is compiled and executed, it produces the following result −

value of g = 10

Formal Parameters

Formal parameters, are treated as local variables with-in a function and they take precedence over global variables. Following is an example −

#include <stdio.h>

/* global variable declaration */
int a = 20;

int main () {

/* local variable declaration in main function */
int a = 10;
int b = 20;
int c = 0;

printf ("value of a in main() = %d
", a);
c = sum( a, b);
printf ("value of c in main() = %d
", c);

return 0;
}

/* function to add two integers */
int sum(int a, int b) {

printf ("value of a in sum() = %d
", a);
printf ("value of b in sum() = %d
", b);

return a + b;
}

When the above code is compiled and executed, it produces the following result −

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

Initializing Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows −

Data TypeInitial Default Value
int0
char''
float0
double0
pointerNULL

It is a good programming practice to initialize variables properly, otherwise your program may produce unexpected results, because uninitialized variables will take some garbage value already available at their memory location.

The document Basic Scope Rules in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Scope Rules in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What are scope rules in C programming?
Ans. Scope rules in C programming refer to the set of rules that determine the visibility and accessibility of variables, functions, and other identifiers within a program. These rules define where a variable can be accessed and how long it remains in memory.
2. What is the difference between global and local scope in C programming?
Ans. In C programming, variables declared outside any function have a global scope, meaning they can be accessed from any part of the program. On the other hand, variables declared inside a function have a local scope, and they can only be accessed within that specific function.
3. What is the lifetime of a variable in C programming?
Ans. The lifetime of a variable refers to the period during which the variable exists in memory. In C programming, local variables have a lifetime that starts when the function is called and ends when the function returns. Global variables, on the other hand, have a lifetime that spans the entire execution of the program.
4. Can a variable with the same name exist in different scopes in C programming?
Ans. Yes, it is possible to have variables with the same name in different scopes in C programming. This is because each scope defines a separate namespace, and variables within a particular scope are independent of variables with the same name in other scopes. However, accessing the variable with the same name will depend on the current scope.
5. What happens when a variable is declared with the same name in nested scopes in C programming?
Ans. When a variable is declared with the same name in nested scopes in C programming, the innermost declaration takes precedence over the outer declarations. This means that the inner variable will be accessed within its own scope, while the outer variable will be inaccessible within the inner scope. However, once the inner scope is exited, the outer variable will become accessible again.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

study material

,

video lectures

,

Sample Paper

,

Summary

,

shortcuts and tricks

,

Basic Scope Rules in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Basic Scope Rules in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

ppt

,

practice quizzes

,

MCQs

,

mock tests for examination

,

Objective type Questions

,

past year papers

,

Semester Notes

,

Important questions

,

Viva Questions

,

Extra Questions

,

Exam

,

Basic Scope Rules in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

pdf

,

Free

;