What is constant in C?
What is constant in C ?
A constant specifies a fixed value that appears directly in a program and this value does not change during the program's execution. Constants are used to create values that are assigned to variables used in expressions or passed to functions . Some examples of constants are 120, 4.576, 'B', "amit" etc.
There are various type of constants available in C programming language.
1. Integer constants.
2. Floating point constants
3. Character constants
4. String constants
1. Integer constants :
An integer is a whole number without any decimal point. It consists of a sequence of digits. An integer constants is assumed to be of the int type, Whole values is between -32768 to 32767.
By default an, integer constant is a decimal number . However, they can be represented in octal (base 8) or hexadecimal (base 16) .
Rules for constructing Integer constants :
(a). No commas or blank spaces are allowed in an integer constant.
(b). It may be either positive or negative.
(c). It must not have a decimal point.
(d). It must have atleast one digit. Eg. 5 .
(e). The value of constant cannot be exceed a specified range.
2. Floating point constants :
When you need to represent quantities that vary continuously such as temperature (eg. 98.4) height (eg. 5.3), integer constants are not sufficient. So in order to represent these quantities, floating point constants are used. Floating point constants contain a decimal point or an exponent sign or both. Floating point constants are also called real constants.
Floating point constants can be written in two forms ,
- Fractional form (like, 125.50, .75 , 210. , -0.55)
- Exponential form (like, 1.4954e2 , 6.0e6 )
3. Character constant :
A character constant is a single character which may be a alphabet, a digit or a special symbol enclosed in a single inverted commas. Both inverted commas should point to left. The type of character constant is 'char' .
For example : 'A' ; 'S' ; '$' ; ' ' ; 'X' ; '0' ';
The maximum length of a character constant can be 1 character. Character constants have integers values that are determined by the computer particular character set called ASCII values.
3. String constants :
A string constants consists of any number of consecutive characters enclosed in double quotation marks. For example : "273" , "yellow" etc.
While working with strings, following points should be kept in mind,
(a). " " is an empty string.
(b). The compiler automatically places a null character ('\0') at end of every string constants as the last character within the string. This character is not visible when the string is displayed. In this way the computer knows where the string ends.
(c). "A" is combination of character 'A' and '\0' . So you cannot mix character constants and character strings.
(d) . Unlike single character constant, a single character string constant do not has an equivalent value.
Leave a Comment