C – Variables and Rules

Variables:

  • Variable is an Identity given to memory location.
  • Variable is used to store information.
  • We need to specify the datatype of every variable.
Syntax:
            datatype identifier = value;
 
Examples:
            char name[] = “amar”;
            int age = 23;
            char* mail =  “amar786@gmail.com”;
            char gender = ‘M’;
            double salary = 35000;

Note: Value of a variable will change. For example “age of a person”

Variable Declaration, Assignment, Modify and Initialization:

Declaration:

  • Creating a variable without value.
  • For example
    • int a ;

Assignment:

  • Assigning a value to the variable which is already declared.
  • For example,
    • int a ;  // declaration
    • a=10 ; // assignment

Modify:

  • Increase or decrease the value of a variable
  • For example,
    • int a ;  // declaration
    • a=10 ; // assignment
    • a=a+20; //modify

Initialization:

  • Defining a variable along with value
  • For example,
    • int a = 10 ;  // initialization
    • a = a+20 ; // modify

Rules to create variable:

  • Variable contains alphabets(A-Z , a-z) or Digits (0-9) and only one symbol( _ ).
  • Variable should not starts with digit
    • int  5a = 10;  (error)
    • int  a5 = 10;
    • int 1stRank; (error)
  • Variable should not contain spaces. 8355832471
    • int acc num = 1001; (error)
    • int acc_num = 1001;
  • Variable should not allow other symbols except _
    • int  acc_num ;
    • int  acc-num;
  • variable can starts with special symbol _
    • int  _acc_num;
    • int  _1rank;
  • Variable should not match with keywords.
    • int  if = 10 ;
    • int  else = 20 ;

Variables classified into:

  • Local variables:
    • Defining a variable inside the block or function.
    • We cannot access local variables outside to the block.
  • Global variables:
    • Defining a variable outside to all functions.
    • We can access global variable from all functions.

Note: We will discuss briefly about Local and Global variables after Functions concept

Scroll to Top