C Programming - Beginner to Brilliance / Basics of C Programming

Constants & Literals

🧾 What is a Constant?

In C, a constant is a value that cannot be changed once it is defined.

It’s like labeling something as “Do Not Touch” in your code.

Think of it like your date of birth — you cannot change it, even though you can use it anywhere.


🧪 Declaring a Constant

✅ Method 1: Using the const keyword

const int maxMarks = 100;
  • Here, maxMarks is a constant integer.

  • If you try to change it later like maxMarks = 90;, you'll get a compile-time error.

✅ Method 2: Using #define preprocessor macro

#define PI 3.14159
  • PI is now a macro constant — this is replaced before compilation.

  • You can’t change it later in the code.

🔎 Difference between const and #define:

Feature

const

#define

Type-safe?

✅ Yes

❌ No (just text replacement)

Memory allocated?

✅ Yes (variable)

❌ No

Scope?

Local or Global

Global

Debugging support

✅ Better

❌ Poor


🔄 Example:

#include <stdio.h>
#define PI 3.14

int main() {
    const int MAX = 100;

    printf("Value of PI = %f\\n", PI);
    printf("Max limit = %d\\n", MAX);

    // PI = 3.14159;  ❌ Error (with const) — can't modify
    // MAX = 200;     ❌ Error — can't modify const variable

    return 0;
}

🔥 Why Use Constants?

  • 🚫 Avoid Accidental Changes

  • 🔄 Make Code Easy to Update (Change value in one place)

  • 💡 Improve Readability & Meaning (e.g., MAX_SPEED is better than using 120 everywhere)


🔍 What is a Literal?

A literal is a value that appears directly in your code.

It’s the actual number or character or string used in your code.

📘 Examples of literals:

25        // integer literal
3.14      // float literal
'A'       // character literal
"Hello"   // string literal

🧬 Types of Literals in C

Let’s break them down:

🔢 Integer Literals

Values without decimal points:

int x = 100;      // decimal (base 10)
int y = 0xFF;     // hexadecimal (base 16)
int z = 075;      // octal (base 8

🌊 Floating-Point Literals

Values with decimal points:

float pi = 3.14;
double radius = 2.718281828;

You can also use scientific notation:

float distance = 1.2e5;  // = 1.2 × 10^5 = 120000.0

🔤 Character Literals

A single character enclosed in single quotes:

char grade = 'A';
char symbol = '$';

Every character literal is stored as an integer ASCII value internally.

char c = 'A';   // ASCII of A = 65

You can even print:

printf("%d", 'A');  // prints 6🧵 **D. String Literals**

A sequence of characters enclosed in double quotes:

char message[] = "Hello, World!";
  • It ends with a special null character \\0

  • Stored as an array of characters


⚙️ Escape Sequence Literals

Special literals used inside strings/characters:

Literal

Meaning

\\n

New Line

\\t

Tab Space

\\\\

Backslash

\\"

Double Quote

\\'

Single Quote

Example:

printf("Hello\\nWorld\\tC");  // Prints on two lines with a tab
Hello
World   C

✅ Best Practices

  • Use const instead of #define for typed constants

  • Use all UPPERCASE names for constants: MAX_SIZE, PI, LIMIT

  • Do not use magic numbers (e.g., don’t write if (x > 500), use MAX_LIMIT instead)


🧪 Difference Between Constants and Literals

Feature

Constant

Literal

Can be named?

✅ Yes (const int x = 100)

❌ No (just value: 100)

Modifiable?

❌ No

❌ No

Use in code?

Defined once, used everywhere

Appears directly in code

Example

const int MAX = 100;

100