← Back to C Course | Chapter 1: Introduction & Basics | Lesson 20 of 21

C Constants

A constant is a value you promise never to change, like the number of days in a week. C gives you const and #define to lock it.
Syntax
c
const data_type CONSTANT_NAME = value;

#define CONSTANT_NAME value

Const Keyword

Marking a variable const tells the compiler to reject any later attempt to reassign it, turning accidental modification of a value that should never change into a compile-time error instead of a silent runtime bug.

Example: Const Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare MAX and set it to 100
	const int MAX = 100;
	// Print formatted text to the screen
	printf("%d", MAX);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Define Directive

#define performs a simple text substitution before compilation even begins -- every occurrence of the defined name in your source is literally replaced with its value, so the compiler never even sees the original name.

Example: Define Directive

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", PI);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Integer Constants

Integer constants can be written in decimal (42), octal with a leading 0 (052), or hexadecimal with a leading 0x (0x2A) -- all three represent the same value, and the choice is purely about which base is clearest for that context.

Example: Integer Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare decimalVal and set it to 42
	int decimalVal = 42;
	// Declare octalVal and set it to 052
	int octalVal = 052;
	int hexVal = 0x2A;
	// Print formatted text to the screen
	printf("%d %d %d", decimalVal, octalVal, hexVal);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Real Constants

Real (floating-point) constants like 3.14 are stored as either float or double depending on context, and always include a decimal point or exponent so the compiler doesn't mistake them for integer literals.

Example: Real Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare pi and set it to 3.14
	double pi = 3.14;
	// Print formatted text to the screen
	printf("%.2f", pi);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Character Constants

A character constant like A is written in single quotes (not double, which is for strings) and is really just shorthand for that character's ASCII integer value -- A and 65 are interchangeable in C.

Example: Character Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	// Print formatted text to the screen
	printf("%c is %d", letter, letter);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to change a const variable later, which is a compile error.
  2. Putting a semicolon at the end of a #define line, which becomes part of the replacement text.
  3. Writing #define PI = 3.14, where the = becomes part of the replacement.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.