C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. Instead of remembering the complex address where we have stored our information, we name that address. The naming of an address is known as variable.Each variable in C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
Rules for naming C variable :
- Variable name must begin with letter or underscore.
- Variable name must not start with a digit.
- Variable name can consist of alphabets, digits and special symbols like underscore .
- Blank or spaces are not allowed in variable name.
- Keywords are not allowed as variable name.
- No special symbols are allowed other than underscore.
- sum, length, _value are some examples for variable name.
Based on the basic types explained in the previous chapter, there will be the following basic variable types -
Type | Description |
---|---|
char | Typically a single octet(one byte). This is an integer type. |
int | The most natural size of integer for the machine. |
float | A single-precision floating point value. |
double | A double-precision floating point value. |
void | Represents the absence of type. |
Declaration and Definition of a Variable :
Declaration of variables must be done before they are used in the program. Declaration does two things.
- It tells the compiler what the variable name is.
- It specifies what type of data the variable will hold.
Definition of variable means to assign a value to a variable. Definition specify what code or data the variable describes. A variable must be declared before it can be defined.
There are three types of variables in C program, they are :
- Local Variable
- Global variable
- Environment Variable
Local Variable :
- The scope of local variables will be within the function only.
- These variables are declared within the function and can't accessed outside the function.
Global Variables :
- The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
- This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.
Environment Variables :
- Environment variable is a variable that will be available for all C applications and C programs.
- We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.
- The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.
Example :
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
Output:
value of a = 10, b = 20 and g = 30