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 typeExamplesMeaning
Integer25, 037, 0x1FWhole-number values written in decimal, octal, or hexadecimal notation
Floating-point3.14, 2.5e3, 1.0fValues 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
EnumerationMONDAYA named integer constant declared in an enum

Integer Constants and Their Suffixes

  • A decimal integer constant contains digits from 0 to 9, as in 4312.
  • A leading zero denotes an octal constant, so 037 represents decimal 31.
  • A 0x or 0X prefix denotes hexadecimal notation, as in 0x1F.
  • The suffix U or u requests an unsigned integer type, as in 4234U.
  • The suffix L or l requests a long integer type, as in 123456789L.
  • Suffixes can be combined, as in 800UL for 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 e or E, as in 1.25e2, which represents 125.
  • An unsuffixed floating-point constant such as 3.14 has type double.
  • The suffix f or F creates a float constant, as in 3.14F.
  • The suffix l or L creates a long double constant, as in 3.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 sequenceSymbolMeaning
\aAlertSounds a beep
\bBackspaceBacks up one character
\fForm feedStarts a new screen of page
\nNew lineMoves to beginning of next line
\rCarriage returnMoves to beginning of current line
\tHorizontal tabMoves to next tab position
\vVertical tabMoves down a fixed amount
\’Single quotationPrints a single quotation
\”Double quotationPrints a double quotation
\\Back slashPrints back slash
\?Question markPrints 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

</>
Copy
#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.

</>
Copy
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.

</>
Copy
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 const type qualifier.
  • Create a symbolic macro with the #define preprocessor 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:

</>
Copy
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:

</>
Copy
constant int radius= 5 ;
constant float pi =3.143 ;

The valid C declarations are:

</>
Copy
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

</>
Copy
#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

</>
Copy
#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

MechanismSuitable useMain consideration
constTyped values and read-only objectsHas a C type and follows normal scope rules
#defineConditional compilation, header guards, and simple textual macrosPerforms textual replacement and has no C type
enumRelated named integer constantsProduces 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 constant instead of the valid C keyword const.
  • 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 0XIF as hexadecimal notation; valid hexadecimal digits are 09 and AF, so 0x1F is valid.
  • Adding a semicolon to a simple #define replacement 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, and L.
  • Verify that examples use const, not the invalid word constant.
  • Ensure every escape sequence includes the leading backslash.
  • Check that #define examples 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.