Symbolic Constants in C: #define, Naming Rules and Examples
A symbolic constant in C is a name that represents a fixed value, usually created with #define like #define PI 3.14159. Learn rules, examples and benefits.
What is a symbolic constant?
3.14159 everywhere, you give it the symbol PI once and use the name. In C, symbolic constants are usually created with #define.#define PI 3.14159
#define MAX_STUDENTS 60
#define SCHOOL "Alpine Public School"
Syntax and rules
#define SYMBOL_NAME value
- No semicolon at the end and no equals sign.
- Written at the top of the file, before
main(). - Convention: symbolic constant names are written in CAPITAL LETTERS (PI, MAX_SIZE, TAX_RATE) so they stand out from variables.
- The preprocessor replaces every occurrence of the symbol with the value before compilation.
Complete example
#include <stdio.h>
#define PI 3.14159
#define PASSING_PERCENT 33
int main() {
float radius = 7.0f;
float area = PI * radius * radius;
printf("Area of circle : %.2f\n", area);
printf("Passing percent : %d%%\n", PASSING_PERCENT);
return 0;
}
Why use symbolic constants? (3 real benefits)
- Single point of change: If the passing percentage changes from 33 to 35, you edit one #define line instead of hunting 50 places in the code.
- Readable code:
if (marks >= PASSING_PERCENT)explains itself;if (marks >= 33)is a mystery "magic number". - Fewer typing mistakes: Typing 3.14159 twenty times invites typos like 3.14195; typing PI does not.
Symbolic constant vs const variable
| Point | #define symbolic constant | const variable |
|---|---|---|
| Created by | Preprocessor | Compiler |
| Type checking | No | Yes |
| Memory/address | No | Yes |
| Works in case labels | Yes | No (in C) |
Both are called "constants" in exams, but the classic textbook term symbolic constant almost always means the #define style.
Symbolic constant क्या है?
3.14159 हर जगह लिखने की बजाय, उसे एक बार symbol PI देकर name use करते हैं. C में symbolic constants आमतौर पर #define से बनते हैं.#define PI 3.14159
#define MAX_STUDENTS 60
#define SCHOOL "Alpine Public School"
Syntax और Rules
#define SYMBOL_NAME value
- अंत में semicolon नहीं और equals sign नहीं.
- File के top पर,
main()से पहले लिखा जाता है. - Convention: symbolic constant names CAPITAL LETTERS में लिखे जाते हैं (PI, MAX_SIZE, TAX_RATE) ताकि variables से अलग दिखें.
- Preprocessor compilation से पहले symbol की हर occurrence को value से replace कर देता है.
पूरा Example
#include <stdio.h>
#define PI 3.14159
#define PASSING_PERCENT 33
int main() {
float radius = 7.0f;
float area = PI * radius * radius;
printf("Area of circle : %.2f\n", area);
printf("Passing percent : %d%%\n", PASSING_PERCENT);
return 0;
}
Symbolic constants क्यों use करें? (3 real फायदे)
- एक जगह change: अगर passing percentage 33 से 35 हो जाए, तो code में 50 जगह ढूंढने की बजाय एक #define line edit करनी है.
- Readable code:
if (marks >= PASSING_PERCENT)खुद को explain करता है;if (marks >= 33)एक mystery "magic number" है. - कम typing mistakes: 3.14159 बीस बार type करने में 3.14195 जैसे typos हो जाते हैं; PI type करने में नहीं.
Symbolic constant vs const variable
| Point | #define symbolic constant | const variable |
|---|---|---|
| बनाता कौन है | Preprocessor | Compiler |
| Type checking | नहीं | हां |
| Memory/address | नहीं | हां |
| case labels में काम | हां | नहीं (C में) |
Exams में दोनों को "constants" कहा जाता है, लेकिन classic textbook term symbolic constant लगभग हमेशा #define style को कहते हैं.
Frequently Asked Questions
What is a symbolic constant in C?
A symbolic constant is a name that represents a fixed value, usually created with #define, such as #define PI 3.14159. The preprocessor replaces the name with the value before compilation.
Why are symbolic constants written in capital letters?
It is a naming convention, not a language rule: capital letters make constants instantly distinguishable from normal variables in the code.
Where should #define be written in a program?
Usually at the top of the file after #include lines and before main(), so the symbol is available to the entire file.