C Global And Local Variables


C program is a block of statements that are enclosed within { }. Each block has its own scope and it decides which variable to use that falls under its scope. In other words, two variables with the same name can exist in the same program if they are declared in different blocks.

The variables have two scopes that way – Global and Local.

Global Variables

When a variable is declared outside of the program then its scope widens and all block of code in the C program can use that variable.

A global variable declaration is similar to any other variable declaration except it is declared outside of the main program and usually placed on top of the main function.

Local Variables

The variables that are declared inside a block, cannot be used outside of the block. The scope of such a variable is local to the block itself. They are called Local Variables.


Since the local variables are native to the block of codes in C programs, they have a higher priority than the global variables.

For example,

#include <stdio.h>;
int sum = 10;
/* declaring and initializing a Global variable */
int main()
       int sum = 100;
/* Declaring and initializing a Local variable with the same name */
       printf(" SUM = %dn", sum);
    return 0;

The scope of the variable is local inside of the block and global outside of the main program. The variable sum inside and outside are different, they are not the same. The local variable sum value gets printed to the output console because it has higher priority.


SUM = 100


  • Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,
  • Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.


Effective C: An Introduction to Professional C Programming

A detailed introduction to the C programming language for experienced programmers.

The world runs on code written in the C programming language, yet most schools begin the curriculum with Python or Java. Effective C bridges this gap and brings C into the modern era–covering the modern C17

You’ll learn:

  • How to identify and handle undefined behavior in a C program
  • The range and representations of integers and floating-point values
  • How dynamic memory allocation works and how to use nonstandard functions
  • How to use character encodings and types
  • How to perform I/O with terminals and filesystems using C Standard streams and POSIX file descriptors
  • How to understand the C compiler’s translation phases and the role of the preprocessor
  • How to test, debug, and analyze C programs

Effective C will teach you how to write professional, secure, and portable C code that will stand the test of time and help strengthen the foundation of the computing world.