Variable:
Variable refers to a name of particular space, where values can be stored and change/manipulate in program. Every variable is assigned to specific datatype (int, float, char etc). The value of a variable can be changed, hence it is named as variable.Variable declaration:
Syntax:
datatype_variable1, variable2, variable3, ..., ..., ..., ..., variableN;Example (A):
int a, b, c;
Example (B):
float x, y, z;
Variable initialization:
Syntax:
Datatype variable_name1=value, variable_name2=value2;Example (A):
int a=5, b=7;
Example (B):
float x=11.65, y=32.54;
- Let's see an examples of variables to get better understand.
There are some rules to name a variable:
- A variable name can have only digits, letters with lowercase, letters with uppercase and underscore.
- Length of variable is not restricted. However, compiler will check only first 31 characters of a variable. So, only that 31 letters of different variables in a program should be different.
- First letter of a variable should be either an underscore or a letter. However, it is discouraged to start name of a variable with an underscore. It is because name of the variable which starts with an underscore can conflict with the name of system and it may cause an error.
- The type of a variable can't be changed because C is a strongly typed language.
Constants:
An identifier whose value can't be altered in a program is called constant. For example: 1, 2.5, "Easy C Program", etc. An identifier also can be defined as a constant.Example:
const float PI = 3.14;
In upper programming line "PI" is a constant. It means is that, 3.14 and PI is same for that program.
Different types of constants:
Integer constants:
An integer constant is a numeric constant without any exponential or fractional part. There are 3 types of integer constants in C:base 8: octal constant (077, 021 etc)
base 10: decimal constant (-9, 27, 99, 0, etc)
base 16: hexadecimal constant (0x521, 0x2a, 0x7f etc)
- An octal constants are starts with a 0 and a hexadecimal constants are starts with a 0x.
Floating-point constants:
A numeric constant that has either an exponent form or a fractional form is called a floating point constant.Example:
0.000354
-5.0
-0.22E-5
Note: E-5 = 10-5
Character constants:
A this constant is a constant which uses single quotation around characters like 'f', 'c', 'u', 'd', 'e','k'.String constants:
The constants which are enclosed in a pair of double-quote marks are called string constant.Example:
"" // null string constant
"f" // string constant having single character.
"nice" // string constant
" " // string constant of five white space
"Enter values\n" // prints string with newline