Last Updated on March 30, 2023 by Prepbytes
We can use any programming language that we want but there is a topic that is common in all the languages and that is variable. C programming language is one of the most widely used programming languages. Variables in c are used to store any data. We can refer to any memory location using the variables. Variables in c are one of the most basic and important topics that you need to know to perform further operations in the C language. While proceeding further in this article we will discuss all variables in c, the variable definition in c, the variable declaration in c, and much more.
What is a Variable in C?
Variables in C are nothing but a name we give to a memory location that is used to store data or value. We can understand it as a container that stores the data. Variables in c can store different types of data like integers, characters, floating point numbers, strings, etc. We can use the variables in c to represent data or values that can be used throughout the program. The value of the variables in c can be changed or modified during the execution of the program.
Variable Declaration in C
In this section, we will see the variable definition in c. The general syntax of variable declaration in c.
data_type varible_name=val;
The above syntax has three aspects.
- Variable declaration: This will tell the compiler about the existence of the variable. In this, we will define the data type of the variable here the variable does not been allocated with any memory.
- Variable Definition: Here we will name the variable that we want to use in the program. Variables in c will be allocated with some memory after variable definition. Initially, it will store the garbage value until it is initialized with any value.
- Variable Initialization: As the name suggests here the variables in c will get assigned some value.
Rules for Declaring Variables in C
There are certain rules that you need to follow while working with variables in c.
Some of them are mentioned below:
- It is preferred to declare variables at the start of the block like functions, loops, etc.
- You have to use the underscore to separate words in variable names.
- All of the declaration statements must end with a semicolon.
- Use const for the variables that need not be modified.
- The variable name should not start with a number.
- The same name variables should not be declared in the same scope.
- It is advised to give meaningful names to the variables.
- It is preferred to declare all the variables belonging to the same data type in the same line.
Purpose of Variable Declaration in C
Variable declaration is a technique used in the C programming language to declare a variable and allot memory for it. Data that can be changed and accessed during program execution is stored in variables.
Before a variable is used in a program, it must be declared in C so that the compiler is aware of its name and data type. This ensures that the variable is used correctly in the program and aids the compiler in allocating memory space for the variable.
Types of Variables in C
We can classify variables on various basis like on the basis of scope, on the basis of a lifetime, on the basis of memory, etc.
Classifications in the basis of Scope
Variables in c are classified on the basis of scope. The scope can be referred as the region in which the variable is able to perform its operation, in simple words where the variable exists. Beyond the scope, we cannot access the variable as it is out of scope.
There are two types of variables in c on the basis of scope and they are:
- Local Variables
- Global Variables
-
Local Variable
They are nothing but the variables that are declared within a block or a function of code. They cannot be accessed outside of the block in which they are assigned and their scope is limited to that block or function only.Let’s understand this with the example
#include <stdio.h> void sample() { int x = 18; printf("%d", x); } int main() { sample(); }
Output
18
Explanation of the above example
In the above example we have declared a local variable in the function sample with the name x and the function prints the variable hence the answer is 18, i.e. the value of the local variable declared. -
Global Variable
Unlike local variables they are declared outside the function or block hence they can be accessed from anywhere in the program hence their scope is the whole program.Let’s understand this with the help of an example.
#include <stdio.h> int x = 22; void f1() { printf("f1 1: %d\n", x); } void f2() { printf("f2: %d\n", x); } int main() { f1(); f2(); return 0; }
Output
f1: 22 f2: 22
Explanation of the above example
In the above example we have declared a global variable and have tried to access it in different functions and it gives the answer without any error.
Classifications on the Basis of Storage Class
Storage class can be understood as a concept that tells us to determine lifetime, scope, memory location, and the default value of the variables in c.
There are mainly 4 types of variables in c based on storage class.
- Static Variables
- Automatic Variables
- Extern Variables
- Register Variables
-
Static Variables
These are the types of variables that are defined by static keywords. We can only define these types of variables only once and on the basis of the declaration, their scope can be local or global. Their default value is 0.Syntax of Static Variable
The syntax of static variable is given below:static data_type variable_name = initial_value;
Let’s understand static variables in c with the help of the example
#include <stdio.h> void function() { int x = 22; static int y = 48; x = x + 10; y = y + 10; printf("\tLocal: %d\n\tStatic: %d\n", x, y); } int main() { printf("1st Call\n"); function(); printf("2nd Call\n"); function(); printf("3rd Call\n"); function(); return 0; }
Output
1st Call Local: 32 Static: 58 2nd Call Local: 32 Static: 68 3rd Call Local: 32 Static: 78
Explanation of the above example
As you can see in the above example the value of the local variable does not change with any function call but the value of the static variable is printed incremented every time. -
Automatic Variables
They are known as auto variables. All the local variables are automatic variables by default. Their lifetime is till the end of the bock and the scope is local. We can use the auto keyword to define an automatic variable and their default value is the garbage value.Syntax of Automatic Variable
Syntax of automatic variables is given below:auto data_type variable_name; or data_type variable_name;
Now let’s look at the example of automatic variable
#include <stdio.h> void function() { int x = 7; auto int y = 24; printf("Auto Variable: %d", y); } int main() { function(); return 0; }
Output
Auto Variable: 24
Explanation of the above example
In the above example both the variables are automatic variables but the only difference is that y is declared explicitly. -
External Variable
These are the types of variables that can be shared between multiple files. We can use this variable to share the data between any file just have to include the file in which the external variable is declared.Syntax of External Variable
The syntax of the external variable is given below.extern data_type variable_name;
sample.h extern int x=17; program1.c #include "sample.h" #include <stdio.h> void printValue(){ printf("Global variable: %d", x); }
Explanation of the above example
In the above example we have declared x as an external variable and can be used in multiple files. -
Register Variable
They are the variables in c which are stored in CPU registers rather than the RAM. They have the local scope and be only valid till the end of function or block.Syntax of Register Variable
register data_type variable_name = initial_value;
Code Implementation
#include <stdio.h> int main() { register int var =4896; printf("The value of Register Variable: %d\n", var); return 0; }
Output
The value of Register Variable: 4896
Explanation of the above example
In the above example we have declared an register variable and printed the value of that variable. -
Constant Variable
The constant variable differs from the rest of the above-mentioned variables as we can change the value of other variables any number of times but we cannot change the value of the constant variable once it is initialized. It is only a read-only variable. We can define it using a constant keyword.Syntax of Constant Variable
The syntax of the constant variable is given below.const data_type variable_name = value;
Code Implementation
#include <stdio.h> int main() { int not_constant; const int constant = 25; not_constant = 47; constant = 28; return 0; }
Output
prog.c: In function ‘main’: prog.c:10:11: error: assignment of read-only variable ‘constant’ constant = 28; ^ prog.c:7:12: warning: variable ‘constant’ set but not used [-Wunused-but-set-variable] const int constant = 25; ^~~~~~~~ prog.c:5:6: warning: variable ‘not_constant’ set but not used [-Wunused-but-set-variable] int not_constant;
Explanation of the above approach
In the above approach we have declared a constant variable and when tried to change its value we got the above error.
Need of Variables in C
In the C programming language, variables are a fundamental component of any program. They are used to store data in the computer’s memory, allowing for the manipulation and reuse of values during runtime. Variables are essential for performing arithmetic, logical, and other manipulations on data stored in memory, and they enable you to dynamically allocate memory and pass values between functions. In C, variables are used to store a wide range of values such as numbers, characters, and strings, and they allow you to reuse values multiple times without having to type them out repeatedly. Variables are an essential tool in creating efficient and functional C programs and mastering their use is a crucial step toward becoming a proficient C programmer.
Conclusion
Variables are an essential concept in C programming language, and they are used to store data and values in the computer’s memory. C programming language supports various types of variables, including integer, floating-point, character, and string variables. The scope of a variable in C determines where it can be accessed or used within a program, and there are two types of variable scope: global and local. Understanding the concept of variables in C is crucial for writing effective and efficient programs in the language.
Frequently Asked Questions
Here are some of the frequently asked questions about variables in c.
1. What is the difference between a constant and a variable in C?
A constant is a value that cannot be changed during the execution of the program, while a variable is a named location in the memory that can hold a value or data that can be changed during the execution of the program.
2. How do you modify the value of a variable in C?
To modify the value of a variable in C, you need to assign a new value to it using the assignment operator (=).
3. What is a pointer variable in C?
A pointer variable in C is a variable that holds the memory address of another variable.
4. How do you access the value of a variable using a pointer in C?
To access the value of a variable using a pointer in C, you need to use the asterisk (*) operator before the pointer variable name.
5. What is the sizeof operator in C?
The sizeof operator in C is used to determine the size of a variable or data type in bytes.