āĻāϏ⧠āĻāĻāĻĻāĻŽ zero āĻĨā§āĻā§ âC Constantsâ āĻŦā§āĻā§ āύā§āĻ â super beginnerâfriendly way te, Banglish mix e, many examples āϏāĻš â
C Constants â āĻā§ & āĻā§āύ?¶
Constant = āϝā§āĻāĻžāϰ value program āĻāϞāĻžāϰ āĻŽāϧā§āϝ⧠change āĻšāĻŦā§ āύāĻžāĨ¤
āϝā§āĻŽāύ: exam fee fixed 500 āĻāĻžāĻāĻžāĨ¤ Code āĻ 500 āĻšāĻā§āĻā§ āĻāĻāĻāĻž literal constant.
Analogy: Constant āĻŽāĻžāύ⧠āĻŦāĻžāϏā§āϰ ticket price āĻāϰ āĻŽāϤā§âprinting āĻšā§ā§ āĻā§āĻā§, āĻāϞ⧠āϝāĻžāĻā§āĻžāϰ āĻāĻā§ price āĻŦāĻĻāϞāĻžāĻŦā§ āύāĻž â
Big Picture (Flowchart đ§)¶
Start
ââ> Number āϞāĻžāĻāĻŦā§?
â ââ> Integer? â base (10/8/16) āĻ āĻŋāĻ āĻāϰ⧠â type & suffix (U/L/LL) āĻ āĻŋāĻ āĻāϰā§
â ââ> Floating? â decimal/scientific â suffix (f / l) āϞāĻžāĻāĻŦā§?
ââ> Character āϞāĻžāĻāĻŦā§? â 'A' āĻŦāĻž '\n' āĻŦā§āϝāĻŦāĻšāĻžāϰ
ââ> String āϞāĻžāĻāĻŦā§? â "Hello" (null-terminated)
ââ> Symbolic name āĻĻāϰāĻāĻžāϰ? â #define / const / enum
ââ> Compile-time constant āĻĻāϰāĻāĻžāϰ? â #define āĻŦāĻž enum (best)
1) Numeric Constants¶
1.1 Integer Constants (decimal, octal, hex)¶
| āϞāĻŋāĻāύ | Base | Example | Meaning | Notes |
|---|---|---|---|---|
| decimal | 10 | 42 |
42 | most common â |
| octal | 8 | 052 |
42 | leading 0 āĻŽāĻžāύ⧠octal â ī¸ |
| hex | 16 | 0x2A |
42 | AâF allowed |
Suffix (type hint):
Uâ unsignedLâ longLLâ long long- combine:
UL,LLU, etc. (order usually doesnât matter)
Examples:
â ī¸ Common mistakes
08āĻŦāĻž09āϞāĻŋāĻāϞ⧠errorâāĻāĻžāϰāĻŖ leading0āĻĻāĻŋāϞ⧠āϤāĻž octal āϧāϰāĻž āĻšā§, āĻāϰ octal-āĻ 8/9 āĻĨāĻžāĻā§ āύāĻž â- āĻŦā§ number āϞāĻŋāĻāϞ⧠overflow āĻšāϤ⧠āĻĒāĻžāϰā§; āĻāĻžāĻāĻĒ/suffix āĻŦā§āĻā§ āύāĻžāĻ (
LL,U) â
1.2 Floatingâpoint Constants (float/double/long double)¶
| āϞāĻŋāĻāύ | Example | Type (default) | Notes |
|---|---|---|---|
| Decimal | 3.14, .5, 2. |
double | dot āĻĨāĻžāĻāϞ⧠float family |
| Scientific | 1.23e3 = 1230 |
double | e āĻŦāĻž E |
| Suffix | 3.14f |
float | āĻā§āĻ āĻŽā§āĻŽāϰāĻŋ āĻĻā§āϰā§āϤ calc |
| Suffix | 3.14L |
long double | high precision |
Examples:
â ī¸ printf format mismatch āĻĻāĻŋāϞ⧠garbage/undefined output āĻāϏāϤ⧠āĻĒāĻžāϰā§:
float/doubleprint â%flong doubleâ%Lf
2) Character & String Constants¶
2.1 Character Constants: single quotes ' '¶
| Example | Meaning |
|---|---|
'A' |
char 'A' |
'\n' |
newline |
'\t' |
tab |
'\0' |
null (string terminator) |
'\x41' |
'A' in hex |
'\101' |
'A' in octal |
Escape quickâtable:
| Escape | Effect |
|---|---|
\\ |
backslash |
\' |
single quote |
\" |
double quote |
\n |
newline |
\t |
tab |
\0 |
null |
â ī¸ 'A' vs "A"
'A'â char (ā§§āĻāĻž character) â"A"â string (char array +'\0') â"A"āĻā§charāĻ assign āĻāϰāϞ⧠warning/error â
2.2 String Literals: double quotes " "¶
â ī¸ String immutable: literal modify āĻāϰāĻž āϝāĻžā§ āύāĻžāĨ¤
āĻāĻžāϞ⧠practice:
3) Symbolic Constants: #define, const, enum¶
āĻāĻā§āϞā§āϰ āĻāĻĻā§āĻĻā§āĻļā§āϝ: magic number/char āύāĻž āϞāĻŋāĻā§ meaningful name use āĻāϰāĻž â
3.1 #define (Preprocessor Macro)¶
- compile āĻšāĻāϝāĻŧāĻžāϰ āĻāĻā§āĻ text replace āĻšā§
- Compile-time constant āĻšāĻŋāϏā§āĻŦā§ use āĻšā§ (array size, case label āĻāϤā§āϝāĻžāĻĻāĻŋ)
- āĻāĻžāĻāĻĒ āύā§āĻ (type-less), āϤāĻžāĻ misuse āĻšāϞ⧠bug āĻāϏāϤ⧠āĻĒāĻžāϰ⧠â ī¸
3.2 const (Readâonly variable)¶
- type āĻāĻā§ (int/doubleâĻ)
- Block scope āĻ uninitialized const technically allowed, but use āĻāϰāĻž risky â ī¸ Always initialize â
- Preprocessor āύā§, āϤāĻžāĻ āĻāĻŋāĻā§ āĻāĻžā§āĻāĻžā§ compileâtime constant āύāĻžāĻ āϧāϰāĻž āĻšāϤ⧠āĻĒāĻžāϰ⧠(āĻŦāĻŋāĻļā§āώ āĻāϰ⧠āĻĒā§āϰā§āύ⧠standard/contexts)
3.3 enum (Enumerator constants)¶
- values are integers (constant expressions)
- Compile-time constant â array size, switch case, āĻāϤā§āϝāĻžāĻĻāĻŋāϤ⧠safe â
Quick Compare¶
| Feature | #define |
const |
enum |
|---|---|---|---|
| Type safety | â | â | â (int) |
| Compile-time expr | â | depends | â |
| Debug visibility | â (text replace) | â | â |
| Best for | global fixed numbers, feature flags | typed readâonly values | integer ids, array sizes |
4) Format Specifier CheatâSheet (for printf/scanf) đ§ž¶
| Type | Literal Example | printf |
scanf |
|---|---|---|---|
int |
42 |
%d |
%d |
unsigned int |
42U |
%u |
%u |
long |
42L |
%ld |
%ld |
long long |
42LL |
%lld |
%lld |
float |
3.14f |
%f (promoted to double) |
%f |
double |
3.14 |
%f |
%lf |
long double |
3.14L |
%Lf |
%Lf |
char |
'A' |
%c |
%c |
string |
"Hi" |
%s |
%s |
â ī¸ Common trap: scanf āĻ double āĻĒā§āϤ⧠%lf āĻāĻžāĻ; printf āĻ double print āĻāϰāϤ⧠%fāĨ¤
5) Initial Values (Context āĻ āύā§āϝāĻžā§ā§)¶
- Local nonâstatic variable (including
const) â uninitialized = garbage â ī¸
â Always initialize:
- Global/static variable â no initializer āĻĻāĻŋāϞ⧠default 0
- Literal constants (e.g.,
42,3.14) â āĻāĻĻā§āϰ āϤ⧠init āϞāĻžāĻā§ āύāĻž; āĻāϰāĻž direct value â
6) Lots of Tiny Examples (with expected outputs)¶
#include <stdio.h>
int main(void) {
// Integer bases
printf("%d\n", 052); // 42 (octal)
printf("%d\n", 0x2A); // 42 (hex)
// printf("%d\n", 08); // â invalid (8 not allowed in octal)
// Suffix
printf("%u\n", 3000000000U); // unsigned
printf("%lld\n", 1234567890123LL);
// Floating & scientific
printf("%f\n", 3.0); // 3.000000
printf("%f\n", 1.23e3); // 1230.000000
printf("%Lf\n", 3.14L); // needs %Lf
// Char & escapes
printf("%c\n", 'A'); // A
printf("Line1\\nLine2\n"); // prints: Line1\nLine2
printf("Line1\nLine2\n"); // real newline
// String & concatenation
printf("%s\n", "Hi" " There"); // Hi There
// Symbolic constants
#define TAX 0.15
const double rate = 0.15;
enum { MAXN = 1000 };
printf("%f %f %d\n", TAX, rate, MAXN);
return 0;
}
7) Quick Summary Tables¶
Numeric literal quickâref¶
| Need | Write like | Notes |
|---|---|---|
| Normal int | 123 |
%d |
| Unsigned | 123U |
%u |
| Long long | 123LL |
%lld |
| Hex | 0xFF |
base 16 |
| Octal | 077 |
leading 0 â ī¸ |
| Float | 3.14f |
%f |
| Double | 3.14 |
%f |
| Long double | 3.14L |
%Lf |
| Scientific | 6.02e23 |
big/small numbers |
Escape quickâref¶
| You want | Use |
|---|---|
| Newline | \n |
| Tab | \t |
| Quote inside | \" or \' |
| Backslash | \\ |
8) Choosing the Right Kind (Mini Flowchart)¶
Number?
ââ Integer? â big range āϞāĻžāĻāĻŦā§?
â ââ No â int (e.g., 100)
â ââ Yes â long long (e.g., 100LL) / unsigned if non-negative
ââ Floating? â precision āĻĻāϰāĻāĻžāϰ?
ââ Normal â double (e.g., 3.14)
ââ Very high â long double (e.g., 3.14L)
9) Pro Tips & Pitfalls¶
- â ī¸ Leading zero = octal â
010is 8, not 10! - â ī¸ Format mismatch â
doubleprint with%f, read with%lf. - â ī¸
'A'vs"A"mix up āĻā§āϰ⧠āύāĻž â char vs string. - â
Meaningful names use āĻāϰā§:
#define MAX_USERS 100orenum{MAX_USERS=100}; - â
Array size/case label āĻāϰ āĻāύā§āϝ
#define/enumsafer thanconstin some compilers. - â
Always initialize
const(esp. local/block scope). - â Readability first: big number āĻ underscore āύā§āĻ C āϤā§âgrouping comments āĻĻāĻžāĻ āĻŦāĻž hex āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠if helpful.
10) Mini Exercises (Try yourself)¶
-
āύāĻŋāĻā§āϰ literals āĻā§āϞā§āĻā§ āϏāĻ āĻŋāĻ format specifier āĻĻāĻŋā§ā§ print āĻāϰā§:
42U,1234567890123LL,3.5f,3.5,3.5L,'Z',"Hello" -
āĻā§āύāĻā§āϞ⧠valid? āĻā§āύ?
08,0x1F,'AB','\x4A',"A\0B" -
#define,const,enumâ āϤāĻŋāύ āĻāĻžāĻŦā§BUFFER_SIZE = 256āϏā§āĻ āĻāϰ⧠code āϞāĻŋāĻā§ āĻāĻāĻāĻŋ array declare āĻāϰā§āĨ¤
11) Final CheatâCard (copyâpaste friendly)¶
- Integer:
10,10U,10L,10LL,0xFF,077 - Float family:
3.0f(float),3.0(double),3.0L(long double),1e-3 - Char:
'A','\n','\x41' - String:
"Hello","Hi" " There" - Specifiers:
%d %u %ld %lld %f %Lf %c %s - Symbolic:
#define N 100,const int N=100;,enum{N=100}; - Always init const; beware leading
0; match format specifiers.
āĻ āĻŋāĻ āĻāĻā§ đ
āĻāĻŽāĻŋ āĻāĻĒāύāĻžāϰ āĻāύā§āϝ C Constants āĻāĻĒāĻŋāĻā§āϰ āĻāĻĒāϰ ā§Šā§ĻāĻāĻž MCQ āĻŦāĻžāύāĻŋā§ā§ āĻĻāĻŋāĻā§āĻāĻŋ â āĻāĻāĻĻāĻŽ beginner-friendly āĻĨā§āĻā§ āĻāĻāĻā§ tricky āĻĒāϰā§āϝāύā§āϤ, āϝāĻžāϤ⧠āĻāĻĒāύāĻŋ āϧāĻžāĻĒā§ āϧāĻžāĻĒā§ āĻŦā§āĻā§ āĻļāĻŋāĻāϤ⧠āĻĒāĻžāϰā§āĨ¤
āϏāĻŦāĻā§āϞ⧠MkDocs-MCQ plugin compatible format āĻ āĻĨāĻžāĻāĻŦā§, āϤāĻžāĻ āĻāĻĒāύāĻŋ āϏāϰāĻžāϏāϰāĻŋ .md āĻĢāĻžāĻāϞ⧠paste āĻāϰ⧠āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āĨ¤
āĻāĻŽā§āĻāĻžāϰ! āύāĻŋāĻā§ āĻāĻĒāύāĻžāϰ C Constants MCQâāĻā§āϞā§āĻā§ sectionâwise āĻāĻžāĻ āĻāϰ⧠collapsible accordion (MkDocs Material / Pymdown Admonitions) āĻāĻāĻžāϰ⧠āĻĻāĻŋāϞāĻžāĻŽāĨ¤
Syntax: ??? <type> "Title" â āϤāĻžāϰāĻĒāϰ āĻā§āϤāϰā§āϰ āĻāύā§āĻā§āύā§āĻ ā§Ēâspace indent. āϏāĻŦ MCQ official mcq format āĻāĨ¤
āύā§āĻ:
sectionāĻāĻžāĻāĻĒ āύā§āĻāĨ¤ Common āĻāĻžāĻāĻĒ:note,info,tip,example,questionāĻāϤā§āϝāĻžāĻĻāĻŋāĨ¤ āĻāĻŽāĻŋexampleāĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰā§āĻāĻŋ āϝāĻžāϤ⧠neutral āϞāĻžāĻā§āĨ¤