What is Variable in C and How to Declare?

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.

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:

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:

  1. Local Variables
  2. Global Variables
#include void sample() < int x = 18; printf("%d", x); >int main()

Output

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.

#include int x = 22; void f1() < printf("f1 1: %d\n", x); >void f2() < printf("f2: %d\n", x); >int main()

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.

  1. Static Variables
  2. Automatic Variables
  3. Extern Variables
  4. Register Variables
static data_type variable_name = initial_value;
Let’s understand static variables in c with the help of the example #include 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()

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.

auto data_type variable_name; or data_type variable_name; 
Now let’s look at the example of automatic variable #include void function() < int x = 7; auto int y = 24; printf("Auto Variable: %d", y); >int main()

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.

extern data_type variable_name;
sample.h extern int x=17; program1.c #include "sample.h" #include void printValue()

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 data_type variable_name = initial_value;
Code Implementation #include int main()

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.

const data_type variable_name = value;
Code Implementation #include int main()

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.