C Variables
Variables are names to refer memory locations. Values that we make use of in a program are stored in memory locations. Variables provide a means or provide an abstraction to access those memory locations.
Generally, every variable in a C program can have a specific data type. The data type may be primary (or) derived (or) enumerated data type. We shall discuss about these datatypes in detail in our next tutorial. We shall deep dive into basic concepts about C Variables in this tutorial.
Need of Variables
For solving a problem, you first write an algorithm or pseudo code and then try to implement it. In most of the cases, there would be one or more values that traverse through the algorithm. C programming provides the concept of variables to store them and provide the abstraction that we usually get in a pseudo code.
Rules for naming a variable
C programming language imposes following rules in naming a variable.
- Name of variable should start with either letter (or) underscore.
Examples : counter , _counter are valid variable names. - Name of variable can have letters, digits and underscore.
Examples : counter_1 , counter2 are valid variable names. - C language is case sensitive. So, a variable with same name in lower case and upper case letters are different.
Example : SALARY , salary and Salary represent three different variables. - No special symbols other than underscore are not allowed.
Examples : counter@1 , money$ are not valid. They contain special characters that are not allowed to declare a C Variable. - C keywords cannot be used as variable names in C programming.
Examples : int , char , enum etc., are reserved as they are keywords in C programming. Hence you cannot use them for variable names. You can use chaR , Int , etc., because C programming is case sensitive.
Types of variables
Variables are categorized based on its scope or lifetime. Types of variables are :
- Local variable
- Global variable
- Environmental variables
An elaborate explanation about these types is provided in our next tutorial.
Declaring a C Variable
Variable must be declared before it is used in the program. Declaration of variable will tell the C Compiler that :
- it has to be considered as a variable
- the variable can be accessed using the name provided
- the variable holds data of a specific type (like char, int or float etc.)
During declaration of variable compiler doesn’t worry about the memory allocation. So, programmer has to take care of the memory usage by the variable. If the variable exceeds the memory available for program execution, then it causes run-time error.
A variable can be declared outside the main function or any other function. To use such variables in other C programs, the variable has to be declared accompanying a keyword called extern.
extern int a;
Defining a C Variable
Defining a variable tells the C Linker that it has to assign a storage space to a variable and it will be used in the program.
We can define several variables of same data type in a single line by using comma operator (,) as separator.
Examples
int a;
float salary;
char grade;
Initializing a variable
Initializing a variable means assigning a value to the variable that was already defined.
Example
int a; //defining a variable
a=10; //initializing a variable
Declaration and initialization of a variable can be done in a single statement. int a=10;
Example
In the following example, we have declared global variables, local variables and initialized these variables.
C Program
#include <stdio.h>
/* global variable declaration */
extern int a, b;
extern int c;
extern float f;
int main () {
/* local variable declaration */
int a, b;
float f;
/* variable initialization */
a = 30;
b = 20;
/* declaration and initialization */
int c = 0;
c = a - b;
printf("value of c : %d \n", c);
f = a/b;
printf("value of f : %f \n", f);
return 0;
}
When the above code is compiled and executed, it produces the following result.
Output
value of c : 10
value of f : 2.5
Conclusion
In this C Tutorial, we have learnt declaration, definition and initialization of a variable in C programming language. We shall learn scope of variables in the next tutorial.