Skip to content

āφāϏ⧋ āĻāĻ•āĻĻāĻŽ 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 → unsigned
  • L → long
  • LL → long long
  • combine: UL, LLU, etc. (order usually doesn’t matter)

Examples:

123        // int
123U       // unsigned int
123L       // long
123LL      // long long
0xFFU      // unsigned hex

âš ī¸ Common mistakes

  • 08 āĻŦāĻž 09 āϞāĻŋāĻ–āϞ⧇ error—āĻ•āĻžāϰāĻŖ leading 0 āĻĻāĻŋāϞ⧇ āϤāĻž 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:

3.0      // double
3.0f     // float
6.022e23 // double (Avogadro)
1.0L     // long double

âš ī¸ printf format mismatch āĻĻāĻŋāϞ⧇ garbage/undefined output āφāϏāϤ⧇ āĻĒāĻžāϰ⧇:

  • float/double print → %f
  • long 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 " "

"Hello"        // 'H' 'e' 'l' 'l' 'o' '\0'
"Hi" " There"  // auto-concatenate → "Hi There"

âš ī¸ String immutable: literal modify āĻ•āϰāĻž āϝāĻžā§Ÿ āύāĻžāĨ¤

char *s = "Hi";
s[0] = 'B'; // ❌ undefined behavior

āĻ­āĻžāϞ⧋ practice:

char s[] = "Hi"; // modifiable copy
s[0] = 'B';      // ✅ "Bi"

3) Symbolic Constants: #define, const, enum

āĻāϗ⧁āϞ⧋āϰ āωāĻĻā§āĻĻ⧇āĻļā§āϝ: magic number/char āύāĻž āϞāĻŋāϖ⧇ meaningful name use āĻ•āϰāĻž ✅

3.1 #define (Preprocessor Macro)

#define PI 3.1415926535
#define MAX_STUDENTS 60
#define NEWLINE '\n'
  • compile āĻšāĻ“āϝāĻŧāĻžāϰ āφāϗ⧇āχ text replace āĻšā§Ÿ
  • Compile-time constant āĻšāĻŋāϏ⧇āĻŦ⧇ use āĻšā§Ÿ (array size, case label āχāĻ¤ā§āϝāĻžāĻĻāĻŋ)
  • āϟāĻžāχāĻĒ āύ⧇āχ (type-less), āϤāĻžāχ misuse āĻšāϞ⧇ bug āφāϏāϤ⧇ āĻĒāĻžāϰ⧇ âš ī¸

3.2 const (Read‑only variable)

const int LIMIT = 100;     // usually initialize āĻ•āϰāĻŦ⧇
const double RATE = 0.05;
  • 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)

enum { BUF_SIZE = 1024, TIMEOUT_MS = 5000 };
  • 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 âš ī¸
void f(void){
    const int x;   // allowed, but value indeterminate ❌
    // printf("%d", x); // UB
}

✅ Always initialize:

const int x = 10;
  • Global/static variable — no initializer āĻĻāĻŋāϞ⧇ default 0
static const int k; // becomes 0 by default, but better to set explicitly
  • 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 → 010 is 8, not 10!
  • âš ī¸ Format mismatch → double print with %f, read with %lf.
  • âš ī¸ 'A' vs "A" mix up āϕ⧋āϰ⧋ āύāĻž — char vs string.
  • ✅ Meaningful names use āĻ•āϰ⧋: #define MAX_USERS 100 or enum{MAX_USERS=100};
  • ✅ Array size/case label āĻāϰ āϜāĻ¨ā§āϝ #define/enum safer than const in 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)

  1. āύāĻŋāĻšā§‡āϰ literals āϗ⧁āϞ⧋āϕ⧇ āϏāĻ āĻŋāĻ• format specifier āĻĻāĻŋā§Ÿā§‡ print āĻ•āϰ⧋:
    42U, 1234567890123LL, 3.5f, 3.5, 3.5L, 'Z', "Hello"

  2. āϕ⧋āύāϗ⧁āϞ⧋ valid? āϕ⧇āύ?
    08, 0x1F, 'AB', '\x4A', "A\0B"

  3. #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 āϞāĻžāϗ⧇āĨ¤