Home > Backend Development > C++ > Explain the scoping rules related to statement blocks in C language

Explain the scoping rules related to statement blocks in C language

WBOY
Release: 2023-09-11 12:53:10
forward
709 people have browsed it

Explain the scoping rules related to statement blocks in C language

Scope rules are related to the following factors −

  • Accessibility of variables.
  • The existence period of the variable.
  • Usage boundaries of variables.

The scope rules related to statement blocksare as follows −

  • Statement blocks are enclosed by curly braces and contain a set of statement.

  • Variables declared in a statement block can be accessed and used within the block, but do not exist outside the block.

Example 1

The following is a C program related to Scope rules related to statement blocks

Demonstration

#include<stdio.h>
main ( ){
   {
      int i = 1;
      printf ("%d",i);
   }
   {
      int j=2;
      printf("%d",j);
   }
}
Copy after login

Output

The output is as follows −

1 2
Copy after login
Copy after login

Even if variables are redeclared in their respective code blocks and use the same name, they are considered different.

Example 2

The following is another C program about statement block scope rules-

Real-time demonstration

#include<stdio.h>
main ( ){
   {
      int i = 1;
      printf ("%d",i);
   }
   {
      int i =2;
      printf ("%d",i);
   }
}
Copy after login

Output

The output is as follows −

1 2
Copy after login
Copy after login

Redeclaring a variable inside a block with the same name as the outer block masks the outer block variable, which happens when the inner block is executed.

Example 3

This is another C program about scope rules related to statement blocks

Real-time demonstration

#include<stdio.h>
main ( ){
   int i = 1;{
      int i = 2;
      printf ("%d",i);
   }
}
Copy after login

Output

The output is as follows −

2
Copy after login

Variables declared outside the inner block can be accessed in the nested block, provided that these variables are not declared in the inner block.

Example 4

Consider another program with scoping rules associated with statement blocks:

Demonstration

#include<stdio.h>
main ( ){
   int i = 1;{
      int j = 2;
      printf ("%d",j);
      printf ("%d",i);
   }
}
Copy after login

Output

Output As follows −

2 1
Copy after login

The above is the detailed content of Explain the scoping rules related to statement blocks in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template