Topic 18 of 100 Data Types and Variables Interactive Tutorial

Literals in C Programming: Integer, Character, Floating, and Other Forms

10 mins read Last updated: 22 Aug 2026 16,900 views
C Compiler 0.00%

What is Literals in C Programming? Core Concept & Direct Summary

Core Objective & Problem Statement

What are literal constants in C, and what suffixes/prefixes determine their base and type?

Direct Answer Summary

Look at this line of code:

You already knowintis the type andscoreis the variable. But what about that95? It's not a variable — it can't change, can't be reassigned, can't do anything exceptbe exactly what it is, forever. That fixed, hard-coded value has a name: aliteral.

Literals are the raw ingredients of every program — the actual numbers, characters, and text you bake directly into your code. And while they look simple, C's literals come with a surprising collection of forms, suffixes, and base systems that trip up even experienced programmers. (Did you know042is not 42 in C? Read on.)

In this guide, you'll learn every literal type C recognizes — integers in four number systems, decimals with their sneaky suffixes, characters and their ASCII secrets, strings and their invisible terminator — with the traps flagged before you step in them.

How Does Literals in C Programming Work? In-Depth Mechanics & Architecture

What Is a Literal in C?

Aliteralis a fixed value written directly into source code. Unlike a variable, a literal has no name, no memory address you can change, and no possibility of becoming something else.95is95forever.

main.c
Run Online
int score = 95;
// 95 is a literalscore = 100;
// 100 is a literal;
score is the variable// 95 = 100;
// ❌ nonsense — literals can't be assigned to!

Think of it this way:variables are boxes; literals are the items placed inside them.You can swap items in a box, but an item itself never changes shape.

C recognizes five literal families:

Literal TypeExampleNotes
Integer42,0x2A,100LWhole numbers — in four bases!
Floating-point3.14,2.5e3,9.99fDecimals and scientific notation
Character'A','\n','7'Single quotes, one character
String"Hello","A"Double quotes, secretly an array
Booleantrue,falseVia<stdbool.h>or native in C23

Let's open each one.

Integer Literals: One Number, Four Number Systems

Here's where C gets unexpectedly interesting. The same value can be written infour different bases:

main.c
Run Online
int a = 42;
// decimal (base 10) — the everyday wayint b = 052;
// octal (base 8) — leading 0int c = 0x2A;
// hexadecimal (base 16) — leading 0xint d = 0b101010;
// binary (base 2) — leading 0b (C23 standard)
printf("%d %d %d %d\n", a, b, c, d);
// 42 42 42 42 — all identical!

All four print42— they're the same value in different costumes.

Why do hex and octal exist?

Because programmers work close to hardware, where binary matters:

main.c
Run Online
int red = 0xFF0000;
// RGB red — hex matches byte structure perfectlyint permissions = 0755;
// Unix file permissions are traditionally octalint flags = 0b1010;
// see exactly which bits are on (C23)

0xFF0000tells a graphics programmerinstantlywhich bytes are set —16711680(the same number in decimal) tells them nothing.

⚠️ The Octal Trap

The leading zero means octal — and this causes real bugs:

main.c
Run Online
int pincode = 042;
// NOT forty-two! It's octal 42 = 34 in decimal
int month = 09;
// 💥 ERROR — 9 doesn't exist in octal (digits: 0-7)

Rule:never write a leading zero before an integer unless you genuinely mean octal. This single rule has saved programmers hours of confusion since 1972.

Suffixes: Choosing the Type

Integer literals default toint, but suffixes change their type:

main.c
Run Online
int normal = 100;
long big = 100L;
// L → long
unsigned u = 100U;
// U → unsigned
long long huge = 100LL;
// LL → long long
unsigned long ul = 100UL;
// combos work too

Floating-Point Literals: Decimals and Scientific Notation

Decimal values come with their own rules and one famous suffix:

main.c
Run Online
double pi = 3.14159;
// decimal literals are DOUBLE by default
float price = 19.99f;
// f suffix makes it a float
long double precise = 3.14L;
// L suffix for long double

The Default-Is-Double Detail

This catches everyone once:every decimal literal is adoubleunless you say otherwise.

main.c
Run Online
float value = 3.14;
// ⚠️ 3.14 is a double being squeezed into a float
float value = 3.14f;
// ✅ a true float from birth

The first line works — compilers allow it — but it quietly converts double→float, and precision-obsessed compilers will warn about it. Add thef; it's free.

Scientific Notation

For very large or small numbers, C speaks scientific notation:

