๐Ÿ“ฆ C Variables, Data Types & Constants
Estimated reading: 3 minutes 428 views

C Constants and Literals โ€“ Syntax, Types, and Best Practices

Introduction โ€“ What Are Constants and Literals in C?

In C programming, constants and literals are used to represent fixed values that remain unchanged during the execution of a program. Constants improve code clarity and prevent accidental modification of important values. Literals, on the other hand, are the raw values directly written into your source code.

In this guide, youโ€™ll learn:

  • The difference between constants and literals
  • How to declare constants using const and #define
  • The different types of literals in C
  • Best practices for using constants and literals in programs

What Is a Constant in C?

A constant is a variable-like entity whose value is fixed after its initialization. C supports two ways to define constants:

const Keyword

Declares a typed constant that is scoped and type-checked by the compiler.

const float PI = 3.14;

#define Preprocessor Macro

Defines a constant at the preprocessor level (no data type checking).

#define MAX_USERS 100

Constant vs Variable

PropertyConstant (const)Variable
Modifiability Cannot be changed Can be modified
Type Safety Type-checked Type-checked
UsageFixed valueValue may change
Compiler CheckedAt compile timeDuring execution

What Is a Literal?

A literal is a raw, fixed value written directly into the source code. It is not named and cannot be modified.

Types of Literals:

TypeExampleDescription
Integer Literal42, 0xA, 075Whole numbers (decimal, hex, octal)
Floating-point Literal3.14, -0.5Decimal numbers
Character Literal'A', '1'Enclosed in single quotes
String Literal"Hello"Enclosed in double quotes
Boolean Literal (C99+)true, falseDefined in <stdbool.h> as macros

Example:

int age = 30;          // 30 is an integer literal
char grade = 'A';      // 'A' is a character literal
float pi = 3.14159;    // 3.14159 is a float literal

Constants with Expressions

Constants can also be initialized using constant expressions:

const int MAX = 5 * 3;       // Result: 15
#define AREA(l, w) ((l) * (w))  // Macro for area calculation

Best Practices

  • Use const for scoped and type-safe constants
  • Use #define for global compile-time constants
  • Prefer meaningful names over magic numbers
  • Use uppercase with underscores for constant names (MAX_SIZE, PI_VALUE)

Summary โ€“ Recap & Next Steps

Constants and literals are essential tools for creating readable, reliable, and maintainable C programs. They protect fixed values from accidental modification and enhance the clarity of your logic.

Key Takeaways:

  • Use const for typed, read-only variables
  • Use #define for macro-level constants
  • Literals are direct values like 10, 3.14, 'A', "text"
  • Avoid magic numbers by assigning them descriptive names

Real-World Relevance:

Constants are critical in embedded systems, configuration values, limits, hardware register definitions, and moreโ€”where stability and fixed data are non-negotiable.


Frequently Asked Questions (FAQ)

What is the difference between const and #define?

const declares a typed constant with scope, checked by the compiler.
#define is a macro that substitutes code during preprocessing without type safety.


Can a const variable be modified?

No. Attempting to change a const variable will result in a compile-time error.


Are string literals modifiable?

No. String literals are stored in read-only memory. Attempting to modify them leads to undefined behavior.


Can I assign an expression to a constant?

Yes. Constants can be initialized using constant expressions:

const int LIMIT = 10 + 5;  //  Valid

What is a magic number and why should it be avoided?

A “magic number” is a hardcoded value used directly in code without explanation. Replacing it with a named constant improves readability and maintainability.


Share Now :
Share

๐Ÿ”’ C Constants & Literals

Or Copy Link

CONTENTS
Scroll to Top