Understanding Literals in C: Direct Value Representation in Code

Discover the concept of literals in C, which represent direct values in the source code. Learn how variables can be initialized using literals, either directly or through expressions, and explore the different types of integer literals, including decimal, octal, hexadecimal, and binary formats.



C - Literals

In C, a literal is a direct representation of a value in the source code. Variables can be initialized either directly using literals or indirectly using expressions.

Integer Literals

Integer literals are whole numbers without a fractional part. They can be decimal, octal, or hexadecimal:

  • Decimal: int x = 200;
  • Octal: Prefix with 0. Example: int oct = 025;
  • Hexadecimal: Prefix with 0x or 0X. Example: int hex = 0xa1;
  • Binary: Prefix with 0b. Example: int x = 0b00010000;
Examples

#include <stdio.h>

int main() {
    int oct = 025;
    int hex = 0xa1;
    printf("Octal to decimal: %d\\n", oct);
    printf("Hexadecimal to decimal: %d\\n", hex);
    return 0;
}
Output

Octal to decimal: 21
Hexadecimal to decimal: 161

Floating-point Literals

Floating-point literals represent real numbers with an optional exponent symbol (e or E).

Examples

#include <stdio.h>

int main() {
    float x = 10.55;
    float y = -1.333;
    printf("x and y are: %f, %f\\n", x, y);
    return 0;
}
Output

x and y are: 10.550000, -1.333000

High precision floating-point literals use scientific notation:

Examples

#include <stdio.h>

int main() {
    float x = 100E+4;
    float y = -1.3E-03;
    printf("x: %f\\n", x);
    printf("y: %f\\n", y);
    return 0;
}
Output

x: 1000000.000000
y: -0.001300

Character Literals

Character literals are single characters enclosed in single quotes. For example, char x = 'I';

Example

#include <stdio.h>

int main() {
    char x = 'I';
    printf("x: %c\\n", x);
    printf("x: %d\\n", x);
    return 0;
}
Output

x: I
x: 73

Escape Sequences

Escape sequences start with a backslash (\) and represent non-printable characters like newline (\n).

Example

#include <stdio.h>

int main() {
    char x = 'I';
    char y = 'J';
    printf("x: %c\\ny: %c", x, y);
    return 0;
}
Output

x: I
y: J

Unicode Literals

Unicode character literals use \u followed by a Unicode code point.

Example

#include <stdio.h>

int main() {
    char x = '\u09A9';
    printf("x: %c\\n", x);
    printf("x: %d\\n", x);
    return 0;
}
Output

x: ⌐
x: -87

String Literals

String literals are sequences of characters enclosed in double quotes. Use arrays of char to store strings.

Example

#include <stdio.h>

int main() {
    char arr[] = "Hello World";
    printf("arr: %s", arr);
    return 0;
}
Output

arr: Hello World

String literals can include escape sequences and Unicode characters.

Arrays and structures can also be initialized with literals. For example:

  • Array: int arr[] = {10, 20, 30, 40};
  • Struct: struct marks m1 = {50, 60, 70};

Learn more about arrays and structures in later sections of the tutorial.

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.