- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
C - Literals
The term "literal" in computer programming terminology refers to a textual representation of a value to be assigned to a variable.
In C, you can assign a value to a variable in two ways −
- Using literal representation
- Using an expression
The initialization of a variable in C is done as follows −
int x = 10;
On the other hand, an indirect initialization of a variable by assigning it the result of an expression is as follows −
int x = 10; int y = x*2;
In the first case, 10 is an integer literal assigned to "x". In the second case, the result of "x*2" expression is assigned to "y".
A literal is thus a value of a certain data type represented directly into the source code. Normally, literals are used to set a value of a variable.
On their own, literals don’t form any of the programming element. Different notations are used to represent the values of different data types.
Integer Literals in C
In the above example, 10 is an integer literal. A positive or negative whole number represented with digits 0 to 9, without a fractional part is a decimal integer literal. It must be within the acceptable range for the given OS platform.
Following examples assign decimal literals to int variables −
int x = 200; int y = -50;
An integer literal can also have a suffix that is a combination of "U" and "L", for "unsigned" and "long", respectively. The suffix can be uppercase or lowercase and can be in any order.
int c = 89U; long int d = 99998L;
C allows you to represent an integer in octal and hexadecimal number systems. For a literal representation of an octal, prefix the number with 0 (ensure that the number uses octal digits only, from 0 to 7).
For a hexadecimal literal, prefix the number with 0x or 0X. The hexadecimal number must have 0 to 9, and A to F (or a to f) symbols.
Example
Take a look at the following example −
#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); }
Output
On running this code, you will get the following output −
Octal to decimal: 21 Hexadecimal to decimal: 161
Modern C compilers also let you represent an integer as a binary number, for which you need to add a 0b prefix.
Example
Take a look at the following example −
#include <stdio.h> int main(){ int x = 0b00010000; printf("binary to decimal: %d", x); }
Output
Run the code and check its output −
binary to decimal: 16
Here are some examples of integer literals −
212 /* valid */ 215u /* valid */ 0xFeeL /* valid */ 078 /* invalid: 8 is not an octal digit */ 032UU /* invalid: cannot repeat a suffix */
Here are some other examples of various types of integer literals −
85 /* decimal */ 0213 /* octal */ 0x4b /* hexadecimal */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */
Floating-point Literals in C
A floating-point literal in C is a real number with an integer part and a fractional part within the range acceptable to the compiler in use, and represented in digits, decimal point with an optional exponent symbol (e or E).
A floating point literal is generally used for initializing or setting the value of a float or a double variable in C.
Example
The following assignment examples use floating point literals with a decimal point separating the integer and the fractional part −
#include <stdio.h> int main(){ float x = 10.55; float y = -1.333; printf("x and y are: %f, %f", x, y); }
Output
You will get the following output −
x and y are: 10.550000, -1.333000
Floating point literals with a high degree of precision can be stated with the exponentiation symbol "e" or "E". This is called the scientific notation of a float literal.
While representing decimal form, you must include the decimal point, the exponent, or both. While representing exponential form, you must include the integer part, the fractional part, or both.
Example
Take a look at the following example −
#include <stdio.h> int main(){ float x = 100E+4; float y = -1.3E-03; printf("x: %f\n", x); printf("y: %f\n", y); }
Output
When you run this code, it will produce the following output −
x: 1000000.000000 y: -0.001300
Here are some examples of floating-point literals −
3.14159 /* valid */ 314159E-5L /* valid */ 510E /* invalid: incomplete exponent */ 210f /* invalid: no decimal or exponent */ .e55 /* invalid: missing integer or fraction */
Character Literals in C
A character literal in C is a single character enclosed within single quote symbols. Note that C recognizes straight quotes only. Hence, use ' to form a character literal and not ‘). Here is an example −
char x = 'I';
Character literals are generally assigned to a char variable that occupies a single byte. Using the %c format specifier outputs the character. Use %d and you’ll obtain the ASCII value of the character.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char x = 'I'; printf("x: %c\n", x); printf("x: %d\n", x); }
Output
Run the code and check its output −
x: I x: 73
Escape Sequences in C
C defines a number of escape sequences as a sequence of characters starting with "\" and an alternate meaning attached to the following characters.
Even though an escape sequence consists of more than one characters, it is put inside single quotes. An escape sequence produces the effect of a single non-printable character. For example, '\n' is an escape sequence that represents a newline character, with the same effect as pressing the Enter key.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char x = 'I'; char y = 'J'; printf("x: %c\ny: %c", x,y); }
Output
Here you will get this output −
x: I y: J
We shall learn more about escape sequences in a later chapter.
A character literal can also be a UNICODE representation of a character. Such a literal has /u at the beginning.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char x = '\u09A9'; printf("x: %c\n", x); printf("x: %d\n", x); }
Output
On running this code, you will get the following output −
x: ⌐ y: -87
String Literals in C
A sequence of characters put inside double quotation symbols forms a string literal. C doesn’t provide a string variable. Instead, we need to use an array of char type to store a string.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char arr[] = "Hello World"; printf("arr: %s", arr); }
Output
Run the code and check its output −
arr: Hello World
A string literal may contain plain characters, escape sequences, and Unicode characters. For example −
char arr[] = "Hello \ World";
You can also have a literal representation of an array by putting its elements inside the curly brackets { and }. For example:
int arr[] = {10, 20, 30, 40};
Similarly, the curly brackets can also be used for a literal representation of a struct value. For example −
struct marks { int phy; int che; int math }; struct marks m1 = {50, 60, 70};
We shall learn about arrays and structures in detail later in this tutorial.