variables

c programming variables

Definition

c programming Variables are a meaningful name used to store a value of the supported data type of C language. When we create a variable we have to define what type of data it will store.

The Syntax is as follows:

data type variable_name;

OR

data type variable name = value;

Examples:

int salary;  // variable declaration

int salary = 30000;  // variable initialization

float height = 5.6;  // variable initialization

in example int tell that the variable salary will contain integer data-type.

The  equal sign (=) is used to assign a value into a variable. This equal sign is called assignment operator in C language.

We can store the result of any equation in variable, but result of equation should match the data type of variable.    

 int basic = 2000;

int salary = basic + 10000;

Declaration  variable

c programming Variables must be declared before they are used. This rule is followed so that the compiler recognizes your variable that is declared by you. A variable name must be declared by us. A variable  is declared with the type of value that it will hold. Hence there are two important things to remember while declaring a variable

  1. First we should write the type of the variable such as int, float etc…
  2. Second we should write the name of the variable.

Example:-

c programming variables
c programming variable

 

In the first example, x is a variable of int type which means that x will hold integer type of variable only. In the second example y is a floating type of variable which can hold a floating type of value.

we can declare many variables of same type, in a single statement:-

1.  int a, b, c, d;

The  Rules for  naming  of a variable

c programming variables Name Must start with the letter combination of alphabets, digits and underscores.

The first character must be an alphabet or an underscore, the name of a variable must not start with a digit.
No comma or blank space is allowed.
No word which is a reserved name in C (keywords) can be used as a variable name.
Example:-

abed —> Valid Variable

12ab —> Invalid Variable

_sum —> Valid Variable

#sum —> Invalid Variable//begin with an alphabetic character
we declared four variables which are all of int type and we declared it in a single statement.
we can do mathematical and logical operation with the help of various types of operator provided by the C language.

 

Leave a Reply

Your email address will not be published. Required fields are marked *