After reading this constants and variables topic, you will understand its theory and examples and also you will also able to use them in C programming.
C constants value always fixed and do not change during the execution of the C program. C constants are also refereed as literals.
Constants Types :
The constants types in C language shown below:
1. Integer constant: It is a group of digits either from 0 to 9.
For example:
int a=500;
2. Octal: It is any combination of digits either from 0 to 7 and 0 (zero) at the beginning.
For example:
int a=058;
3. Hexadecimal: It is any combination of digits either from 0 to 9 and letters a,b,c,d,e,f respectively also 0x at the beginning.
For example:
int a=0x8f;
4. Real/float: It is any number containing a fractional part.
For example:
float a=1.54;
5. Single character: A single character enclosed in between pair of single quote marks (‘ ’).
For example:
char a=’k’;
6. String: – It is a group of character enclosed in double quotes (“ “).
For example:
char a[]=”LANGUAGE”;
7. Symbolic constant: It is defined by using #define directive.
For example:
#define PI 3.14;
8. Object constant: It is used to declare variable as constant during the execution of program.
For example:
int const a=5;
9. Backslash character constants: These constants having special meaning and made up of using backslash character (\) at the beginning with a single character. These are
Constant | Meaning |
---|---|
\a | audible alert (bell) |
\\ | backslash |
\b | backspace |
\r | carriage return |
\" | double quote |
\f | form feed |
\t | horizontal tab |
\n | new line |
\0 | null |
\? | question mark |
\' | single quote |
\v | vertical tab |
Variables
A variable is a name of storage area for holding data and variable data can vary during the execution of the program.
For example:
The statement in C is given by
int sum;
Indicate that
- int represent integer data type.
- sum is variable name.
Rules for defining Variable Name
- An alphabet or underscore used in the starting letter of variable name.
- A variable name cannot be keywords or standard function name.
- A variable name length is compiler dependent.
- Only underscore in special characters is allowed.
- Variable name is case sensitive i.e. uppercase letters and lower case letters are different.