Programmer Coding

Literals in C Language

What is Literals?

Literals are constant values ​​assigned to constant variables. It can be said that the text represents a fixed value that cannot be changed. It also has a memory but no data as a change. For example const int = 10; It is an integer expression where 10 is an integer literal.

Different types of literals in C

  • Integer Literals:

Integer literals are whole numbers without any fractional or exponential part.

Examples: 0, 123, -45

  • Floating-Point Literals:

Floating-point literals are numbers with a fractional part.

Examples: 3.14, -0.005, 6.02e23

  • Character Literals:

Character literals represent single characters enclosed within single quotes ‘.

Examples: ‘a’, ‘7’, ‘%’

  • String Literals:

String literals represent sequences of characters enclosed within double quotes “.

Examples: “hello”, “programmercoding.com”, “123”

  • Boolean Literals:

C does not have a built-in boolean data type, but boolean literals are often represented using integer values, where 0 represents false and any non-zero value represents true.

Examples: 0 (false), 1 (true)

  • Hexadecimal Literals:

Hexadecimal literals are integers written in base-16 format, prefixed with 0x or 0X.

Examples: 0x1A, 0xFF, 0xABC

  • Octal Literals:

Octal literals are integers written in base-8 format, prefixed with 0.

Examples: 012, 076, 0777

  • Binary Literals (C99 and later):

Binary literals are integers written in base-2 format, prefixed with 0b or 0B.

Examples: 0b1010, 0b110011, 0B1111

  • Long and Unsigned Literals:

Long literals are suffixed with L or l to indicate a long integer type.

Unsigned literals are suffixed with U or u to indicate an unsigned integer type.

Examples: 123L, 456U, 789UL

 

Example

#include <stdio.h>

int main() {

// Integer Literal

int intLiteral = 123;

printf(“Integer Literal: %d\n”, intLiteral); // Output: Integer Literal: 123

// Floating-Point Literal

float floatLiteral = 3.14;

printf(“Floating-Point Literal: %.2f\n”, floatLiteral); // Output: Floating-Point Literal: 3.14

// Character Literal

char charLiteral = ‘A’;

printf(“Character Literal: %c\n”, charLiteral); // Output: Character Literal: A

// String Literal

char *stringLiteral = “Hello, programmercoding.com!”;

printf(“String Literal: %s\n”, stringLiteral); // Output: String Literal: Hello, C Programming!

// Boolean Literal

int booleanLiteral = 1; // Assuming 1 represents true

printf(“Boolean Literal: %d\n”, booleanLiteral); // Output: Boolean Literal: 1

// Hexadecimal Literal

int hexLiteral = 0xFF;

printf(“Hexadecimal Literal: %#X\n”, hexLiteral); // Output: Hexadecimal Literal: 0XFF

// Octal Literal

int octalLiteral = 076;

printf(“Octal Literal: %#o\n”, octalLiteral); // Output: Octal Literal: 076

// Binary Literal (C99 and later)

int binaryLiteral = 0b1010;

printf(“Binary Literal: %#X\n”, binaryLiteral); // Output: Binary Literal: 0XA

// Long Literal

long longLiteral = 123456789L;

printf(“Long Literal: %ld\n”, longLiteral); // Output: Long Literal: 123456789

return 0;

}

Output

 

Integer Literal: 123

Floating-Point Literal: 3.14

Character Literal: A

String Literal: Hello, programmercoding.com!

Boolean Literal: 1

Hexadecimal Literal: 0XFF

Octal Literal: 076

Binary Literal: 0XA

Long Literal: 123456789

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top