21
NovVariables in C Programming - Types of Variables in C ( With Examples )
Variables in C: An Overview
Are you curious about using variables in C? Using the right variables can not only make the code easier to read but also help optimize your program. If you're looking to deepen your understanding of variables and enhance your C programming skills, consider enrolling in C Online Training. In this article, we'll explore all the aspects of a variable. By utilizing the insights gained from C for beginners you will be able to create powerful programs that efficiently utilize memory resources.
Variables in C language
A variable is a named storage location of data in memory. Data types of variables can be int
, float
, boolean
, char
, and double
.
How to Declare a Variable?
Syntax
type variableName;
Example
float f;Here we can see that a float
type variable f
is created. This means that a memory location of name f
which can store floating value is being created in the memory.
You can assign the value to the variable f
after a declaration like this:
f=15.7;
You can also declare a variable by assigning values to it in the following way:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
What are the rules for naming a variable?
In C programming language, there are rules for defining variables, which are as follows:
- The name can consist of
alphabets
(both uppercase and lowercase),digits
, andunderscore(_)
. - The first character must be either an
alphabet
or anunderscore
. - Variable names are case-sensitive, i.e., "
num
" and "Num
" are two different variable names. - No
white spaces
and special characters like!
,*
, must be there in the variable names. - A
keyword
cannot be your variable name. - The type of the declared variable cannot be changed after declaration.
Read More - Top 50 Mostly Asked C Interview Questions
Format Specifiers
They convey to the compiler regarding the type of data value stored in a variable. They are used with the printf()
function. The representation of it is, a %
sign followed by a character in double quotes,””
.
Example in C Compiler
#include <stdio.h>
int main()
{
int a = 15; //integer
float f = 5.99; // Floating point number
char c = 'D'; // Character
// Print variables
printf("%d\n", a); // prints 15
printf("%f\n", f); // prints 5.99
printf("%c\n", c); //prints D
return 0;
}
Here, we are printing values of various types of variables.
Output
15
5.990000
D
Operations that can be performed on variables
- Assigning the value of one variable to another variable
Example
#include <stdio.h>
int main()
{
int a = 15;
int b = 20;
a = b; // Assign the value of 'b' to 'a'
printf("%d", a);
return 0;
}
Here we first declared a
and b
with values. Then we assigned the value of b
to a
.
Output
20
Example
#include <stdio.h>
int main()
{
int x = 5, y = 6, z = 50;
printf("%d\t%d\t%d", x, y, z);
return 0;
}
Here we have declared 3 int
type variables in a single line separated by commas. In the printf()
function “\t
” is used for giving spacing between 2 values in the same line.
Output
5 6 50
Example
#include <stdio.h>
int main()
{ int a = 5;
int b = 10;
int sum = a + b;
printf("%d", sum);
return 0;
}
Output
15
Types of Variables in C Language
There are 5 types of variables in C language, which are
- Local Variable
- Global Variable
- Static Variable
- Automatic Variable
- External Variable
- Local Variable in C
Local variables are declared and initialized at the start of a function or block and allocated memory inside that execution scope. The statements only inside that function can access that local
variable. Such variables get destroyed when the control exits from the function.Example
#include <stdio.h>
void local();
void main() {
int a = 22, b = 44;
local();
printf("Values in the main() function a = %d and b = %d\n", a, b);
}
void local() {
int a = 50, b = 80;
printf("Values in the local() function a = %d and b = %d\n", a, b);
}
In the above code in the C Online Editor, there are two functions, main()
and local()
. The variables a
and b
are local
variables declared inside each function with different values. The value of a
and b
is restricted to the function in which it is declared.
Output
Values in the local() function a = 50 and b = 80
Values in the main() function a = 22 and b = 44
- Global Variable in C
The global variable in C is defined outside of any function and is therefore accessible to all functions. Global
variables in C are initialized when the program starts, and until the program exits, these variables remain stored in the memory.
Example
#include <stdio.h>
int a = 20; // Global declaration
void global();
void main() {
printf("In main() function a = %d\n", a); // Prints 20
global();
a = a + 15; // Uses global variable
printf("In main() function a = %d\n", a); // Prints 55
}
void global() {
a = a + 20; // Uses global variable
printf("In global() function a = %d\n", a); // Prints 40
}
In the above code, a global
variable, a=20
is declared. It is accessed by both the functions, global()
and main()
. The modification of a
by the global()
function affects the value of a
in the main()
function as well. The global()
function opeartes on the modified value of a
.
Output
In main() function a = 20
In test() function a = 40
In main() function a = 55
- Static Variable in C
Static
variables enable data to remain after the function has been executed and returned; this allows the function to remember a value from a previous call, making it both efficient and powerful. We will learn in detail about static
variables and functions in the section, Boolean and Static in C- Automatic Variable in C
All the local
variables are automatic
variables by default. They are also known as auto
variables.
You will get to know automatic
variable in detail in the section, Storage Classes in C.
Example
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
- External Variable in C
External
or global
variables are declared using the ‘extern
’ keyword. You can declare an external variable any number of times but, the value can be assigned only once. Their default value is 0
or null
.External
variables specify an external linkage and so such variables are not allocated any memory. The initialization must be done globally and not within any function or block. If an external
variable is not initialized anywhere in the program, the compiler will show an error.
Read more: Storage Classes in C: Auto, Extern, Static, Register
Example
#include <stdio.h>
int a;
int main()
{
extern int a; // variable a is defined globally, the memory will not be allocated to a
printf("%d",a);
}
Output
0
Summary
Becoming familiar with variables
in C is essential for creating successful programming solutions. Understanding how to declare and use a variable correctly will help developers structure their code correctly. Experimenting with different techniques and researching the different types of variables can also give valuable insight into how each type of variable behaves. Take the time to practice and understand variables in C, and consider pursuing a C Certification to demonstrate your skills to potential employers.