Literals in C - GeeksforGeeks (2024)

Last Updated : 21 Jun, 2024

Summarize

Comments

Improve

In C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably.
For example, “const int = 5;“, is a constant expression and the value 5 is referred to as a constant integer literal.

Types of C Literals

There are 4 types of literal in C:

  • Integer Literal
  • Float Literal
  • Character Literal
  • String Literal

Literals in C - GeeksforGeeks (1)

1. Integer Literals

Integer literals are used to represent and store the integer values only. Integer literals are expressed in two types i.e.

A) Prefixes: The Prefix of the integer literal indicates the base in which it is to be read.
For Example:

0x10 = 16

Because 0x prefix represents a HexaDecimal base. So 10 in HexaDecimal is 16 in Decimal. Hence the value 16.

There are basically represented into 4 types:

a. Decimal-literal(base 10): A non-zero decimal digit followed by zero or more decimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

Example:

56, 78

b. Octal-literal(base 8): a 0 followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7).

Example:

045, 076, 06210

c. Hex-literal(base 16): 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F).

Example:

0x23A, 0Xb4C, 0xFEA

d. Binary-literal(base 2): 0b or 0B followed by one or more binary digits(0, 1).

Example:

0b101, 0B111

B) Suffixes: The Suffixes of the integer literal indicates the type in which it is to be read.

For example:

12345678901234LL

indicates a long long integer value 12345678901234 because of the suffix LL

These are represented in many ways according to their data types.

  • int: No suffix is required because integer constant is by default assigned as an int data type.
  • 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:

C
#include <stdio.h>int main(){ // constant integer literal const int intVal = 10; printf("Integer Literal:%d \n", intVal); return 0;}

Output

Integer Literal:10 

2. Floating-Point Literals

These are used to represent and store real numbers. The real number has an integer part, real part, fractional part, and exponential part. The floating-point literals can be stored either in decimal form or exponential form. While representing the floating-point decimals one must keep two things in mind to produce valid literal:

  • In the decimal form, one must include the integer part, or fractional part, or both, otherwise, it will lead to an error.
  • In the exponential form, one must include both the significand and exponent part, otherwise, it will lead to an error.

A few floating-point literal representations are shown below:

Valid Floating Literals:

10.125
1.215e-10L
10.5E-3

Invalid Floating Literals:

123E
1250f
0.e879

Example:

C
#include <stdio.h>int main(){ // constant float literal const float floatVal = 4.14; printf("Floating point literal: %.2f\n", floatVal); return 0;}

Output

Floating point literal: 4.14

3. Character Literals

This refers to the literal that is used to store a single character within a single quote. To store multiple characters, one needs to use a character array. Storing more than one character within a single quote will throw a warning and display just the last character of the literal. It gives rise to the following two representations:

  • char type: This is used to store normal character literal or narrow-character literals.

Example:

char chr = 'G';

Example:

C
#include <stdio.h>int main(){ // constant char literal const char charVal = 'A'; printf("Character Literal: %c\n", charVal); return 0;}

Output

Character Literal: A

Escape Sequences: There are various special characters that one can use to perform various operations.

4. String Literals

String literals are similar to that character literals, except that they can store multiple characters and uses a double quote to store the same. It can also accommodate the special characters and escape sequences mentioned in the table above. We can break a long line into multiple lines using string literal and can separate them with the help of white spaces.
Example:

char stringVal[] = "GeeksforGeeks";

Example:

C
#include <stdio.h>int main(){ const char str[] = "Welcome\nTo\nGeeks\tFor\tGeeks"; printf("%s", str); return 0;}

Output

WelcomeToGeeks For Geeks

Must Read:

  • User-Defined Literals
  • Raw String Literal in C++
  • Octal Literals in C
  • Compound Literals in C
  • Type Difference of Character Literals C++


C

Chinmoy Lenka

Literals in C - GeeksforGeeks (2)

Improve

Previous Article

Data Types in C

Next Article

Escape Sequence in C

Please Login to comment...

Literals in C - GeeksforGeeks (2024)
Top Articles
Totems
Why Am I Running and Not Losing Belly Fat? Experts Share 9 Common Mistakes
Bank Of America Financial Center Irvington Photos
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Mr Tire Prince Frederick Md 20678
Craigslistdaytona
Myunlb
Infinite Campus Parent Portal Hall County
Revitalising marine ecosystems: D-Shape’s innovative 3D-printed reef restoration solution - StartmeupHK
Seafood Bucket Cajun Style Seafood Restaurant in South Salt Lake - Restaurant menu and reviews
Charmeck Arrest Inquiry
Aldi Sign In Careers
Mals Crazy Crab
Iu Spring Break 2024
Invert Clipping Mask Illustrator
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
Schedule An Oil Change At Walmart
Qual o significado log out?
Unionjobsclearinghouse
Valic Eremit
Danielle Ranslow Obituary
Violent Night Showtimes Near Johnstown Movieplex
Dal Tadka Recipe - Punjabi Dhaba Style
New Stores Coming To Canton Ohio 2022
O'reilly's In Mathis Texas
The Fabelmans Showtimes Near Baton Rouge
Frank Vascellaro
Nurtsug
25Cc To Tbsp
King Soopers Cashiers Check
Busted! 29 New Arrests in Portsmouth, Ohio – 03/27/22 Scioto County Mugshots
Frommer's Belgium, Holland and Luxembourg (Frommer's Complete Guides) - PDF Free Download
Gr86 Forums
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Ducky Mcshweeney's Reviews
Frcp 47
Wo ein Pfand ist, ist auch Einweg
Entry of the Globbots - 20th Century Electro​-​Synthesis, Avant Garde & Experimental Music 02;31,​07 - Volume II, by Various
Www Usps Com Passport Scheduler
Lamp Repair Kansas City Mo
Rocky Bfb Asset
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Pixel Gun 3D Unblocked Games
Tlc Africa Deaths 2021
Funkin' on the Heights
UWPD investigating sharing of 'sensitive' photos, video of Wisconsin volleyball team
Dagelijkse hooikoortsradar: deze pollen zitten nu in de lucht
Gt500 Forums
2294141287
What Time Do Papa John's Pizza Close
Mike De Beer Twitter
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6758

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.