main.c
Run Online
double speedOfLight = 2.998e8;
// 2.998 × 10⁸ = 299,800,000
double electronMass = 9.11e-31;
// 9.11 × 10⁻³¹ (tiny!)
printf("%f\n", 1.5e3);
// 1500.000000

Theemeans "times ten to the power of" —2.998e8reads as 2.998 × 10⁸.

Character Literals: Single Quotes, Single Character

Acharacter literalis exactly one character wrapped insingle quotes:

main.c
Run Online
char grade = 'A';
char digit = '7';
// the CHARACTER seven, not the number!char symbol = '@';
char newline = '\n';
// escape sequences count as ONE character

The ASCII Secret (Again, Because It Matters)

Every character literal is actually an integer underneath — its ASCII code:

main.c
Run Online
printf("%d\n", 'A');
// 65printf("%d\n", '7');
// 55 — NOT 7! It's the character's codeprintf("%c\n", 65);
// A — the code becomes the character

So'7' + 1equals56(the character'8'), not the number 8. Character ≠ digit — the quotes change everything.

Escape Sequences as Character Literals

Special characters use backslash escapes, and each is stillonecharacter:

main.c
Run Online
char tab = '\t';
char quote = '\'';
char backslash = '\\';

Deep-dive waiting for you:C Escape Sequences.

String Literals: Text With a Hidden Passenger

Wrap characters indouble quotesand you get a string literal:

main.c
Run Online
char greeting[] = "Hello";
printf("%s\n", "Direct strings work too");

Two facts every C programmer must know:

1. Every string literal secretly ends with'\0'— the null terminator:

main.c
Run Online
"Hello" // actually stored as: 'H' 'e' 'l' 'l' 'o' '\0'  (6 bytes, not 5!)

2.'A'and"A"are completely different things:

main.c
Run Online
char c = 'A';
// ✅ char — ONE byte, holds 65
char s[] = "A";
// ✅ string — TWO bytes: 'A' + '\0'
// char bad = "A"; // ❌ ERROR — can't store a string in a char

3. String literals are read-only.Modifying one through a pointer (char *s = "Hi"; s[0] = 'b';) is undefined behavior — programs crash over this. If you need editable text, copy it into a char array.

Boolean Literals: The Newest Family Member

Classic C had no boolean type — programmers used0(false) and any non-zero value (true). Modern C fixed this:

main.c
Run Online
#include <stdbool.h> // C99 approach
bool isReady = true;
bool hasError = false;

And inC23,trueandfalsebecame native keywords — no header needed at all. A small luxury, fifty years in the making.

The Complete Literal Cheat Sheet

LiteralTypeMeans
42intDecimal integer
042int (octal)34 in decimal!
0x2Aint (hex)42 in decimal
42L/42Ulong / unsignedSuffix changes type
3.14doubleDecimal — double by default
3.14ffloatThe f makes it float
1.5e3doubleScientific: 1500.0
'A'charOne character (ASCII 65)
'\n'charEscape sequence — still one char
"Hello"stringChar array + hidden\0
true/falseboolstdbool.h or native C23

The 4 Literal Traps (Memorize These)

  1. Leading zero = octal.int x = 010;gives you 8, not 10.
  2. Decimals are doubles.3.14withoutfis a double — add the suffix for floats.
  3. 'A'≠"A".Character vs string — different types, different sizes, different rules.
  4. '7'≠ 7.A quoted digit is a character with an ASCII code (55), not the number.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#include <stdio.h>
int main() {
    int dec = 42;
    int hex = 0x2A;
    int oct = 052;
    unsigned long long bigNum = 1844674407370955161ULL;
    float piFloat = 3.14159f;
    printf("Dec: %d | Hex: %d | Oct: %d\
", dec, hex, oct);
    printf("Big Num: %llu | Float PI: %.5f\
", bigNum, piFloat);
    return 0;
}

Key Takeaways, Best Practices & Memory Safety Rules

Best Practice & Key Takeaways
Be careful with leading zeros in numeric literals to avoid accidental octal conversions.

Frequently Asked Questions & Technical Interview Insights

Technical Interview & Viva Questions

Frequently asked C programming interview questions
Q

Q: What type does the literal `3.14` have by default in C?

A: `double`. To make it a 32-bit single-precision float, you must append the `f` suffix (`3.14f`).
Q

Q: Why does leading zero `012` evaluate to 10 in C?

A: A leading zero indicates an octal (base-8) literal: (1 * 8^1) + (2 * 8^0) = 10.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations