Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Literals?
3.
Types of Literals in C
4.
Integer Literal
4.1.
Prefix
4.2.
Suffix
4.3.
Example in C
4.4.
C
4.5.
Example in C++
4.6.
C++
5.
Float Literal
5.1.
Syntax
5.2.
Example in C
5.3.
C
5.4.
Example in C++
5.5.
C++
6.
Character Literal
6.1.
Syntax
6.2.
Example in C
6.3.
C
6.4.
Example in C++
6.5.
C++
7.
String Literal
7.1.
Syntax
7.2.
Example in C
7.3.
C
7.4.
Example in C++
7.5.
C++
8.
Boolean Literal
8.1.
Syntax
8.2.
Example in C++
8.3.
C++
9.
Difference of character literals in C and C++
9.1.
Example in C and C++
10.
Decimal Notation
10.1.
Example in C
10.2.
C
10.3.
Example in C++
10.4.
C++
11.
Exponential Notation
11.1.
Example in C and C++
11.2.
C
12.
Frequently Asked Questions
12.1.
How many types of Literals are there in C?
12.2.
What is literals in C with example?
12.3.
What are the literals in C#?
12.4.
What is a literal in programming?
12.5.
Why is literal used?
13.
Conclusion
Last Updated: Apr 4, 2024
Easy

Literals in C/C++ With Examples

Author Manan Singhal
0 upvote

Introduction

The value that's assigned to the constant variable is called literals. The constant value may be named differently as literal. For instance, "const int temp = 10" assigns constant integer value 10 to temp, which is an integer literal. In this blog, we will learn about the Literals in C and C++ and different types of Literals, along with their examples.

literals in c

What are Literals?

When we work in a specific programming language, we need to work with variables to store the data. Literals are fixed values that are directly used in the code and represent specific types of data. They are used to assign values to variables or as operands in expressions. There are several types of literals, let us understand what types of literals are.

Types of Literals in C

There are five types of Literals in C++ and four types of Literals in C:

  • Integer Literal: used to represent an integer constant.
     
  • Float Literal: used to represent float constant.
     
  • Character Literal: used to represent a single character.
     
  • String Literal: used to represent the character sequence(string).
     
  • Boolean Literal: used to represent boolean(true or false). This literal does not exist in C.

Integer Literal

Integer Literals are used to represent the integer constant values. They are represented mainly in two ways: Prefix and Suffix.

Prefix

The Prefix of the integer literal indicates the type of base in which it is to be read.

1. Decimal-literal in base 10: This represents value in decimal (i.e. 0,1,2,3,4,5,6,7,8,9) digits. The value starts with a non-zero decimal digit.
Example

84, 43


2. Binary-literal in base 2: This represents value in binary (i.e. 0,1) digits. The value starts with a prefix of 0b or 0B.
Example

0b101, 0B111


3. Octal literal in base 8: This represents value in octal (i.e. 0,1,2,3,4,5,6,7) digits. The value starts with a prefix of 0.
Example

084, 043

 

4. Hex-literal in base 16: This represents value in hex (i.e. A,a,B,b,C,c,D,d,E,e,F,f,0,1,2,3,4,5,6,7,8,9) digits. The value starts with a prefix of 0x or 0X.
Example

0x23A, 0Xb4C

Suffix

The Suffix of the integer literal indicates the type in which it is to be stored.

  • int: No suffix is required because integer constant is assigned as an int data type by default.
     
  • unsigned int: character U or u at the end of an integer constant.
     
  • long int: character L or l at the end of an integer constant.
     
  • unsigned long int: character UL or ul at the end of an integer constant.
     
  • long long int: character LL or ll at the end of an integer constant.
     
  • unsigned long long int: character ULL or ull at the end of an integer constant.

Example in C

  • C

C

#include <stdio.h>

int main()
{
const int integer_literal = 100;

printf("Integer Literal:%d \n", integer_literal);
return 0;
}


Output

Integer Literal: 100


Example in C++

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
const int integer_literal = 100;
cout << "Integer Literal: " << integer_literal << "\n";
return 0;
}


Output

Integer Literal: 100

Float Literal

Float literals are used to represent and store real numbers. There are mainly two methods to symbolise the float literals. It may store it in decimal format or exponential format.

  • In decimal format, they are represented using decimal points and exponent parts; otherwise, it will throw an error.
     
  • In exponential format, they are represented using integer and fraction parts; otherwise, it will throw an error.

Syntax

const float decimal_float_literal = 71.245
const float exponential_float_literal = 71245E-3;


Example in C

  • C

C

#include <stdio.h>

int main()
{
const float decimal_float_literal = 71.245;

printf("Float Literal: %.2f\n", decimal_float_literal);
return 0;
}


Output

Float Literal: 71.245


Example in C++

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
const float decimal_float_literal = 71.245;

cout << "Float Literal: " << decimal_float_literal << "\n";
return 0;
}


Output

Float Literal: 71.245

Character Literal

They are used to store a single character inside a single quote. To store more than one character, we can use a character array; storing within a single quote will throw a warning and show the last character of the literal.

  • char type: It is used to store narrow-character literal. It is supported by both C and C++.
     
  • wchar_t type: It is used to store wide-character literal. To assign the value, L is used before a single character. It is supported by both C and C++.

Syntax

/* C program to write Character literals */
const char character_literal = 'C';

/* C++ program to write Character literals */
const char character_literal = 'C';
const wchar_t wide_character_literal = L'C';


