C - Constants
Constants in C are fixed values that do not change during execution of a program. Different types of constants are:
1. An integer constant is an integer valued number or a sequence of digits, preceded by an optional + or - sign.
2. Number systems in which an integer constant can be written as:
a) Decimal integer constant :
A decimal integer constant may consist of any combination of digits of any combination of digits from 0 to 9. Moreover, a decimal integer cannot start with a 0. Valid examples are 12, -543, +324 and even 0.
b) Octal integer constant :
An octal integer constant may consist of any combination of digits from 0 to 7. Moreover, t must start with a 0. Valid examples are 0, 012, 0557.
c) Hexadecimal integer constant :
A hexadecimal integer constant may consist of any combination of digits from 0 to 9 and alphabets a to f(uppercase or lowercase) . Moreover, it must start with ox or 0X. The letters a to f or A to F represent decimal quantities 10 to 15. Valid examples are 0x, 0x12, 0x557, 0X12, 0xaaBc, 0XAAaa.
2. Floating Constants:
A floating point constant is a base -10 number that contains either a decimal point or an exponent or both.
hence, the quantity 7*10^5 can be represented in C by any one of the following floating point constant:
3) Character Constants:
- A character constant is a single character, enclosed in apostrophes(Single quotation marks).
- Valid character constants are ‘A’, ‘X’,’4’,’$’,’’.
- .Character constants have integer values that are determined by the computer’s particular character set. Thus the numerical value associated with a character constants may differ from one computer to another.
- Most computer make use of ASCII(American Standard Code for Information Interchange) character set in which each character has an equivalent integer value.
For example, the ASCII code of ‘A’ is 65.
1. An escape sequence always begins with a backslash (\) and is followed by one or more characters. Such escape sequence always represents single characters, even though they are written in terms of two or more symbols.
2. for example, \n is an escape sequence which represents the newline character.
3. Certain non-printing characters, as well as the double quote(“), the apostrophe(‘), the question mark(?), and the backslash(\) can be expressed in terms of escape sequence.
4. The commonly used escape sequence are given below:
5. The escape sequence \0 represents the null characters which is used to indicate the end of the string. Null character is not equivalent to character constant ‘0’.
4) String Constants:
- String constant consists of any number of characters (0 or 1 or more) enclosed in quotation marks(double quotes).
- A character constant represents a single character, however a string constant represents a sequence of characters.
- String constants do not represents numerical values.
Can escape sequence be included in a string constant?
1. yes. Escape sequence can be included in a string constant. 2. Certain printing characters like a backslash or a quotation mark and certain non-printing characters like tab or newline can be included in a string if they are represented in terms of their escape sequence. 3. A valid examples is “Teacher said \”Good Morning\””.
5. symbolic constants:
1.A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a numeric constant, a character constant or a string constant.
2. Symbolic constant definition can appear anywhere in a program, but it should be placed before that symbolic name is used in the program. the usual practice is to place all definitions in the beginning of the program.
3. A symbolic constant definition is written as $define name text where name is a symbolic constant (normally written in uppercase) and text represents the sequence of characters associated with the symbolic constant. Note that text does not end with a semicolon.
4. During preprocessing, each occurrence of a symbolic constant is substituted by its corresponding characters sequence.
5. A symbolic constant definition is sometimes also called a macro definition or simply a macro. Given below is an example: #define PI 3.141593
6. Now whenever PI appears in the programs,
it will be substituted by 3.141593.
7. This feature has two advantages:
a) Ease of modification b) Ease of understanding
6. Qualifier Constants:
1. At certain times, we may require that value of a particular variable remain constant during execution of a program.
2. We can achieve this by declaring the variable with the qualifier const at the time of initialization.
3. Example: const float pi=3.141593 or float const pi=3.141593;
float & const can be written in either order i.e. the data type and the keyword const can be written in either order.
5. This definition tells the compiler that the value of the variable pi should not be modified by the program.
6. Moreover the declaration const int a=3; is equivalent to const a=3; By default, the datatype of the variable is int.