What are literals in C language ?

In C programming, literals are fixed values that are directly written into the code and represent themselves. They provide a way to incorporate specific data into a program without the need for variables. Here are the main types of literals:

1. Integer Literals:

  • Represent whole numbers, positive, negative, or zero.
  • Can be written in:
    • Decimal form (base 10): e.g., 10, -5, 0
    • Octal form (base 8, prefixed with 0): e.g., 021 (represents 17 in decimal)
    • Hexadecimal form (base 16, prefixed with 0x or 0X): e.g., 0x1F (represents 31 in decimal)

2. Floating-Point Literals:

  • Represent real numbers with decimal points.
  • Consist of a whole number part, a decimal point, and a fractional part.
  • Can use scientific notation (e or E for exponent): e.g., 3.14159, 1.25e-3 (represents 0.00125)

3. Character Literals:

  • Represent single characters enclosed in single quotes.
  • Can include:
    • Alphabets: e.g., 'A', 'a'
    • Digits: e.g., '5'
    • Special characters: e.g., '+', '%', ' ' (space)
    • Escape sequences: e.g., '\n' (newline), '\t' (tab), '\\' (backslash)

4. String Literals:

  • Sequences of characters enclosed in double quotes.
  • Represent text or messages.
  • Example: "Hello, world!"

5. Boolean Literals:

  • Represent logical values: true (1) and false (0).
  • Not a fundamental data type in C, but often implemented using integers.

Key Points:

  • Literals are not variables; their values cannot be changed during program execution.
  • They are stored in specific memory locations based on their data types.
  • Literals contribute to code readability and clarity by representing values directly.
  • Understanding different literal types ensures accurate data representation in C programs.

Related posts

List some online websites for c language code writing practice ?

Write common C language questions ?

What are header files and why they are used ?