Last Updated on January 9, 2024 by Ankit Kochar
Understanding variables in C programming is fundamental as they serve as containers to store data within a program. Variables possess various data types and hold different values, playing a crucial role in manipulating and managing information in a program. This article aims to elucidate the concept of variables in C, explaining their declaration, types, and usage, providing a comprehensive guide for beginners and enthusiasts alike.
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
In conclusion, mastering the concept of variables in C programming is pivotal for developing efficient and functional code. Through this article, you’ve learned about the significance of variables, their types, declaration methods, and how they facilitate data storage and manipulation in C. By practicing and experimenting with different variable types and declarations, you’ll enhance your proficiency in C programming and empower yourself to create robust and versatile applications.
Frequently Asked Questions Related to Variables in C
Here are some of the frequently asked questions about variables in c.
1. What happens if you declare a variable without initializing it in C?
If a variable is declared without initialization in C, its value will be indeterminate and may contain garbage or unpredictable data until explicitly assigned a value.
2. How do you declare a variable in C?
In C, you declare a variable by specifying its data type followed by the variable name. For example, int num; declares an integer variable named "num".
3. What are the basic data types for variables in C?
C supports basic data types such as int, char, float, double, and void. These types vary in size and hold different kinds of data.
4. Can you change the value of a variable once it’s declared in C?
Yes, you can change the value of a variable after it’s declared by assigning a new value using the assignment operator (=).
5. Are variable names case-sensitive in C?
Yes, variable names in C are case-sensitive, meaning uppercase and lowercase letters are distinct.
6. What is the scope of a variable in C?
The scope of a variable in C refers to the part of the program where the variable is accessible. Variables can have local scope within a function or global scope accessible throughout the program.
7. How do you initialize a variable during declaration in C?
You can initialize a variable during declaration by assigning a value to it, for instance, int x = 10; initializes an integer variable "x" with the value 10.
8. Can a variable name start with a number in C?
No, in C programming, a variable name cannot start with a number; it must begin with a letter or an underscore.
9. What is the difference between local and global variables in C?
Local variables are declared within a function and can only be accessed within that function, while global variables are declared outside all functions and can be accessed by any function in the program.