Example in C

  • C

C

#include <stdio.h>

int main()
{
const char character_literal = 'C';

printf("Character Literal: %c\n", character_literal);
return 0;
}


Output

Character Literal: C


Example in C++

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
const char character_literal = 'C';
const wchar_t wide_character_literal = L'C';

cout << "Character Literal: " << character_literal << "\n";
cout << "Wide Character Literal: " << wide_character_literal << "\n";

return 0;
}


Output

Character Literal: C
Wide Character Literal: C

String Literal

String literals are like that of the character literals, except they can store more than one character and use a double quote to represent a string.

Syntax

/* C program to write String literals */
const char string_literal[] = "CodingNinjas";

/* C++ program to write String literals */
const string string_literal = "CodingNinjas";


Example in C

  • C

C

#include <stdio.h>

int main()
{
const char string_literal[] = "Welcome\nTo\nCoding Ninjas";
printf("%s", string_literal);
return 0;
}


Output

Welcome
To
Coding Ninjas


Example in C++

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
const string string_literal = "Welcome\nTo\nCoding Ninjas";
cout << string_literal;
return 0;
}


Output

Welcome
To
Coding Ninjas

Boolean Literal

Boolean literal represents Boolean constant value. This literal type can typically take two Boolean values(true/false). Boolean literal is provided only in C++ and not in C.

Syntax

/* C++ program to write Boolean literals */
const bool boolean_literal1 = true;
const bool boolean_literal2 = false;


Example in C++

  • C++

C++

/* C++ program to show Boolean literals */

#include <iostream>
using namespace std;

int main()
{
const bool boolean_literal1 = true;
const bool boolean_literal2 = false;
cout << "Boolean Literal 1 is: " << boolean_literal1 << "\n";
cout << "Boolean Literal 2 is: " << boolean_literal2 << "\n";

return 0;
}


Output

Boolean Literal 1 is: 1
Boolean Literal 2 is: 0

Difference of character literals in C and C++

Character literals (e.g. 'C') will have different types. sizeof('C') returns distinct values in C and C++. There are some basic differences between character literals in C and C++. Let's have a look to get it better. 
In C, a character literal is handled as int type, whereas in C++, a character literal is handled as the char type. 
For example: sizeof('C') and sizeof(char) are equal in C++, however not in C.


Example in C and C++

int main()
{
   printf("sizeof('C') = %d and sizeof(char) = %d", sizeof('C'), sizeof(char));
   return 0;
}

 

Output

C = sizeof('C') = 4 and sizeof(char) = 1
C++ = sizeof('C') = 1 and sizeof(char) = 1

Decimal Notation

The ANSI C language specification is expanded upon by the decimal data type. You must include the decimal.h header file in your C source code when using the decimal data types. You can format simply the decimal places using the C++ setprecision function rather than the entire floating-point or double value. Before calling the setprecision() method, use the fixed keyword to accomplish this.

Example in C

  • C

C

#include <stdio.h>
int main()
{
float f = 5.4;
printf("\nValue of f = %f \n", f);
printf("Size of float = %ld \n", sizeof(float));

return 0;
}

Output

Value of f =  5.400000 
Size of float =  4 

Example in C++

  • C++

C++

#include <bits/stdc++.h>
using namespace std;
int main() {

float f = 5.43678;
cout << "Original number is: " << f<<endl;
cout << "The number with 2 decimal places is: ";
cout << fixed << setprecision(2) << f<<endl;
return 0;
}

Output

Original number is: 5.43678
The number with 2 decimal places is: 5.44

Exponential Notation

The exponential value of the constant e (Euler's number) is calculated in C programming. E has a value of around 2.71828. The maths.h header file defines the exp() function. The "times 10 to the power of" portion of the equation is denoted in C++ by the letter "e" (or occasionally "E"). For instance, the formula for 5.3 x 104 would be 5.3e4.

Example in C and C++

  • C

C

#include <stdio.h>  
#include <math.h>

int main()
{
printf("The value for e raised to power 2 is = %.6f \n", exp(2));
printf("The value for e raised to power 4 is = %.6f \n", exp(4));
return 0;
}

Output

The value for e raised to power 2 is = 7.389056 
The value for e raised to power 4 is = 54.598150 

Frequently Asked Questions

How many types of Literals are there in C?

C allows four types of Literals: integer, float, character, and string literal.

What is literals in C with example?

In C, literals are fixed values directly written into the code. For example, 42 is an integer literal, "hello" is a string literal, and 3.14 is a floating-point literal.

What are the literals in C#?

In C#, literals are similar to C and represent fixed values directly written into the code. Examples include 42 (integer), "hello" (string), and 3.14 (floating-point).

What is a literal in programming?

In programming, a literal is a notation representing a fixed value within the source code of a program. It can represent various data types such as integers, strings, characters, or floating-point numbers.

Why is literal used?

Literals are used to represent fixed values directly within code, making it easier to understand and maintain. They provide clarity and precision, reducing the need for additional variables or calculations, and enhancing the readability of the code.

Conclusion

In this article, we have discussed the literals in C and C++ and their types. Understanding literals in C is fundamental for any programmer venturing into the world of C programming. Literals represent fixed values directly within the code, such as integers, strings, characters, or floating-point numbers, enhancing the readability and clarity of the code.

If you would like to learn more, check out our new articles on Library. Do upvote our blog to help other ninjas grow.

Recommended Readings:


Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass