Constants & Literals in C

A constant in C is a value that cannot be changed while the program is running. Constants make programs safer, clearer, and prevent accidental modification of important data.

What Are Constants?

A constant is a fixed value used in a C program. Once assigned, its value cannot be altered during program execution.

Examples: 10, 3.14, 'A', "Hello"

Types of Constants in C

C supports several types of constants:

1. Integer Constants

These represent whole numbers (no decimal point).

10  
-45  
2500  
0  

Forms of Integer Constants

2. Floating (Real) Constants

These involve decimal values or exponential notation.

3.14  
-2.50  
6.02e23
7.0E-8

3. Character Constants

A character constant contains a single character enclosed in single quotes.

'A'
'5'
'$'
'\n'
'\t'

Escape Characters

\n → newline  
\t → tab  
\\ → backslash  
\' → single quote  
\" → double quote  

4. String Constants

Strings are sequences of characters enclosed in double quotes.

"Hello"
"Welcome to C"
"12345"
Every string ends with a hidden null character → \0

5. Symbolic Constants

Symbolic constants are defined using #define. They help avoid magic numbers and make code readable.

#define PI 3.14159
#define MAX 100

Once defined, PI and MAX cannot be changed.

Literal Values

"Literals" simply mean the raw values written directly in code.

10      // integer literal  
3.5     // float literal  
'A'     // char literal  
"Hi"    // string literal  

Every literal is also a constant because its value is fixed.

Examples of Constants

const int age = 18;
const float GST = 18.0;
const char grade = 'A';
Using const is a safer alternative to #define.

Difference Between const and #define

const:
- Has data type
- Checked by compiler
- Stored in memory

#define:
- No data type
- Preprocessor replacement
- No memory allocated

Diagram: Types of Constants

        CONSTANTS IN C
      --------------------
      | Integer         |
      | Floating        |
      | Character       |
      | String          |
      | Symbolic (#define) |
      --------------------

Practical Example 1 – Using Constants & Literals

#include <stdio.h>

#define MAX 100
const float PI = 3.14;

int main() {

    int roll = 25;
    float radius = 7;

    printf("Roll Number: %d\n", roll);
    printf("Maximum Limit: %d\n", MAX);
    printf("Circle Area: %.2f\n", PI * radius * radius);

    return 0;
}

Practical Example 2 – Using Multiple Literal Types

#include <stdio.h>

int main() {

    int day = 7;                // integer literal
    float temp = 32.6;          // float literal
    char grade = 'A';           // character literal
    char name[] = "Sourav";     // string literal

    printf("Day Number: %d\n", day);
    printf("Temperature: %.1f°C\n", temp);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}

Practical Example 3 – Using Symbolic Constants

#include <stdio.h>

#define GST 18
#define COMPANY "WebTechIO"

int main() {

    float price = 2500;
    float tax = (price * GST) / 100;

    printf("Company: %s\n", COMPANY);
    printf("Base Price: %.2f\n", price);
    printf("GST (18%%): %.2f\n", tax);
    printf("Total Price: %.2f\n", price + tax);

    return 0;
}

Practical Example 4 – Using Escape Sequence Literals

#include <stdio.h>

int main() {

    printf("Hello\\nWorld\n");
    printf("Tabbed\\tText: \tSourav\n");
    printf("Quote Example: \\\"C Programming\\\" \n");
    printf("Backslash Example: \\\\ \n");

    return 0;
}

Practical Example 5 – Constant Expressions

#include <stdio.h>

#define BASE 50
#define BONUS 20

int main() {

    const int TOTAL = BASE + BONUS;  
    const float RATE = 2.5 * 3.2;

    printf("Total Value: %d\n", TOTAL);
    printf("Rate Value: %.2f\n", RATE);

    return 0;
}

Practice Questions

  1. What are constants? List different types.
  2. Differentiate between integer and floating constants.
  3. What is a character constant? Give examples.
  4. What is the difference between const and #define?
  5. Write a program using #define to calculate area of a rectangle.

Practice Task

Write a C program that uses:
✔ 2 integer constants
✔ 1 float constant
✔ 1 symbolic constant
✔ 1 string literal
and prints all values clearly.