Definition of Constant
constant is a value (for example: 11,20.05,"Hello World",etc.) or a variable whose value is fixed during program execution. To declare a variable as a constant C proviedes a keyword const.
syntax of constant
constant data_type variable_name=value;
Example of constant
const float PRICE=495.75;
Declaration |
>Explanation |
const int MAX=1; |
read as "MAX is an integer which constant" |
int const MAX=1; |
read as"MAX is a constant integer"
|
Both are valid.
types of constants
 |
Various Types of Constants |
primary Constants
- integer Constants in C
- Real Constants in C
- Character and string constants in C
- Backslash character constants in C
- integer Constants in C
Rules for constructing Integer constants
- An integer constant must have at least one digit .
- It must not have a decimal point.
- It can either be positive or negative.
- No commas or blanks are allowed within an integer constant.
- If no sign precedes an integer constant, it is assumed to be positive.
- The allowable range for integer constants is -32768 to 32767.
Example . - 10,-578, 5000, etc.
- Here we can use following data types to declare integer constant :
int, long int, long long int, signed int, unsigned int.
2. Real constants in C
Rules for constructing Real constants
- A real constant must have at least one digit.
- It must have a decimal point.
- It could be either positive or negative.
- If no sign precedes an integer constant, it is assumed to be positive.
- No commas or blanks are allowed within a real constant.
Example
- Here we can use following data types to declare real constant:
float
3. Character and string constants in C
Rules for constructing Character constants
- A character constant is a single alphabet, a single digit
or a single special symbol enclosed within single quotes.- The maximum length of a character constant is 1 character.
- String constants are enclosed within double quotes.
Example
Character Constants: 'Z', 'M', etc.
String Constants "Kunal", "Hello", etc.
Here we can use char data type to declare character or string constant.
4. Backslash character constants in C
Rules for constructing Backslash constants
- There are some characters which have special meaning in C language.
- These characters are preceded by backslash symbol to make use of special function of them.
- Table is the list of special characters and their purpose.
Backslash_character |
Meaning |
---|
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Return |
\t |
Horizontal tab |
\v |
Vertical tab |
\\ |
Backslash |
\' |
Single quotation mark |
\" |
Double quotation mark |
\? |
Question mark |
\0 |
Null character |
click the link and read this post
you can join my Telegram group and learn C programming
Comments