Updated March 27, 2023
Introduction to C Literals
C Literals are defined as constants which are used to represent something static but can never be declared as a variable, these constant values occupy memory but do not have any specific reference like variables. C literals are basically used to optimize the code and to run out of the situation where no option is left to declare a variable.
Types of Literals in C
Literals are mainly of four types:
- Integer literals
- Character literals
- String literals
- Float or Real Literals
1. Integer Literals
Further Integer literals can be represented in three ways:
- Decimal Number Literal
- Octal Number Literal
- Hexadecimal Number Literal
Moreover, Integer literal is a type of literal which is followed by an integer which can be say long and is represented as either l or L i.e. [l, L]. Similar is the case with Unsigned integer which is represented as either [u, U] and stores positive integers only.
Code:
#include <stdio.h>
int main ()
{
const int z = 15;
printf ("Integer type Literal:%d \n", z);
return 0;
}
Output:
a. Decimal Number Literal
Decimal Constants can be represented using digits lying within the range of 0 and 9.
Example of decimal constant
- 456
- 789
b. Octal Number Literal
Octal constants, on the other hand, are the type of constants that are represented using digits ranging between 0 and 7 and prefixed with digit 0.
Example:
0678 is re
c. Hexadecimal Number Literal
Hexadecimal number Literals contain hexadecimal characters which are prefixed with 0x or 0X, in short, it should contain value ranging from 0 and 9 and characters ranging from a to for A to F.
For Example:
0022 in hexadecimal is considered equivalent to 34 in decimal.
Examples for Defining Integer Constants
Decimal Representation | Octal Representation | Hexadecimal Representation | Description |
2016U | 03740u | 0x7E0u | Unsigned integer |
2147483697l | 02000000061l | 0x80000031l | Long Integer |
2147483697ul | 020000000061ul | 0x80000031ul | Unsigned Long Integer |
2. Character Literals
Character type literal is a kind of literal that considers a single character within single quotes. In C programming a character literal or constant which occupies one-byte memory.
Ways to represent a character literal are as follows:
- Using the Unicode value of a character. Ex: \u09A9
- Escape sequence characters can also be used to represent any character literal.
- Using an ASCII integer to represent a character says a character literal. Ex: ‘B’ to use to represent ‘066’ as a character literal.
- Characters within a single quote. For example: ‘a’, ‘1’, ‘.’, ‘!’
- Using the octal or hexadecimal representation of an integer as an escape sequence character.
Code:
#include <stdio.h>
int main ()
{
const char f = 'B';
printf ("Character type Literal: %c\n", f);
return 0;
}
Output:
Further, character literal can also be classified as multi-character constants.
a. Multi-char Literal
Character literals that contain more than one character within a single quote or set of characters within single quotes are known as multi-char literal. If someone wants to get a set of characters within a single quote can go for multi-char literal but we should not make use of multi-char literal while programming or to write a neat program.
Examples of Multi-char Literal:
- ddd
- 6579300.
3. String Literals
String literals are the type of literals which considers a set of characters within double-quotes. String literal occupies some bytes in a way that first it stores total characters with one extra byte space in memory. The additional one byte is added to keep the last null character. The addition of null is done in order to parse the entire string and put it in a way that specifies the end of the string. The concatenation of string value or literal using + operator is also possible.
Code:
#include <stdio.h>
int main ()
{
const char strarr []
= "author\n\tof\t educba";
printf ("%s", strarr);
return 0;
}
Output:
Example of some valid string Literals:
- I will try to write a good program.
- writing a good program is very hard…
- I know to program. \n” + “I must perform that.
- It is a bit confusing therefore we need to keep it in mind that both ‘C’ and “C” look similar, but they are actually very different from each other. ‘C’ literally consumes 1-byte memory.
4. Float or Real Literal
C programming is a programming language where the float or real literal is used in a way where it is specified either by using it as a decimal or exponential notation.
Code:
#include <stdio.h>
int main ()
{
const float g = 4.14;
printf ("Floating type of literal: %d\n", g);
return 0;
}
Output:
Examples of Float or real Literal:
- 167859
- 4167859E-6L
Decimal Notation
An optional decimal point or fractional part is prefixed with a real constant which is considered as a whole number. Further, that will be preceded with either + or – representing a positive or negative number respectively.
Examples of float constants are as follows:
- +1
- 3.2
- -0.5
- 0.
- .3
- -.5
Exponential Notation
- Any number with small or big magnitude is helpful if it is represented using exponential notations of literals. Numbers with more digits or notations are expressed in this way. Numbers like 7950000000000 can be written in the format of 7.95e12, 0.0000000000795 and it is represented as 7.95e-011.
- Any exponential notation real constants are expressed in scientific format and it accepts in that way only like mantissa and exponent.
There is a specific scientific notation to do that namely:
[-/+] /mantissa/ /e/E/ [+/-] /Exponent/Examples of exponential notation are as follows:
- 0.2e2
- 0f-7
- 6e45
- -8.90
Rules for real Constant representation in exponential notation:
- Exponent must be a decimal value only.
- Either an uppercase or lower case for must be assigned in as “E” or “e” again it depends on the requirement of how we need to use it.
- Mantissa can either be expressed as uppercase or lowercase for exponent E or e.
- Spaces are also not allowed.
Conclusion
Literals behavior is like constants only and they are very much needed when we need to fix and make things act like a constant. But then it depends on the rules and the requirement of how and when we need to use which kind of literal. Literals are a very sorted form of constants which rather than increasing the memory and space should be versatile and optimized.
Recommended Articles
This is a guide to C Literals. Here we discuss the basic concept, main four types of C Literals with respective examples. You can also go through our other related articles to learn more –