BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Constants in C Language — BKNMU Junagadh
Literal Types, Rules, const Keyword, and #define Preprocessor Constants
100, 3.14, or 'A', or named variables whose values are locked using the const keyword or #define macro.
C constants are fundamentally divided into two major groups: Numeric Constants and Character Constants.
1. Integer Constants
Whole numbers without decimal points. Supports Decimal, Octal (starts with 0), and Hexadecimal (starts with 0x).
2. Real (Floating-Point)
Numbers containing fractional decimal parts or exponential notation (e.g., 1.5e3).
3. Character & String
Single characters enclosed in single quotes ' ' or character sequences in double quotes " ".
Here is a detailed parameter breakdown of all constant types in C:
| Constant Type | Enclosure / Syntax | Description & Rules | Examples |
|---|---|---|---|
Decimal Integer | None | Base-10 digits (0-9); no commas or spaces allowed | 10, -450, 999 |
Octal Integer | Prefix 0 | Base-8 digits (0-7); starts with a leading zero | 021, 075 |
Hexadecimal | Prefix 0x or 0X | Base-16 digits (0-9 and A-F); starts with 0x | 0x1A, 0xFF |
Real / Floating | Decimal point or e | Contains a decimal point or exponential notation (mantissa & exponent) | 3.14159, -0.85, 2.5e4 |
Single Character | Single Quotes ' ' | A single character or escape sequence | 'a', '9', '#', '\n' |
String Constant | Double Quotes " " | Sequence of characters ending with a hidden null character (\0) | "Hello C", "BKNMU" |
'A' is a character constant requiring 1 byte of memory. "A" is a string constant requiring 2 bytes because C automatically adds a null character (\0) to the end!
- Uses compiler memory with explicit data type
- Syntax:
const float PI = 3.14; - Checked by compiler for type safety
- Scoped to function/block where declared
- Replaces text before compilation (no memory allocated)
- Syntax:
#define PI 3.14 - No data type checking; global macro scope
- Does not end with a semicolon (
;)
Observe how fixed values, const variables, and #define macros function together:
const and #define in exams, emphasize that const is handled by the compiler with type checking, whereas #define is handled by the preprocessor via text replacement!
No Commas or Spaces
Numeric constants cannot contain spaces, commas, or special currency symbols (e.g., 1,000 is invalid; write 1000).
Sign Requirement
A constant can be positive or negative. If unsigned, it defaults to positive.
Range Limits
Constants must stay within allowable memory bounds for their data type (e.g., standard 16-bit int ranges from -32,768 to 32,767).
#define directive like #define PI 3.14; will cause compilation errors wherever PI is substituted in math expressions!
- Definition: Fixed values that remain unchanged during program execution.
- Numeric Types: Integer (Decimal, Octal, Hexadecimal) and Real (Floating-point).
- Character Types: Single Character (
'A') and String Constants ("Hello"). - Octal vs Hex: Octal constants start with
0; Hexadecimal constants start with0x. - const Keyword: Creates read-only typed variables checked by the compiler.
- #define Directive: Preprocessor text replacement macro without type checking.
- Null Terminator: All string constants automatically end with
\0.