The term constant means that it does not change during the execution of program. In the language C, constant is the data with a constant value that does not change in the program.
For example : "100", "3.14"
There are four basic types of constants in C. They are :
- Integer constants
- Floating point constants
- Character constants
- String constants
Integer & floating-point constants are also referred as numeric-type constants because they represent number.
The following rule applies to all numeric type constants:
- The rules to all numeric type constants are :
- The constant can be preceded by a minus ( - ) sign.
- Commas & Blank spaces cannot be included with in the constant.
- Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be positive.
- The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another.
- a) Integer constants
- b) Floating-point constants
- c) Character constants
- d) String constants
Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. Integer constants can be written in three different number systems: Decimal, Octal and Hexadecimal.
A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants.
Ex : 0 1 1234 -786
A floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used.
The following are valid floating-point constants.
1.0 0.1 2E-4 -0.1555e-4
A character constant is a single character, enclosed in single quotation marks. Characters are stored internally in computer as coded set of binary digits, which have positive decimal integer equivalents. The value of a character constant is the numeric value of the character in the machine’s character set. This means that the value of a character constant can vary from one machine to the next, depending on the character set being used on the particular machine. For example, on ASCII machine the value of ‘A’ is 65 and on EBCDIC machine it is 193.
A string constant consists of zero or more character enclosed in double quotation marks. Several string constants are given below.
Ex: “AMT” “12345” “a+b” “Welcome to C Programming”
There is a difference between the constant ‘A’ and “A” in C, the first ‘A’ is a character constant while the second “A” is string constant. The notation ‘A’ is a constant occupying a single byte containing the ASCII code of the character A. The notation “A” on the other hand ,is a constant that occupies two bytes one for ASCII code of A and another for the null character with the value 0,that terminates all the string.
ASCII character set (American Standard code for Information Interchange) Constant Value
"A" -> 65
"a" -> 97
"s" -> 53
"%" -> 37