
C Programming Constants
Table of Contents
In this chapter you will learn about C programming constants and different types of constants.
C Programming Constants and Iiterals
A constant is a value or identifier. whose value does not change during program execution. For example: “SATT Academy”, 1, 2 etc.
So we see that identifiers can also be defined as constants.
const double PI = 3.14
Here PI is a constant which means that in this program PI and 3.14 are the same.
Various types of constants used in C programming and their description are given below.
C Programming Character Constants
A character constant is a single character constant that is surrounded by single quotation marks (‘ ‘). For example: ‘a’, ‘M’, ‘T’, ‘s’ etc.
C Programming Integer Constants
Integer constants are constants with arithmetic whole numbers without decimal and exponent parts. There are three types of integer constants in C programming.
- Decimal constant-decimal constant (constant based on 10)
- Octal Constant
- Hexadecimal constant-hexadecimal constant (constant based on 16)
For Example:
8 based constants: 070, 055, 099 etc
10 based constants: 0, 20, 100 etc
16 based constants: 0x7f, 0x2a, 0x521 etc
In C Programming octal constants start with 0 and hexadecimal constants start with 0x.
C Programming Floating Point Constants
A floating point constant is an arithmetic constant with decimal and exponent parts.
10.05
0.03487
-0.15E-4
Note: E-4=10 -4
C Programming Spetial Sequences
Sometimes there are some characters that are not typeable or have special meaning in C programming. Like backspace, tab, backslash etc. To use them, escape sequence (escape sequence) has to be used.
For example Newline \n is used for \b Used for backspace.
Space sequence:
C Programming String Constants
String constant is a constant that is surrounded by double quotations (” “). For example:
" " //null string constant
"QUICK" //string constant
" " //String constant with 5 spaces
"t" //A single character string constant
"Love is heaven\n" //will print the string with a new line

C Programming Enumeration Constants
Enumeration types are defined with the enum keyword. For example:
enum color {teal, green, orange, white};
Here color is a variable and purple, green, black and white are enumeration constants whose are 0, 1, 2 and 3 respectively. To learn more about enumeration, visit our C enumeration section.
Defining Constants in C Programs
There are two general methods for defining constants in C programs.
- Using the const keyword
- #define uses the preprocessor
C Programming const keyword
You can the const keyword as a prefix to declare a constant of a specific type.
Syntax
const type variable = value;
Example: The following example shows the use of the const keywords:
#include
int main() {
const char NEWLINE = '\n';
const int LENGTH = 5;
const int WIDTH = 4;
int area;
area = LENGTH * WIDTH;
printf("Total area: %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above program is compiled and executed it will show the following output:
output
TOTAL AREA : 20
C Programming #define Preprocessor
You can also define constants using the #define preprocessor.
You can also define constants using the #define preprocessor.
Syntax
#define identifier value
Example: The following example shows the use of the #define preprocessor:
#include
#define NEWLINE '\n'
#define LENGTH 5
#define WIDTH 4
int main() {
int area;
area = LENGTH * WIDTH;
printf("Total area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above program is compliled and executed it will show the following output:
output
TOTAL AREA : 20
Pingback: C Programming Data Type -