What Are Constants in C Programming?
A constant in C is a fixed value used directly in an expression or assigned to an object that the program should not modify. Examples include the integer constant 42, floating constant 3.14, character constant 'A', and string literal "Hello".
Constants make source code easier to understand and help prevent accidental changes to values such as limits, conversion factors, and configuration settings. C programs commonly use literal constants, objects declared with const, enumeration constants, and symbolic constants created with #define.
Types of Constants in C
| Constant type | Examples | Meaning |
|---|---|---|
| Integer | 25, 037, 0x1F | Whole-number values written in decimal, octal, or hexadecimal notation |
| Floating-point | 3.14, 2.5e3, 1.0f | Values containing a fractional part or exponent |
| Character | 'A', '\n' | One character or escape sequence enclosed in single quotes |
| String literal | "Hello" | A sequence of characters enclosed in double quotes |
| Enumeration | MONDAY | A named integer constant declared in an enum |
Integer Constants and Their Suffixes
- A decimal integer constant contains digits from
0to9, as in4312. - A leading zero denotes an octal constant, so
037represents decimal 31. - A
0xor0Xprefix denotes hexadecimal notation, as in0x1F. - The suffix
Uorurequests an unsigned integer type, as in4234U. - The suffix
Lorlrequests a long integer type, as in123456789L. - Suffixes can be combined, as in
800ULfor an unsigned long constant.
An integer constant does not contain a decimal point. Its exact type is selected from the C integer types according to its value, base, and suffix.
Floating-Point Constants in Decimal and Exponent Form
- Floating-point constants can use decimal notation, such as
123.5. - Exponent notation uses
eorE, as in1.25e2, which represents 125. - An unsuffixed floating-point constant such as
3.14has typedouble. - The suffix
forFcreates afloatconstant, as in3.14F. - The suffix
lorLcreates along doubleconstant, as in3.14L.
Character Constants and C Escape Sequences
A character constant is written between single quotation marks, such as 'a', '4', or '\n'. In C, an ordinary character constant has type int and represents the numeric value of the character in the execution character set.
Escape sequences provide a way to represent characters that are difficult or impossible to type directly. They begin with a backslash and can be used in both character constants and string literals.
| Escape sequence | Symbol | Meaning |
| \a | Alert | Sounds a beep |
| \b | Backspace | Backs up one character |
| \f | Form feed | Starts a new screen of page |
| \n | New line | Moves to beginning of next line |
| \r | Carriage return | Moves to beginning of current line |
| \t | Horizontal tab | Moves to next tab position |
| \v | Vertical tab | Moves down a fixed amount |
| \’ | Single quotation | Prints a single quotation |
| \” | Double quotation | Prints a double quotation |
| \\ | Back slash | Prints back slash |
| \? | Question mark | Prints question mark |
Printing C Escape Sequences with printf()
The following program uses a horizontal tab, newline, and alert escape sequence. Whether \a produces an audible alert depends on the terminal or operating environment.
C Program
#include<stdio.h>
int main() {
printf("hello \t users \n of \a tutorialkart ");
return 0;
}
Output
hello users
of tutorialkart
String Constants and the Null Terminator
A string literal is a sequence of zero or more characters enclosed in double quotation marks, for example "I am good". Its stored representation includes a terminating null character, '\0'. Consequently, the literal "ABC" requires four character positions.
Adjacent string literals are joined during translation. This allows a long message to be divided across source-code lines without inserting a newline into the resulting string.
const char message[] = "C joins "
"adjacent string literals.";
Character Constant Compared with String Literal
'x' and "x" are not interchangeable. 'x' is a character constant representing one character value. "x" is a string literal containing 'x' followed by '\0'.
Enumeration Constants in C
An enumeration gives meaningful names to related integer constants. Unless values are assigned explicitly, the first enumerator has the value 0 and each following enumerator increases by one.
enum Status {
STATUS_PENDING,
STATUS_RUNNING,
STATUS_COMPLETE
};
Defining Named Constants with const and #define
C provides two common ways to give a fixed value a meaningful name:
- Declare an object with the
consttype qualifier. - Create a symbolic macro with the
#definepreprocessor directive.
Declaring Read-Only Objects with the const Keyword
The C keyword is const, not constant. A const-qualified object must not be modified through its declared name after initialization. It also retains type information, so the compiler can check how the value is used.
Legacy Constant Declaration Syntax
The following legacy syntax shows the intended declaration pattern, but constant is not a valid C keyword and this form will not compile:
constant data_type variable = value ;
Legacy Radius and Pi Declaration
This older snippet has the same issue because it uses constant instead of the valid const keyword:
constant int radius= 5 ;
constant float pi =3.143 ;
The valid C declarations are:
const int radius = 5;
const float pi = 3.143F;
Initializing a const object when it is declared is the clearest approach. In C, const means that the program cannot modify the object through that const-qualified access path; it does not necessarily place the object in read-only memory.
Creating Symbolic Constants with #define
The #define directive instructs the preprocessor to replace a macro name with its replacement text before compilation. Object-like macro names used as constants are conventionally written in uppercase.
#define Constant Directive Syntax
#define identifier value
For example, #define PI 3.143 creates a macro named PI. Do not place an assignment operator or semicolon in a simple object-like macro definition.
Using Literal, const, and #define Constants Together
The following program demonstrates a macro constant, a const int, a character constant, a string stored in a const-qualified array, and an escape sequence.
C Program
#include <stdio.h>
#define NUM 3.14
int main() {
const int length= 100; //int constant
const char letter = 'A'; //char constant
const char str[10] = "ABC"; //string constant
const char esc = '\?'; //special char constant
printf("value of length :%d \n", length );
printf("value of num : %f \n", NUM);
printf("value of letter : %c \n", letter );
printf("value of str : %s \n", str);
printf("value of esc: %c \n", esc);
return 0;
}
Output
value of length :100
value of num : 3.140000
value of letter : A
value of str : ABC
value of esc: ?
Choosing Between const, #define, and enum
| Mechanism | Suitable use | Main consideration |
|---|---|---|
const | Typed values and read-only objects | Has a C type and follows normal scope rules |
#define | Conditional compilation, header guards, and simple textual macros | Performs textual replacement and has no C type |
enum | Related named integer constants | Produces integer constants and groups related names |
Prefer const when a typed object is required. Use enum for a related set of integer names. Use #define when preprocessing behavior is needed or when a project specifically requires a macro constant.
Common C Constant Errors
- Writing
constantinstead of the valid C keywordconst. - Using
"A"where a single character value'A'is required. - Writing a decimal integer with a leading zero and unintentionally creating an octal constant.
- Using
0XIFas hexadecimal notation; valid hexadecimal digits are0–9andA–F, so0x1Fis valid. - Adding a semicolon to a simple
#definereplacement value. - Attempting to modify a string literal. Use a writable character array when the characters need to change.
C Constants Editorial QA Checklist
- Confirm that character constants use single quotes and string literals use double quotes.
- Check integer prefixes and suffixes, including
0,0x,U, andL. - Verify that examples use
const, not the invalid wordconstant. - Ensure every escape sequence includes the leading backslash.
- Check that
#defineexamples omit an unnecessary assignment operator and trailing semicolon.
Frequently Asked Questions About C Constants
What does a constant mean in C?
A constant is a value that does not change while it is being used as a constant. It may be written directly as a literal, represented by an enumeration constant, substituted by a macro, or stored in an object qualified with const.
What are five examples of constants in C?
Examples are 25 (integer), 3.14 (floating-point), 'A' (character), "Hello" (string literal), and STATUS_COMPLETE declared as an enumeration constant.
What is the difference between const and #define in C?
const is a C type qualifier applied to an object, so the object retains a type and scope. #define creates a preprocessor macro whose replacement text is substituted before the compiler processes the C program.
What is the difference between a character constant and a string constant?
A character constant such as 'A' represents one character value. A string literal such as "A" is an array containing the character 'A' and a terminating null character.
C Constants Summary
In this C Tutorial – C Constants, we covered integer, floating-point, character, string, and enumeration constants. We also compared typed const objects with #define macros and identified common mistakes involving quotes, numeric prefixes, suffixes, and escape sequences.
TutorialKart.com