EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C Literals

Home » Software Development » Software Development Tutorials » C Programming Tutorial » C Literals

c literals

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

Literals are mainly of four types:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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:

C Literals - 1

a. Decimal Number Literal

Decimal Constants can be represented using digits lying within the range of 0 and 9.

Example of decimal constant

  1. 456
  2. 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

Note: It is needed to be conscious while defining an octal number because any number starting with 0 must be included between the range of 0 and 7 or compiler will throw compilation error. Also, an octal number should never be confused with a decimal number.
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.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

For Example:

0022 in hexadecimal is considered equivalent to 34 in decimal.

Note: Order of qualifiers doesn’t matter in case of integer literals they can be represented as either ul or lu it not matters.

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
NOTE: Order of qualifiers doesn’t matter it can be either ul or lu.

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:

C Literals - 2

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:

  1. ddd
  2. 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:

String

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:

Float or Real

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 –

  1. Arithmetic Operators in C
  2. Static Constructor in C#
  3. C++ Literals
  4. C# Literals

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you