list of contents
What is constant in C with example?
In C programming language, constants are the fixed values that are provided to the system while the designing of program. The value of a constant cannot be change while the execution of the program. The values are stored in identifiers with different name and at different places. The type of the constant can be mentioned using keywords before initializing the constants.
Constants can be defined as the fixed values that are either defined by the user as an output during the execution of the program or defined at the time of declaration to a particular identifier. There are some universal constants that cannot be change by the user. In C programming language, constants are subdivided into many forms. The two specific form of constants that contains the other subdivision forms are Numeric constant and Character constant.
Define a Constant
Constant in C programming language can be defined by two ways:
- A variable is directly declared with the value of our constant at the start of the main function.
1 |
Example: int a=7; |
- We can also use #define directive, before the start of the main function. This statement does not end with a semicolon.
1 |
Example: #define PI 3.14 |
Different types of Constant in C
The constants in C programming language can be of 2 main types, Numerical constant and Character constant.
Numerical Constants
Numerical constants are the constant which contains all the numerical values such as real numbers, Integers, decimal, octal, hexadecimal, fractional or floating point values. The numerical constants is also sub-divided into 2 types, they are,
- Integer constants
- Real Constants
Integer constants
Integer constants are the constant that contains a series or sequence of digits. They do not contain any floating point value or fractional values. The sequence is continued and there is no other character is present between the digits of the constant. The Integer constant is further divided into 3 types, such as,
- Decimal integers
- Octal Integers
- Hexadecimal Integers
Decimal Integers
Decimal Integers contains all the digits from 0 to 9, which can be of positive form (0 to 9)or can be of negative form (0 to -9). The sequence of decimal integer constant does not permit the spaces, commas and any other character or symbol between the digits.
Example:
Some valid examples of Decimal integers:
- 1234
- -91
- 765418
Some Invalid examples of Decimal integers:
- 53 876
- 54.76
- 54,768
Octal Integers
Octal integer is a sequence of digits which contains digits from 0 to 7, with a prefix “0”, at the beginning. The octal integer does not permit any value without the prefix “0”. It also does not allow any digit above 7 and below 0 in the sequence.
Example:
Some valid examples of octal integers:
- 0345
- 0567
- 0777
Some invalid examples of octal integers:
- 7676
- O897
- 1234
Hexadecimal Integers
Hexadecimal integer consist digits from 0 to 15, where 10 to 15 represented by A to F respectively. The sequence is started with a 0, which is followed by x or X. the following table shows the alphabets that are used in place of digits from 10 to 15.
Digit | 10 | 11 | 12 | 13 | 14 | 15 |
Alphabet | A | B | C | D | E | F |
Example:
- 0x3c34
- 0X25
- 0XACD
Real Constant
Real constant contains the values which have a factional part or a floating point. This is also called as floating point constants. These constants are represented by values like 54.876. This constants can be of positive form (+ sign) or negative form (- sign).
Examples;
- 0.078
- -1.1456
- +765.089
Exponential Constants
Real constants can also be represented as a exponential constants. The exponential constant starts with notation E or e, followed by an exponent. The exponent is an integer value with an option plus (+) or minus (-) sign.
Examples:
- 123 e 6
- 54.43 E 8
- 354 e-5
Character Constant
The character constants are the constants that contain the character or string values. They may contain digits, alphabets, special symbols or the string of all three. They are classified into two forms such as, Single character constant and String Constants.
Single character constants
The single character constant contains a single character that can be an alphabet, digit or special symbol. These constants are mentioned in a pair of single quotation mark.
Example:
- ‘A’
- ‘7’
- ‘!’
- ‘x’
String Constants
The string constant contains a sequence of character, followed by enclosed in double inverted commas. The characters can be a digit, alphabet or special symbol.
Example:
- “HelloWorld!”
- “ABC!1234”
In C programming language, String is closed by ‘\0’, which is a null character that is automatically added to the end of the string.
Some of the other character constants are:
\0: Null character constant
\b: backspace character constant
\r: carriage-return character constant
\ t: horizontal tab character constant
\n: new line character constant
Here, we have completed with the whole concept of Constants and its types. Hope it will help you for better understanding purpose. If you have any queries, please feel free to contact us through comment section or through contact us forum.
Leave a Reply