In this post I'm gonna discuss what constants are actually in C. You can access my C programming full course in order to learn programming skills quickly.
When we talk about Constants in C, there are lots of questions arise, which I will try to answer in this post.
So lets start discussing all the questions.
What are Constants in C?
C Constants are the most fundamental and essential part of the C programming language. Constants in C are values or variables which can't be changed in the program, also called literals.
I mean contants are like variables, which value never changes during execution once defined.
For example: 40, 54, 'd', 2.4, "c programming tutorial" etc. are constants.
Types of Constant in C
C Constant can be classified as:
- Integer constant
- Real constant
- Character constant
- String constant
1. Integer Constant in C Language:
Integer Constants in C are refers to the sequence of digits. Range of integer constants is -32,768 to 32,767.
There are three types of integer constants in the C language:
Decimal constant (base 10) Decimal Digits: 0 1 2 3 4 5 6 7 8 9 Example: 0. -8, 24 etc.
Octal constant (base 8) Octal Digits: 0 1 2 3 4 5 6 7 Example: 421, 027, 233 etc.
Hexadecimal constant (base 16) Hexadecimal Digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F Example: 0x3f, 0x5a, 0x541 etc.
2. Real Constant in C Language
Real constant in C refers to the numbers containing fractional parts like 45.25, are called real or floating points constant. The range of Real Constants is -3.41038 to 3.41038.
There are two form of writing Real constants: Decimal Form: -2.0, 0.0000234 Exponential Form: -0.22E-5
3. Character Constant in C Language
Character Constant in C refers to the single character enclosed within a pair of single quote (' ') like 'A', 'd', '54', '8'.
The range of Character Constants is -128 to 127.
4. String Constant in C Language
String constants in c refers to the sequence of characters enclosed in double quotes, and they may include letters, digits, special characters, and blank spaces.
Examples: “Hello”, “1987”, “?….!”, “x”, ” “, “” (null string constant)
Two ways to define constant in C
Now, we see, How to use Constants in a C Program? There are two ways to define constant in C programming const keyword
define preprocessor
1. C const keyword
In order to define constant in C programming, the const keyword is used. Read More...
Original Credit: www.dheerajpatidar.com