Constants in C Language – BKNMU Junagadh | BCA Sem 1 Notes



BCA Sem 1  |  Unit 1  |  Computer Concepts & C Programming

Constants in C Language — BKNMU Junagadh

Literal Types, Rules, const Keyword, and #define Preprocessor Constants

BKNMU Junagadh NEP 2020 C Constants const vs #define Literals
In simple words: A Constant (also called a Literal) in C refers to fixed values that cannot be altered or modified by the program during its execution. Constants can be plain values like 100, 3.14, or 'A', or named variables whose values are locked using the const keyword or #define macro.
📖 Major Types of C Constants

C constants are fundamentally divided into two major groups: Numeric Constants and Character Constants.

100, -25, 075

1. Integer Constants

Whole numbers without decimal points. Supports Decimal, Octal (starts with 0), and Hexadecimal (starts with 0x).

3.14, -0.005

2. Real (Floating-Point)

Numbers containing fractional decimal parts or exponential notation (e.g., 1.5e3).

'A', '\n', "BKNMU"

3. Character & String

Single characters enclosed in single quotes ' ' or character sequences in double quotes " ".

1️⃣ Comprehensive Classification of Constants

Here is a detailed parameter breakdown of all constant types in C:

Constant TypeEnclosure / SyntaxDescription & RulesExamples
Decimal IntegerNoneBase-10 digits (0-9); no commas or spaces allowed10, -450, 999
Octal IntegerPrefix 0Base-8 digits (0-7); starts with a leading zero021, 075
HexadecimalPrefix 0x or 0XBase-16 digits (0-9 and A-F); starts with 0x0x1A, 0xFF
Real / FloatingDecimal point or eContains a decimal point or exponential notation (mantissa & exponent)3.14159, -0.85, 2.5e4
Single CharacterSingle Quotes ' 'A single character or escape sequence'a', '9', '#', '\n'
String ConstantDouble Quotes " "Sequence of characters ending with a hidden null character (\0)"Hello C", "BKNMU"
ℹ️ Important Difference: '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!
🔍 Two Ways to Create Named Constants in C
1. Using const Keyword
  • 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
2. Using #define Preprocessor
  • 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 (;)
💻 Code Sample — Using Constants in C

Observe how fixed values, const variables, and #define macros function together:

#include <stdio.h> // Method 1: Preprocessor Constant #define MAX_SCORE 100 int main() { // Method 2: const Keyword Constant const float PI = 3.14159; float radius = 5.0; // 5.0 is a floating-point literal float area = PI * radius * radius; printf("Circle Area: %.2f\n", area); printf("Max Score Limit: %d\n", MAX_SCORE); // PI = 3.14; // ERROR! Cannot reassign a const variable. return 0; }
💡 Exam Tip: When asked to compare 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!
🔄 Rules for Constructing Constants in C
1

No Commas or Spaces

Numeric constants cannot contain spaces, commas, or special currency symbols (e.g., 1,000 is invalid; write 1000).

2

Sign Requirement

A constant can be positive or negative. If unsigned, it defaults to positive.

3

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).

⚠️ Common Mistake: Adding a semicolon at the end of a #define directive like #define PI 3.14; will cause compilation errors wherever PI is substituted in math expressions!
📝 Quick Revision — Key Exam Points
  • 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 with 0x.
  • 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.

Post a Comment

Thanks for comment.

Previous Post Next Post