
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the scope rules related to the statement blocks in C language
The scope rules are related to the following factors −
- Accessibility of a variables.
- Period of existence of a variable.
- Boundary of usage of variables.
Scope rules related to statement blocks are given below −
Block is enclosed in curly braces which consists of set of statements.
Variables declared in a block are accessible and usable within that block and does not exist outside it.
Example 1
Following is the C program for scope rules related to statement blocks −
#include<stdio.h> main ( ){ { int i = 1; printf ("%d",i); } { int j=2; printf("%d",j); } }
Output
The output is stated below −
1 2
Even if the variables are redeclared in their respective blocks and with the same name, they are considered differently.
Example 2
Here is another C program for scope rules related to statement blocks−
#include<stdio.h> main ( ){ { int i = 1; printf ("%d",i); } { int i =2; printf ("%d",i); } }
Output
The output is stated below −
1 2
Redeclaration of variables within the blocks bearing the same names as those in the outer block masks the outer block variables, while executing the inner blocks.
Example 3
Here is another C program for scope rules related to statement blocks−
#include<stdio.h> main ( ){ int i = 1;{ int i = 2; printf ("%d",i); } }
Output
The output is stated below −
2
Variables declared outside the inner blocks are accessible to the nested blocks, provided these variables are not declared within the inner block.
Example 4
Consider another program for scope rules related to statement blocks −
#include<stdio.h> main ( ){ int i = 1;{ int j = 2; printf ("%d",j); printf ("%d",i); } }
Output
The output is stated below −
2 1
- Related Articles
- Explain scope rules related to the functions in C language
- What are the local and global scope rules in C language?
- Scope Rules in C
- Explain scope of a variable in C language.
- Explain switch statement in C language
- What are the scope rules to functions in C programming?
- Explain ‘simple if’ statement in C language
- Explain if-else statement in C language
- Explain variable declaration and rules of variables in C language
- Explain common code blocks in JavaScript switch statement?
- Explain Nested if-else statement in C language
- Explain else-if ladder statement in C language
- Explain the Scope and Scope Chain in JavaScript
- Explain the Union to pointer in C language
- Explain the pointers to unions in C language
