Variables in C Programming: Types, Scope, and Lifetime
Variables in C Programming are a container to hold the value of a data type. They help the compiler to create memory space for the type of Variable. Each variable has a unique name.
The variable name is similar to the name of a person. Each individual has some name to recognize him. The only purpose of the variable name is to make it human-readable; the compiler does not use a variable name. The variables in C programming have a scope that defines their accessibility and lifetime.
Also, Explore Data Types in C.
There are various types of variables with different scopes and lifetimes. In this tutorial, we will understand the concepts of variables in C programming with their different types, scopes, and lifetimes. We will use C programs to use and declare variables.
Before we move further, let’s go through the list of topics that we will cover in this article:
Table Of Contents
- What is a Variable in C Programming
- What is the Scope and Lifetime of a Variable
- Types of Variables in C Programming
- Conclusion
What is a Variable in C Programming
A variable is a holder of data. In geek’s terms, a variable is a memory allocation space to a data type. It stores a value or data so that the C compiler will create a memory space for that data. Each variable has a separate name to make it readable and unique.
Syntax of Variable in C is:
data_type variable_name = value
Example:
int num = 3
Here,
- int is the data type
- num is a variable name
- 3 is the variable value
A Variable in C programming consists of two parts, Variable definition, and initialization.
- Variable definition = it consists of the data type and the variable name. This part helps to identify the variable data type and its identifier. Each data type consumes different memory, so it is necessary to define the variable type.
- Variable initialization = variable initialization means to provide some value to the variable as per the data type.
Rules for Variable Names in C Programming
There are rules for defining the variables in C Programming, and those rules are as follows:
- Variable names will always start with an alphabet and underscore. Example: num, name, a, x, _value.
- Variable names in C will never start with a digit. Example: 4name, is an invalid name.
- Variables should be declared with their data type. Variable names without data type will generate errors. For example, int a =2 is valid. a = 2 is an invalid variable.
- C is a strongly typed language, you cannot change the data type of a variable after its definition.
- Reserved keywords cannot be used as a variable name in C programming.
- No white spaces are allowed within the variable name.
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
What is the Scope and Lifetime of a Variable
The scope is the variable region in which it can be used. Beyond that area, you cannot use a variable. The local and global are two scopes for C variables. The local scope is limited to the code or function in which the variable is declared.
Global scope is the entire program. Global variables can be used anywhere throughout the program.
Lifetime is the life or alive state of a variable in the memory. It is the time for which a variable can hold its memory. The lifetime of a variable is static and automatic. The static lifetime variable remains active till the end of the program.
An automatic lifetime variable or global variable activates when they are called else they vanish when the function executes.
Types of Variables in C programming
The variables in C programming can be divided into different types based on their scope and storage classes.
- Types of Variables on the Basis of Scope
- Local Variables
Types of Variables on the Basis of Scope
The variable that is declared in a function or code is called a local variable. The scope of Local variables is within the defined function only. You cannot use a local variable outside the function (in which it is declared).
Program of local variables.
#include <stdio.h>
void person()
{
// Local Variables of the function
int age = 20;
float height = 5.6;
printf("age is %d \n", age);
printf("height is %f", height);
}
int main()
{
person();
return 0;
}
OUTPUT
age is 20 height is 5.600000
Global Variables
The scope of the global variable is the entire program. They are not defined inside any function and can be used in any function.
Program to use a global variable in C
#include <stdio.h>
// Declaring global variable
int a = 23;
void function1()
{
// Function using global variable a
printf("The number is %d \n", a);
}
void function2()
{
// Function using global variable a
printf("The number is %d \n", a);
}
int main()
{
// Calling functions
function1();
function2();
return 0;
}
OUTPUT
The number is 23 The number is 23
Types of Variables on the Basis of Storage Class or Lifetime
These Variables use keywords before their definition. The keyword defines the purpose and use of the variable. Their scope can be local, global, or both depending on their type. There are 4 types of keyword variables.
You can also explore: Learning About Pointers in C
Static Variable
The static variable is defined using the static keyword. Its scope depends on the area of its declaration. If a static variable is defined within a function, it is a local variable. If declared outside the function, its scope is global.
A static variable statically allocated memory to the variable and its lifetime throughout the program. It holds its value whenever a program is called.
The default value of the static variable is 0.
Syntax:
static data_type variable_name = initial_value
Program to use static variable in C
#include <stdio.h>
void value()
{
int a = 10; // Local variable
static int b = 20; // Static variable
a = a + 10;
b = b + 10;
printf("The value of local variable: %d \n", a);
printf("The value of Static variable: %d \n", b);
}
int main()
{
value();
printf("Calling function 2nd time \n");
value();
printf("Calling function 3rd time \n");
value();
return 0;
}
OUTPUT
The value of local variable: 20 The value of Static variable: 30 Calling function 2nd time The value of local variable: 20 The value of Static variable: 40 Calling function 3rd time The value of local variable: 20 The value of Static variable: 50
NOTE: In the above program the value of the static variable is increasing by 10 every time when the function is called. While the value of the local variable is the same as the function call.
You can also explore: Ternary Operator in C: Syntax, Examples and Advantages
Auto Variable
All variables declared in C programming are automatic by default. You can use the auto keyword to declare the automatic variable. An automatic variable is a local variable whose lifetime is within the code only.
Its default value is garbage.
Syntax:
auto data_type variable_name = initial_value;
Or
data_type variable_name = initial value;
Program to use automatic variable in C
#include <stdio.h>
void value(){ int a = 10; //local variable auto int b = 20; //automatic variable printf("The value of local variable: %d \n", a); printf("The value of automatic variable: %d \n", b); } int main() { value(); //calling function return 0; }
OUTPUT
The value of local variable: 10 The value of automatic variable: 20
Register Variable
Register variable uses register keyword before its definitions and it is stored in the CPU register. It is a local variable. These variables have fast processing. The default value of the register variable is garbage. Its lifetime is till the end of the function.
You can also explore: Understanding Logical Operators in C
Syntax:
register data_type variable_name = initial_value
Program to declare register variable
#include <stdio.h>
int main()
{
register int a = 30; // Declaring register variable
printf("The value of register variable is %d", a);
return 0;
}
OUTPUT
The value of register variable is 30
Extern Variable
The extern variables use the extern keyword before the variable definition. This enhances the variable visibility, and the variable can be used in different C files. It is a global variable. It is not necessary to initialize the variable at the time of its declaration. Its lifetime is the entire program.
Syntax:
extern data_type variable_name;
Example: extern int a;
Constant Variable
The constant variables are special variables in C whose value does not change throughout the program after initialization. It is a read-only variable in C. Initializing a constant variable at the time of variable declaration is mandatory. It uses the “const” keyword before its definition.
Syntax:
const data_type variable_name = initial_value;
We can initialize other variables anytime and anywhere in the program and can also change their value. But constant variables have a fixed value, you cannot change their value after their initialization. If you try to change the value of a constant variable, the program will generate errors.
You can also explore: C Input Output: printf() and scanf()
Program to change the constant variable value.
#include <stdio.h>
int main()
{
int a = 30;
// Declaring constant variable
const int b = 10;
printf("Original value of const variable b: %d\n", b);
// Changing value of constant variable using a pointer
int* ptr = (int*)&b;
*ptr = 20;
printf("Modified value of const variable b: %d\n", b);
return 0;
}
OUTPUT
Original value of const variable b: 10 Modified value of const variable b: 10
You can also explore: Difference Between Array and Structure
Conclusion
In this article, we understand various types of variables in C programming. We also understand the scope and lifetime of variables in C.
Author: Sonal Meenu Singh
FAQs
What are the scope and lifetime of variables?
The scope of a variable is the area in which a variable is accessible. You cannot use a variable beyond its scope. In C programming, the scope of variables is local and global. A lifetime of a variable is the working time of a variable; till its lifetime a variable holds a memory place. The lifetime of a variable is static and automatic.
What is lifetime of variables in C?
A lifetime of a Variable in C is the time up to which a variable holds its memory. The lifetime of a static variable is the code in which it is declared. The lifetime of an automatic variable is when it is used, else it is deactivated.
What is variable and its types?
Variable is a container in C programmer to hold values. They hold the memory as per the type of variable. In C there are 3 types of variables: int, float, and double. Syntax of variable: data_type variable_name = initial_value.
What are C variables?
C variables are special placeholders of the initial values of a particular data type. Each variable has a unique name and scope. There are local and global variables whose scope is different from each other. The other C variables are static, automatic, register, and extern.u202f
What are the 3 main data types?
The data types in C are int, double, and float.
What is the scope of a variable in C?
The scope is the area in which a variable can be used or accessed. There are 2 scopes for C variables. Local and Global. A local variable has its scope to the function only, in which it is declared. A local variable cannot be used outside the defined function.u202f The scope of global variables is throughout the program and is declared outside the function. They can be used in any function.
What is constant vs static in C?
A constant variable is one whose value cannot be changed once it is initialized. The compiler will generate an error if you try to reinitialize a constant variable. Static variables are declared in a function only and you can change their value anytime.
Is lifetime a continuous variable?
Lifetime is not a variable in C programming, it is the active time of the variable up to which a variable can hold the memory space.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio