C Constants

C Tutorial

 C Constants (Complete Guide: Beginner → Advanced)

In C language, a constant is a value that cannot be changed during program execution.
Constants improve code safety, readability, and maintainability, and they are very important for interviews.


 What is a Constant in C?

A constant is a fixed value whose value does not change while the program is running.

10, 3.14, ‘A’, “Hello”

 Types of Constants in C

C constants are mainly divided into:

  1. Numeric Constants

  2. Character Constants

  3. String Constants

  4. Symbolic Constants

  5. Enumeration Constants


 Numeric Constants

 Integer Constants

10, -5, 100

 Floating-Point Constants

3.14, -0.5, 2.0

Example


 Character Constants

A single character enclosed in single quotes.

‘A’, ‘9’, ‘$’

Example:

  • Stored internally as ASCII value

 String Constants

A sequence of characters enclosed in double quotes.

“Hello”
“Welcome to C”

Example:

  •  Automatically ends with '\0'

 Symbolic Constants using #define

Defined using preprocessor directive.

Syntax

#define PI 3.14159

Example


 

  •  No memory allocation
  •  No type checking

 Constants using const Keyword (Recommended)

Syntax

const data_type variable = value;

Example


 

  •  Type safe
  •  Preferred over #define

 Difference: #define vs const

Feature#defineconst
Type checking NoYes
ScopeGlobalBlock / Global
DebuggingHardEasy
MemoryNoYes
RecommendedLessMore

 Enumeration Constants (enum)

Used for named integer constants.


 

Values:

SUN = 0, MON = 1, TUE = 2, WED = 3
  •  Improves readability
  •  Used in real projects

const with Pointers (Interview Favorite)


 

  •  Very important for advanced interviews

 Common Mistakes

  •  Trying to modify a constant
  •  Confusing const int *p with int *const p
  •  Overusing #define 
  •  Forgetting string '\0'

 Interview Questions (Must Prepare)

  1. What is a constant in C?

  2. Difference between #define and const

  3. What are symbolic constants?

  4. What is enum?

  5. Explain const with pointer

  6. Are constants stored in memory?


Real-Life Use Cases

  • Mathematical values (PI, GRAVITY)

  • Configuration values

  • Array size limits

  • Status codes

  • Embedded systems


 Summary

  •  Constants store fixed values
  •  Use const for type safety
  • #define is preprocessor-based
  • enum for grouped constants
  •  Very important for clean code & interviews

You may also like...