Variables in C

Programming languages or concepts
0

 

Variables in C:

A variable is a name for a piece of memory in the Random Access Memory (RAM) which can used to store and access the data. The value stored in the allocated memory may change as the program execution progresses so called variable.
 Variable is used to store the value.
A variable in simple terms is a storage place which has some memory allocated to it.  As name indicates it value can be changed or also it can be reused many times.

Syntax:-
            Data type  Variable_name;
 eg. 
         int a;


Types of Variable in c:-

1) Local variable
2) Global variable
3) Static variable
4) External variable
5) Automatic variable


1) Local variable:-

         A variable which is declared inside the function is known as local variable.
       It is used only inside the program. A variable that is declared and used inside the function or block is called local variable. 
It’s scope is limited to function or block. It cannot be used outside the block.Local variables need to be initialized before use. 

Example:
                #include <stdio.h>
                void function() 
                {
                      int x = 10; // local variable
                }
 
                int main()
                 {
                      function();
                  }

2) Global variable:-

              It is declared at the starting of program. 
      A variable is declared outside the program is known as global variable.
 Any function can change the value of the global variable. It is available to all the functions.
    Global variable is used all over the program.

For Example :

        int value=20;//global variable  
        void function1()
                {  
                    int x=10;//local variable  
                }  

3) static variable:-

     static is a keyword . Static variable is declared using static keywords. A variable that retains its value between multiple function calls is known as static variable. 

For Example :
            void function1()
            {  
                int a=20;//local variable  
                static int b=1;//static variable  
                a=a+1;  
                b=b+1;  
                printf("%d,%d",a,b);  
            }  

4) External variable:-

     We can share a variable in multiple C source files by using external variable.
     It is declared using external keyword.  A user will be capable of sharing a variable in multiple numbers of source files in C if they use an external variable. If we want to declare an external variable, then we need to use the keyword extern.

Syntax

extern int a=10;// external variable (also a global variable)

Example:

#include "myfile.h"  
#include <stdio.h>  
void printValue(){  
    printf("Global variable: %d", global_variable);  
 
        

5) Automatic variable:-

    Variable which is declared inside the block is known as Automatic variable by default. All variables in C that are declared inside the block, are automatic variables by default. Every variable that gets declared inside a block (in the C language) is by default automatic in nature. One can declare any given automatic variable explicitly using the keyword auto.

Example:
                #include <stdio.h>
                void func()
                {
                      int a=15;//local variable (also automatic) 
                      auto int b=2;//automatic variable
                 }
                int main()
                 {
 
                    func();
                    return 0;
                  }

Rules to declare variable in C:-

In we can declared variable for declared variable we need to follow rules and regulations.
This Rules and Regulations are listed  below


  • In c Every Variable name should start with alphabets or underscore.
     Ex. int alph;.         
     Ex. int _underscore;

  • In variable declared spaces are not allowed.
  • Keywords are not allowed as a variable.
  • Every variable name always should exist in the left hand side of assignment operators.
  • Length of variable is depending on compiler and  operating system but maximum size of variable is 8.
  • Information middle of variable declaration special symbols are not allowed except underscore (_).

Variable declaration in c

Variable declaration is a process to allocate required memory space for the data in form of variable.

Syntax for declaration of variable
     Data type variable_name;

Ex.
        int a;

Post a Comment

0Comments

Post a Comment (0)
close