Constant:
The quantity which does not change its value during the execution of a program is known as Constant. There are two types of Constants ;
1. Numeric Constant:
There are two types namely, Integer constant and Real or floating point constant.
1.1 Integer constant:
It is a whole number. It is a sequence of digit without a decimal point.
It is prefixed with a + or - sign. The general form is,
Sign | Digit
Sign: Optional + sign for positive numbers and - (minus) sign for negative numbers. Digits: sequence of numbers, +2, -25..
1.2 Real Constant :
It is a number with decimal point. It is defined as a sequence of digits preceded and followed by a decimal point. It is prefixed with a + or - sign. The general form is,
| sign | integer part | decimal part | fractional part |
Sign: Optional + sign for positive numbers and - (minus) sign for negetive numbers. Integer part : sequence of digits before decimal point. Decimal Point: ( . ) Period Symbol. Fractional Part : sequence of digits of decimal point. -0.012,12.25, -1.05...
2. Non - Numeric Constant:
There are two types namely, Single character constant and String constant.
2.1 Single Character Constant:
a single character enclosed with single quotes (' ') called as a single character constant. 'A', 'r', '#' , '<', etc...
2.2 String Constant:
It is a sequence of characters enclosed within double quotes (" ").
Variable:
The quantity which changes its value during the execution of the program. The variables are the names given to identify the specific program elements. Variables are also called as identifiers. The variables represents a particular memory location where data can be stored.
Rules for forming variable names:
1. The first character of a variable name must be an alphabet or an underscore ( _ ).
2. All succeed characters consists of letters and digits.
3. Both upper case and lower case are significant in C.
4. Writing the variable name in lower case is a good programming practice.
5. Keywords should not be used as a variable.
6. Special characters are not allowed.
7. There is no limit on the number of characters in variable name.
8. Always choose an appropriate variable name that makes proper sense of the user.
Declaration of Variable:
All variables must be declared before they are using in C Program. The Purpose of declaring variable is to reserve the amount of memory required for these variables.
Syntax :
| Data type | Var list | Semicolon |
int a,b ;
float c,d ;
char name[5] ;
Assigning Values to Variable:
The variable represents some memory location, where the data is stored. Each variable is associated with one/more value. The process of giving values to variable is called assignment of values. The assignment operator is "=" sign is use to assign a variable.
Syntax:
| var name | = | value | semicolon |
There are two methods of assigning values to variables. In the first method, the initial value can be assigned to variable within the declaration section. In second method, the initial value are assigned to the variable in the execution part of a program.
Assigning variables within the declaration is called initialization. In this case the declaration must consists of a data type followed by a variable name, an equal sign and a number appropriate to the data type and finally semicolon.
Before Execution : float PI = 3.142 ;
After Execution : float PI; (the value will be asked while the program in execution).