Constants in C: Usage and Examples
Table of Content:
In C programming, a constant is a value that cannot be modified once it is assigned. Constants are typically defined using the keyword "const
" before the data type. For example, the line "const int MAX_SIZE = 100;" defines a constant integer named MAX_SIZE with a value of 100. Unlike variables, constants cannot be changed during the execution of a program. They are used to represent values that will remain the same throughout the program, such as mathematical constants or other fixed values.
C constants can be divided into two major categories:
- Primary Constants
- Secondary Constants
Integer Constants
Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows
Example
- Decimal Integer Constants
- Ocatal Integer Constants
- Hexadecimal Integer Constants
Decimal Integer Constants
Decimal constant consists of a set of digits 0 through 9, preceded by an optional –or + sign.
1, 3, 7 , 8, 65, 543676664
Octal Integer Constants
An octal integer constant consists of any combimation of digits from the set 0 through 7, with an leading 0.
038, 320, 0456, 0552, 0432
Hexadecimal Integer Constants
An octal integer constant consists of any combimation of digits from the set 0 through F, with an leading 0x or 0X.
0x4, 0X456, 0x552 , 0x32, 0x43
Rules for Constructing Integer Constants
(a) It must not have a decimal point.
(b) It can be either positive or negative.
(c) If no sign precedes an integer constant it is assumed to be
positive.
(d) No commas or blanks are allowed within an integer constant.
(e) The allowable range for integer constants is -32768 to 32767.
(f) An integer constant must have at least one digit.
Real Constants
Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
Integer numbers are unable to represent distance, height, temperature, price, and so on. This information is containing fractional parts or real parts like 56.890. Such numbers are called Real or Floating Point Contents.
Example
- A Real Constant must have at Least one Digit
- it must have a Decimal value
- it could be either positive or Negative
- if no sign is Specified then it should be treated as Positive
- No Spaces and Commas are allowed in Name
Like 251, 234.890 etc are Real Constants
In The Exponential Form of Representation, the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.
- In Real Constant, The Mantissa and Exponent Part should be Separated by letter e
- The Mantissa Part have may have either positive or Negative Sign
- Default Sign is Positive
- The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
- Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Example:
+4.2e-5 5.1e8 -0.2e+3 -7.2e-5
Rules for Constructing Real Constants
Following rules must be observed while constructing real constants expressed in fractional form:
(a) Real Constants must have a decimal point.
(b) A real constant must have at least one digit.
(c) Real Constants could be either positive or negative.
(d) Default sign is positive.
(e) No commas or blanks are allowed within a real constant.
Single Character Constants
A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas.
- Character Constant Can hold Single character at a time.
- Contains Single Character Closed within a pair of Single Quote Marks
- Single Character is smallest Character Data Type in C.
- Integer Representation: Character Constant reprent by Unicode
- It is Possible to Perform Arithmetic Operations on Character Constants
Examples of Character Type:
'a' 'A' '1' '4343' '#' '-' '<' 'X' ETC.
How to Declare Character Variable?
Way 1: Declaring Single Variable
char variable_name;
Way 2: Declaring Multiple Variables
char var1,var2,var3;
Way 3: Declaring & Initializing
char var1 = 'A',var2,var3;
- Question 1: What is lvalue and rvalue?
- Question 2: What is l-value?
- Question 3: What are variables and it what way is it different from constants?
- Question 4: What is constant in C?
- Question 5: What are the types of constants in C?
- Question 6: What is the difference between a variable and a constant in C programming?
Related Questions
- Assignment 1: integer constant in c programming language
- Assignment 2: character constant in c programming language
- Assignment 3: floating point constant in c programming language
- Assignment 4: double constant in c programming language
- Assignment 5: unsigned char constant in c
- Assignment 6: signed char constant in c
- Assignment 7: short constant in c
- Assignment 8: short unsigned constant in c
- Assignment 9: long constant in c
- Assignment 10: unsigned long constant in c
- Assignment 11: signed long constant in c
- Assignment 12: signed short constant in c
- Assignment 13: long double constant in c
- Assignment 14: Character constant in c program