OnlineCBT Logo OnlineCBT
C Tutorial
C · LESSON 4

Constants and Keywords

By Ratan Updated: 15 Oct 2026

Define read-only variables using const keyword and #define preprocessor macros.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Give fixed values meaningful names

Constants describe values that should remain fixed during an operation. Keywords are reserved words that define the language grammar. This lesson separates a typed read-only object from preprocessor text substitution, two mechanisms beginners often treat as identical.

const objects

const double tax_rate = 0.05;
const int batch_capacity = 30;

After initialization, assignment through these identifiers is not permitted. const helps the compiler detect accidental modification. It does not mean that every const object is a compile-time integer constant expression in C. For a fixed integer array bound at file scope, an enum constant is often suitable.

Macros and enum constants

#define APP_NAME "OnlineCBT"
enum { MAX_ATTEMPTS = 3 };

A macro performs token replacement before compilation. It has no independent C type or storage. Avoid adding a semicolon to the macro replacement: it becomes part of every expansion. Enum constants are useful named integers; their declarations follow C's normal syntax and end with a semicolon.

Complete example

#include 
#define APP_NAME "OnlineCBT"
enum { MAX_ATTEMPTS = 3 };
int main(void) {
    const double price = 200.0;
    const double discount_rate = 0.10;
    double payable = price * (1.0 - discount_rate);
    printf("%s: attempts=%d, payable=%.2f\n",
           APP_NAME, MAX_ATTEMPTS, payable);
    return 0;
}

Expected output: OnlineCBT: attempts=3, payable=180.00. The names explain the calculation better than unexplained literal values. For actual currency accounting, consider integer minor units and an explicit rounding rule rather than assuming binary floating point gives exact decimal accounting.

Keywords versus identifiers

int, return, if, while and const are keywords and cannot be your variable names. C is case-sensitive: total and Total are distinct. Prefer meaningful lower-case names for objects and an established convention such as upper case for macros. Avoid names reserved by the implementation, including identifiers beginning with a double underscore.

Common mistakes

  • const int limit; inside a function leaves an uninitialized object that you cannot then initialize with ordinary assignment.
  • A macro such as #define LIMIT 10; can break an expression because of the unwanted semicolon.
  • Changing every literal into a macro does not automatically improve clarity. A local const often provides better type checking.

Exercise

Write a fare calculation with a named base fare and a named per-kilometre rate. For base 20, rate 8 and distance 5, the answer is 60. Identify which values belong in user input and which are configured constants. Then attempt to assign a new value to a const rate and explain the compiler diagnostic.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat