

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 if-else statement 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 variable declaration and rules of variables in C language
- Explain ‘simple if’ statement in C language
- C program to explain the goto statement
- Explain the Union to pointer in C language
- Explain the pointers to unions in C language