Clouds
Home
Tutorial of C language
Computer Language
Introduction to C
Programming in C
Structure of C program
Variable in C
Datatypes in C
Token in C
Operator in C
Precedence of C Operator
Assignment Operator in C
Logical Operator in C
Bitwise Operator in C
Ternary Operator in C
C Input Output
Pointers in C
Storage Classes of C
Control Structure of C
C Decision Making Statement
C Looping Statement
C Jumping Statement
C Function
Function Call By Value
Function Call By Reference
C Input Output Function
C Structure
Array in C
Union in C

Variable in C

  • Variable is a name location in the memory used to store data that can be manipulated during run time
  • A variable may consists of letters, digits and underscore character as defined in the identifiers and should be chosen as to reflect its function or nature in the program.
  • Some examples of variables are :- (total, TOTAL, radius, area, sum, volume etc.).
  • Some invalid examples are :- (1num, total, net salary etc.).
  • The variables name should not be a keyword.
  • Space is not allowed in variable name.
  • Uppercase and lowercase letters are significant i.e. in the above example ‘total’ is not same as ‘TOTAL’.

    Declaration of variables

    A variable can be used to store a value of any data type, that is the name has nothing to do with its type.
    Syntax:- datatype var1, var2, var3,....,varn;
    Where var1, var2, var3,..., varn are the names of the variables and are separated by commas.
    A declaration statement must end with a semicolon.

    Example

    int marks1, total;
    float radius, volume;
    char name;
    double ratio;

    Where int, float, char, double are keywords to represent their types.

    Assigning values to variables

    Values can be assigned to variables using the assignment operator '=' as follows:
    Syntax :- Variable_name = constant;

    Where variable _name is defined by its type. 'C' permits multiple assignments in one line as:
    Example: - value = 0, sum = 50;

    It is also possible to assign a value to a variable at the time, the variable is declared.
    Syntax:- datatype var1=constant;

    Example:-
    int var1 = 100;
    char name = 'A';

    The process of giving initial values to variables is known as initialization.

    The Programming language C has two main variable types

  • Local Variables
  • Global Variables

    Local Variable

  • A variable declare within opening and Close Curly braces is called local variable
  • Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
  • When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
  • When execution of the block starts the variable is available, and when the block ends the variable 'dies'. Example:- #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a; /* Local Variable */
    clrscr();
    a=5 ;
    printf(("%d",a);
    getch();
    }

    Note: 'a' is a local variable. The scope of ’a’ is opening braces to close curly braces.

    Global Variable

  • The variable declared outside of any function they can be used in whole program is called global variable.
  • Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.
  • Global variables are initalised automatically by the system when you define them!.
  • If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.

    Example: #include<stdio.h>
    #include<conio.h>
    int a; /*Global Variable */
    void main()
    {
    clrscr();
    a=5;
    printf("%d",a);
    getch();
    }

    #include<stdio.h>
    #include<conio.h>
    int a; /*Global Variable */
    void main()
    {
    clrscr();
    printf("Enter the value of a: ");
    scanf("%d",&a);
    printf("%d",a);
    getch();
    }

    Note: ‘a’ is a global variable. The scope of ‘a’ is anywhere in the program.
    Datatypes in C >>
    Token in C >>
    Operator in C >>
    Precedence of C Operator >>
    Assignment Operator in C >>


    Home Clouds