Scope Rules In C https://www.skillvertex.com/blog Fri, 10 May 2024 06:50:47 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Scope Rules In C https://www.skillvertex.com/blog 32 32 Scope Rules In C https://www.skillvertex.com/blog/scope-rules-in-c/ https://www.skillvertex.com/blog/scope-rules-in-c/#respond Fri, 10 May 2024 06:50:47 +0000 https://www.skillvertex.com/blog/?p=1836 Read more]]>

Table of Contents

Scope Rules In C

Scope rules in C are like the rules that guide how and where you can use things in programming. They determine where you can work with different pieces of information, like numbers or words, in your program. Whether you’re just starting to learn programming or you’re already experienced, understanding these rules is crucial for writing good code in C. In this explanation, we’ll make it easier to understand how these scope rules work in C and how they affect your code. So, let’s dive into the world of C scope rules together!

What is the Scope Of Variable in C

In C programming, the scope of a variable refers to the specific part of the program where that variable is declared, defined, and used. Once we go outside this specific part, the variable becomes inaccessible, as if it doesn’t exist.

Think of scope as the area where you can “see” and use a variable.

In C, you can only work with a variable within its scope, and you can’t use it outside of that scope. It’s important to note that in C, all identifiers follow lexical (or static) scoping rules.

Example

// C program to illustrate the scope of a variable
#include <stdio.h>
 
int main()
{
    // Scope of this variable is within main() function
    // only.
    int var = 34;
 
    printf("%d", var);
    return 0;
}
 
// function where we try to access the var defined in main()
void func() { printf("%d", var); }

Output

solution.c: In function 'func':
solution.c:15:28: error: 'var' undeclared (first use in this function)
 void func() { printf("%d", var); }

Types of Scope Rules in C

C Scope Rules are divided into 2 categories :

  1. Global Scope
  2. Local Scope

1. Global Scope in C

The global scope in programming refers to the area outside of any specific block or function. Variables that are declared in this global scope are known as global variables. These global variables can be seen and used in any part of the program.

You can also think of the global scope as the file scope because the scope of an identifier begins at the start of the file where it’s defined and continues until the end of that file.

Example

// C program to illustrate the global scope
#include <stdio.h>
  
// variable declared in global scope
int global = 5;
  
// global variable accessed from
// within a function
void display()
{
    printf("%d\n", global);
}
  
// main function
int main()
{
    printf("Before change within main: ");
    display();
  
    // changing value of global
    // variable from main function
    printf("After change within main: ");
    global = 10;
    display();
}

Output

Before change within main: 5
After change within main: 10

Linkage of Variables in Global Scope

Global variables in C have external linkage by default, which means they can be accessed in another C source file. To access these global variables from another source file, you need to use the extern keyword to declare them. This allows you to use the global variables defined in one source file in other source files as well, promoting code modularity and reusability.

Example

file1.c

// filename: file1.c
 
int a;
 
int main(void)
{
   a = 2;
}

file2.c

// filename: file2.c
// When this file is linked with file1.c, functions
// of this file can access a
 
extern int a;
 
int myfun()
{
   printf("%d", a);
}

Output



2

2. Local Scope in C

The local scope in programming is the area enclosed by curly braces {} within a block or a function. Variables declared within this local scope are known as local variables. These local variables are only visible within the block they are declared in and any nested blocks inside that block. Local scope is also referred to as block scope because it’s tied to the specific block where the variables are declared.

Additionally, local variables have internal linkage by default, meaning they are only accessible within the file or function where they are defined and cannot be accessed from other source files.

Example

// C program to illustrate the local scope
#include <stdio.h>
 
// Driver Code
int main()
{
    {
        int x = 10, y = 20;
        {
            // The outer block contains
            // declaration of x and
            // y, so following statement
            // is valid and prints
            // 10 and 20
            printf("x = %d, y = %d\n", x, y);
            {
                // y is declared again,
                // so outer block y is
                // not accessible in this block
                int y = 40;
 
                // Changes the outer block
                // variable x to 11
                x++;
 
                // Changes this block's
                // variable y to 41
                y++;
 
                printf("x = %d, y = %d\n", x, y);
            }
 
            // This statement accesses
            // only outer block's
            // variables
            printf("x = %d, y = %d\n", x, y);
        }
    }
    return 0;
}

Output

x = 10, y = 20
x = 11, y = 41
x = 11, y = 20

FAQ – Scope Rules In C

Q1. What are the 4 scopes of C?

Ans. This concept is also known as “lexical scope.” In programming, there are four main types of scope: function scope, file scope, block scope, and function prototype scope.

Q2. What is the scope of the storage class in C?

Ans. In the C language, we use storage classes to specify key attributes of a variable, such as its visibility, lifetime, initial value, and memory location. These storage classes help define how long a variable exists and where it can be accessed in a C program.

Q3. What is the scope of a structure in C?

Ans. In C programming, the scope of a type defines where it can be used in the source code. When defining a structure, you have two forms: one with a name and another without. The named form allows broader use throughout the program, while the unnamed form has limited scope, typically within the same block or function.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/scope-rules-in-c/feed/ 0