- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
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
- 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 |
- 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 |
- 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') |
- 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
- 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 |
- 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
- Integer Literals:
- No decimal point is allowed.
- Must fit within the range of the data type (e.g., int, long).
- Floating-Point Literals:
- Must include a decimal point or use scientific notation.
- Character Literals:
- Enclosed in single quotes and represent one character only.
- 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
- Literals represent fixed values in code and are immutable.
- Different types of literals exist to match the data types used in C programs.
- Escape sequences are used to represent non-printable characters.
- Use const for literals you don't want to change.
Literals are fundamental in C, forming the building blocks for initializing and manipulating data.
