Constant
Constants in C are fixed value that does not change during the execution of a program. Constants can be of any of the basic data types. C supports several types of constants in C language as C constants
a. Numeric Constants i. Integer Constant 1. Decimal Integer constant 2. Octal integer constant 3. Hexadecimal Integer constant ii. Real Constant b. Character Constants i. Single Character Constant ii. String Constant c. Backslash Character constants d. Symbolic constants Review Questions
1.Define constants? 2.What are constants? 3.State the difference types of constants in C? (Or) Name the various types of constants in C? 4. Mention different constant types? Numeric constants: There are two types of numeric constants,
Definition of Integer
Definition of Negative Integer
Definition of Positive Integer
Rule for an Integer
Rules for constructing Integer constants i. An integer constant must have at least one digit. ii. It must not have a decimal point. iii. It can be either positive or negative. iv. If no sign precedes an integer constant, it is assumed to be positive. v. Commas or blanks are not allowed within an integer constant. There are three types of integer constants namely, a) Decimal integer constant b) Octal integer constant c) Hexadecimal integer constant Decimal Integer constant (base 10) Rule
Invalid: 12,245 - Illegal character (,) 10 20 30 - Illegal character (blank space) Octal Integer Constant (base 8) Rule
Invalid: 0786 - Illegal digit 8 123 - Does not begin with zero 01.2 - Illegal character (.) Hexadecimal integer constant Rule
Valid : 0x 0X1 0x7F Invalid : 0xefg - Illegal character g 123 - Does not begin with 0x Unsigned integer constant: Rule
Valid: 0u 1U 65535u 0x233AU Invalid: -123 - Only positive value Long integer constant:
Valid: 0l23456L 0x123456L -123456l Invalid: 0x1.2L - Illegal character (.) Short integer constant:
Invalid: 12,245 - Illegal character (,) 10 20 30 - Illegal character (blank space) Note: - A sign qualifier can be appended at the end of the constant. Usually suffixes(s or S, u or U, l or L) are not needed. The compiler automatically considers small integer constants to be of type short and large integer constants to be of type long. Part-A
Part-B
Definition: A real constant is combination of a whole number followed by a decimal point and the fractional part.
Example: 0.0083 -0.75 .95 215. Use of Real or Floating-point constants Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices and so on. These quantities are represented by numbers containing fractional part. Such numbers are called real or floating point constants. Various forms of Real or Floating Constants The Real or Floating-point constants can be written in two forms: 1. Fractional or Normal form 2. Exponential or Scientific form Express a Real constant in fractional form A real constant consists for a series of digits representing the whole part of the number, followed by a decimal point, followed by a series of representing the fractional part. The whole part or the fractional part can be omitted, but both cannot be omitted. The decimal cannot be omitted. That is, it is possible that the number may not have digits before the decimal point or after the decimal point. Valid Real constants (Fractional): 0.0 -0.1 +123.456 .2 2. Invalid Real constant: - 1 - a decimal point is missing 1, 2.3 - Illegal character (.) Rules for Constructing Real Constants in Fractional Form 1. A real constant must have at least one digit. 2. It must have a decimal point. 3. It could be either positive or negative. 4. Default sign is positive. 5. Commas or blanks are not allowed within a real constant. Express a real constant in Exponential form A real constant is combination of a whole number followed by a decimal point and the fractional part. If the value of a constant is either too small or too large, exponential form of representation of real constants is usually used. In exponential form, the real constant is represented in two parts. Mantissa: The part appearing before e, the mantissa is either a real number expressed in decimal notation or an integer. Exponent: The part following e, the exponent is an integer with an optional plus or minus sign followed by a series of digits. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase. Example: 0.000342 can be represented in exponential form as 3.42e-4 7500000000 can be represented in exponential form as 7.5e9 or 75E8 Rules for Constructing Real Constants in Exponential Form 1.The mantissa part and the exponential part should be separated by letter in exponential form. 2.The mantissa part may have a positive or negative sign. 3.Default sign of mantissa part is positive. 4.The exponent part must have at least one digit, which must be a positive or negative integer. Default sign is positive. 5.Range of real constants expressed in exponential for is -3.4e38 to 3.4e38. Review Questions Part-A
Part-B
Character: A character denotes an alphabet, digit or a special character.
Single Character constants:
For example, ۥAۥ is a valid character constant whereas ‛Aۥ is not. Note that the character constant ۥ 5ۥ is not the same as the number 5. Valid Character Constants: ۥmۥ ۥ=ۥ ۥAۥ ۥInvalid: ۥ123ۥ - Length should be 1 "A" - Enclosed in single quotes Note: - Each single character constant has an integer value that is determined by the computer’s particular character set. Rules for Constructing Single Character constants 1.A single character constant or character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. 2. The maximum length of a single character constant can be one character. 3. Each character constant has an integer value that is determined by the computer’s particular character set. Review Questions
String
Note that a character constant ۥA and the corresponding single character string constant "A" are not equivalent. A ۥ - Character constant - ۥAۥ "A" - String Constant - ۥAۥ and ۥ \0ۥ (NULL) The string constant "A" consists of character A and \0. However, a single character string constant does not have an equivalent integer value. It occupies two bytes, one for the ASCII code of A and another for the NULL character with a value 0, which is used to terminate all strings. Valid: - "W" "100" "24, Kaja Street" Invalid: - "W the closing double quotes missing Raja" the beginning double quotes missing Rules for Constructing String constants 1) A string constant may consist of any combination of digits, letters, escaped sequences and spaces enclosed in double quotes. 2) Every string constant ends up with a NULL character which is automatically assigned (before the closing double quotation mark) by the compiler. Review Questions
Difference between single-character constant and String constant |
Character Constant
|
String Constant
|