How to Declare and Initialize Variables in C

Variable:- In simple words variable is the name given to the memory location where costant value is stored. Means variable is an identifier assign to the memory location where data is stored.


    Rules For Declaring C Variables:

  • Variable names must begin with letter or underscore.
  • Variables are case-sensitive and they can be constructed with digits, letters.
  • We cannot use special symbols other than underscore.
  • We cannot start variable name with number.
   For Example:    int 1a, 2b;

   Right way is:     int a1, b2;



    How to declare the variables:


Syntax:

datatype variable_name;
{
     // Block of code
}


For Example:

int a,b,c;// here int is datatype and a,b,c are variables.

Let's understand with another  example's:

float x,y; // here float is datatype and x,y are variables.

char name; // here char is datatype and name is variable name.



    How to initialize the variables in C:


Syntax:

datatype variable_name = value;


For Example:

int num = 10; // here, int is datatype, num is variable and 10 is value.

float a = 5.4; // here, float is datatype, a is variable_name and 5.4 is value.


char gender = 'M'; // here, char is character datatype, gender is variable_name and M is value.


No comments:

Post a Comment

If you have any doubts, please discuss here...👇