C में Constant Define करने के 3 तरीके (const, #define, enum)
C में constant define करने के 3 तरीके हैं: const keyword, #define preprocessor directive और enum. Syntax, examples और सही choice यहां सीखें.
How to define a constant in C
const keyword, the #define preprocessor directive, and enum. Each works differently and each has a best use case.Way 1: const keyword
const int MAX_STUDENTS = 60;
const double GST_RATE = 0.18;
const creates a real variable in memory that the compiler marks read-only. It has a data type, obeys scope rules and shows up properly in a debugger. This is the preferred modern way for typed constant values.
Way 2: #define directive
#define MAX_STUDENTS 60
#define PI 3.14159
#define SCHOOL_NAME "Alpine Public School"
#define is handled by the preprocessor before compilation. Every occurrence of the name is textually replaced by the value — no memory, no type, no scope. It works everywhere a literal works, including case labels and array sizes in old C standards.
#define MAX = 60; is a classic beginner mistake — the = 60; text gets pasted into your code and breaks it.Way 3: enum
enum { MIN_AGE = 5, MAX_AGE = 18, CLASS_SIZE = 40 };
int students[CLASS_SIZE]; // valid: enum is a true compile-time constant
enum creates named integer constants that are true compile-time constants. It is the cleanest way to define a group of related integer constants without polluting the code with many #define lines.
Full program using all 3 ways
#include <stdio.h>
#define PI 3.14159 /* Way 2: preprocessor */
enum { MAX_ITEMS = 5 }; /* Way 3: enum */
int main() {
const float RADIUS = 7.0f; /* Way 1: const */
float area = PI * RADIUS * RADIUS;
printf("Area = %.2f\n", area);
printf("Max items = %d\n", MAX_ITEMS);
return 0;
}
Which one should you use?
| Situation | Best choice |
|---|---|
| A single typed value (float, double, struct, pointer) | const |
Value needed in case labels or conditional compilation (#if) | #define |
| A group of related integer constants | enum |
| String constant text used in many files | #define or const char * |
C में constant कैसे define करें
const keyword, #define preprocessor directive, और enum. तीनों अलग तरह से काम करते हैं और हर एक का अपना best use case है.तरीका 1: const keyword
const int MAX_STUDENTS = 60;
const double GST_RATE = 0.18;
const memory में एक real variable बनाता है जिसे compiler read-only mark कर देता है. इसका data type होता है, scope rules follow करता है और debugger में सही दिखता है. Typed constant values के लिए यह modern preferred तरीका है.
तरीका 2: #define directive
#define MAX_STUDENTS 60
#define PI 3.14159
#define SCHOOL_NAME "Alpine Public School"
#define को preprocessor compilation से पहले handle करता है. Name की हर occurrence value से textually replace हो जाती है — न memory, न type, न scope. यह हर उस जगह काम करता है जहां literal काम करता है, जैसे case labels.
#define MAX = 60; लिखना classic beginner mistake है — = 60; text आपके code में paste होकर उसे तोड़ देता है.तरीका 3: enum
enum { MIN_AGE = 5, MAX_AGE = 18, CLASS_SIZE = 40 };
int students[CLASS_SIZE]; // valid: enum true compile-time constant है
enum named integer constants बनाता है जो true compile-time constants होते हैं. Related integer constants के group को define करने का यह सबसे clean तरीका है — कई सारी #define lines की ज़रूरत नहीं.
तीनों तरीकों वाला पूरा program
#include <stdio.h>
#define PI 3.14159 /* तरीका 2: preprocessor */
enum { MAX_ITEMS = 5 }; /* तरीका 3: enum */
int main() {
const float RADIUS = 7.0f; /* तरीका 1: const */
float area = PI * RADIUS * RADIUS;
printf("Area = %.2f\n", area);
printf("Max items = %d\n", MAX_ITEMS);
return 0;
}
कौन-सा तरीका कब use करें?
| Situation | Best choice |
|---|---|
| Single typed value (float, double, struct, pointer) | const |
case labels या conditional compilation (#if) में ज़रूरी value | #define |
| Related integer constants का group | enum |
| कई files में use होने वाला string constant | #define या const char * |
Frequently Asked Questions
C में constant define करने के 3 तरीके कौन-से हैं?
C में constant const keyword, #define preprocessor directive, या integer constants के groups के लिए enum से define कर सकते हैं.
क्या #define में semicolon लगता है?
नहीं. #define एक preprocessor directive है, statement नहीं, इसलिए इसमें semicolon या equals sign नहीं लगता.
const और #define में बेहतर कौन है?
आमतौर पर const बेहतर है क्योंकि यह type-checked और scoped होता है, लेकिन case labels और conditional compilation के लिए #define ज़रूरी है.