Variables in C

What is Variable in C?

A variable is a logical name (given or assigned by the programmer) for the physical location in a computer memory, by which we can access the physical memory location such as we can store data, retrieve data from that location. In C programming every variable has its own data type

For example: int a, float b, char c; here a, b and c are the name of the variables and int, float and char are the data types for those variables.

Why variables is used in C programming?

In computer programming whatever we want to do we need to access memory (RAM) every time, So to access memory we have to access it by addresses because every byte of the memory has a unique address which is known as physical address (like f0f4) identifies a location in memory. This physical addresses are very difficult to remember. Variables are the logical name given by the programmer which refers to a physical location in the memory which is very easy to remember, this is why variables are used.

What are the rules for constructing variables?

  1. Variable name must start with alphabet.
    Ex – var (right)
            34var (wrong)
  2. No special symbol other than the underscore can be used.
    Ex – var_1 (right)
            var-1 (wrong)
  3. No comma or blank space can be used in variable name.
    Ex – var1 (right)
            var, 1 (wrong)
  4. It can contain digits but not as starting.
    Ex - var (right)
           34var (wrong)
  5. No keywords can be used as variable.
    Ex – var (right)
            int (wrong because it is keyword)
  6. variables are case sensitive, means var and VAR are different from each other.
  7. The length of the variable varies compiler to compiler(min 1 character)

How to declare variable in C or What is variable declaration?

Declaring a variable means declaring the datatype of the variable, that is what type of data the variable can contain such as a variable can contain integer value, floating value, character value and so on. The declaration of variable helps the compiler to allocate memory for those variables with appropriate memory size. Following are the examples of variable declaration in c:

Ex – int intVar; // reserve 2 bytes of memory for variable intVar which can store integer value only like 23454.
        float floatVar; // reserve 4 bytes of memory for variable floatVar which can store floating value only like 234.54
       char charVar; // reserve 1 byte of memory for variable charVar which can store a character only like 'a'.

What is scope of a variable in c?

Scope of a variable means where the variable is available withing a program. Because sometimes we may need to make a variable scope limited to a specific portion of a program and sometimes may need to make it available to the whole program. So variables can have two types of scope – local scope and global scope.

What is Local scope or Local variable in C?

A local variable is a variable which is only accessible to the block in which it is declared. Following is the example program to demonatrate local variable:


#include  
void main(){
	int i=5; //scope of variable i is only within the opening and closing braces of main() function
	printf(“Value of i = %d”,i);
	display();
}
int display(){
	printf(“Value of i = %d”,i); // Here the variable i is not accessible.
}

What is Global scope or global variable in C?

A global variable is a variable which is accessible within the whole program. Following is the example program to demonatrate global variable:


#include  
int i=5; //The variable i is available through the whole program. So it is a global variable
void main(){
	printf(“Value of i = %d”,i);
	display();
}
int display(){
	printf(“Value of i = %d”,i); // Here the variable i is accessible.
}
15:00
Check Page Rank