Literals in C

Literals in C. Literals in C Tutorial. Learn Literals in C
Literals in C

What is Literals in C?

In C programming, literals are values that are used to represent a fixed value in the code. Literals can be of various types such as integer, floating-point, character, string, etc.

Literals are used in programming to assign a fixed value to a variable or expression. For example, if you want to store the value of Pi in a variable, you can assign it using a literal value of 3.14159.

Types of Literals in C:

  1. Integer Literals
    • 1. Decimal Integer Literal
    • 2. Octal Integer Literal
    • 3, Hexadecimal Integer Literal
  2. Floating Point Literals
    • 1. Decimal Floating Point Literals
    • 2. Exponential Floating Point Literals
  3. Character Literals
  4. String Literals

1. Integer Literals:

Integer Literals: Integer literals are used to represent integer values. Integer literals can be in decimal, octal, or hexadecimal format.

1. Decimal Integer Literals:

These are integer values represented in the base-10 decimal system. The value is represented by a sequence of digits from 0 to 9.

For example:

int decimal_value = 10;

2. Octal Integer Literals:

These are integer values represented in the base-8 octal system. The value is represented by a sequence of digits from 0 to 7, preceded by a ‘0’ (zero).

For example:

int octal_value = 012;

3. Hexadecimal Integer Literals:

hese are integer values represented in the base-16 hexadecimal system. The value is represented by a sequence of digits from 0 to 9 and letters from A to F, preceded by a ‘0x’ or ‘0X’.

For example:

int hex_value = 0xA;

2. Floating Point Literals:

Floating-point literals are used to represent floating-point values. Floating-point literals can be in decimal or exponential format.

1. Decimal Floating Point Literals:

These are floating-point values represented in the decimal system. The value is represented by a sequence of digits, optionally followed by a decimal point and another sequence of digits.

For example:

float float_value = 3.14;

2. Exponential Floating Point Literals:

These are floating-point values represented in the exponential format. The value is represented by a sequence of digits, optionally followed by a decimal point, another sequence of digits, and an exponent indicated by the letter ‘e’ or ‘E’.

For example:

float exp_value = 1.2e-3;

3. Character Literals:

Character literals are used to represent character values. Character literals are enclosed in single quotes (‘ ‘) and can be any printable ASCII character.

For example:

char char_value = 'A';

4. String Literals:

String literals are used to represent a sequence of characters. String literals are enclosed in double quotes (” “) and can contain any printable ASCII characters, including whitespace.

For example:

char string_value[] = "Hello, World!";

Example with all the literals:

#include <stdio.h>

int main() {
  int decimal = 10;          // decimal integer literal
  int octal = 012;           // octal integer literal
  int hex = 0xA;             // hexadecimal integer literal
  float decimal_f = 3.14;    // decimal floating-point literal
  float exp_f = 1.2e-3;      // exponential floating-point literal
  char c = 'A';              // character literal
  char str[] = "Hello, World!"; // string literal
  
  printf("Decimal Integer: %d\n", decimal);
  printf("Octal Integer: %d\n", octal);
  printf("Hexadecimal Integer: %d\n", hex);
  printf("Decimal Floating-Point: %f\n", decimal_f);
  printf("Exponential Floating-Point: %e\n", exp_f);
  printf("Character: %c\n", c);
  printf("String: %s\n", str);
  
  return 0;
}

Output:

Decimal Integer: 10
Octal Integer: 10
Hexadecimal Integer: 10
Decimal Floating-Point: 3.140000
Exponential Floating-Point: 1.200000e-03
Character: A
String: Hello, World!

Rules to follow while using Literals in C:

  • Integer literals cannot have a decimal point.
  • Integer literals in octal format must be preceded by a ‘0’ (zero).
  • Integer literals in hexadecimal format must be preceded by a ‘0x’ or ‘0X’.
  • Floating-point literals must have a decimal point or an exponent.
  • Character literals must be enclosed in single quotes.
  • String literals must be enclosed in double quotes.
  • String literals are automatically null-terminated with a ‘\0’ character at the end.
  • The size of a string literal is the number of characters in the literal plus one for the null terminator.

More: