C Programs Tutorials | IT Developer
IT Developer

C Programming - C Literals



Share with a Friend

C Programming - C Literals

C Literals

In C, literals are fixed values directly embedded in the code. They represent constant values used in a program and cannot be changed during execution. Literals are categorized based on their type, such as integer, floating-point, character, string, etc.

Types of Literals in C

  1. Integer Literals
  • Represent whole numbers without a fractional part.
  • Can be written in decimal, octal, or hexadecimal notation.

Examples:

C

int decimal = 10;     // Decimal literal

int octal = 012;      // Octal literal (starts with 0)

int hexadecimal = 0xA; // Hexadecimal literal (starts with 0x or 0X)

Notation Base Example

Decimal (default)

Base 10

10

Octal

Base 8

012

Hexadecimal

Base 16

0xA

  1. Floating-Point Literals
  • Represent numbers with a fractional part.
  • Can be written in decimal or scientific/exponential notation.

Examples:

C

float decimal = 3.14;         // Decimal literal

float scientific = 3.14e2;    // Scientific notation (equivalent to 3.14 × 10^2)

float negative = -0.001;      // Negative floating-point literal

Form Example

Decimal

3.14

Scientific

3.14e2

  1. Character Literals
  • Represent single characters enclosed in single quotes (').
  • Each character literal corresponds to an ASCII value.

Examples:

C

char letter = 'A';       // Character literal

char number = '5';       // Character literal (not numeric)

char newline = '\n';     // Escape sequence

Type Example

Single character

'A'

Escape sequence

'\n', '\t'

ASCII value representation

'\x41' (Hex for 'A')

  1. String Literals
  • Represent a sequence of characters enclosed in double quotes (").
  • Always end with a null character (\0).

Examples:

C

char str[] = "Hello, World!";   // String literal

  1. Boolean Literals
  • Introduced with <stdbool.h> in C99.
  • Represent true or false values.

Examples:

C

#include <stdbool.h>

bool isValid = true;   // Boolean literal

Value Literal

True

true

False

false

  1. Constant Literals
  • Represent constant values using the const keyword.

Examples:

C

const int MAX = 100;   // Constant integer literal

Escape Sequences in Character and String Literals

Escape sequences are special character literals that represent non-printable or control characters.

Escape Sequence Description

\n

Newline

\t

Tab

\b

Backspace

\\

Backslash

\'

Single quote

\"

Double quote

\0

Null character

Rules for Literals

  1. Integer Literals:
    • No decimal point is allowed.
    • Must fit within the range of the data type (e.g., int, long).
  2. Floating-Point Literals:
    • Must include a decimal point or use scientific notation.
  3. Character Literals:
    • Enclosed in single quotes and represent one character only.
  4. String Literals:
    • Enclosed in double quotes and represent multiple characters.

Examples of Different Literals in a Program

C

#include <stdio.h>

#include <stdbool.h>

int main() {

    int decimal = 42;           // Integer literal

    float pi = 3.14159;         // Floating-point literal

    char letter = 'C';          // Character literal

    char newline = '\n';        // Escape sequence

    const char *message = "Hello, World!"; // String literal

    bool isActive = true;       // Boolean literal

    printf("Integer: %d\n", decimal);

    printf("Float: %.2f\n", pi);

    printf("Character: %c\n", letter);

    printf("String: %s\n", message);

    printf("Boolean: %d\n", isActive);  // Output: 1 (true)

    return 0;

}

Key Points

  1. Literals represent fixed values in code and are immutable.
  2. Different types of literals exist to match the data types used in C programs.
  3. Escape sequences are used to represent non-printable characters.
  4. Use const for literals you don't want to change.

Literals are fundamental in C, forming the building blocks for initializing and manipulating data.