Dark Mode On/Off

# C Literals/Constants

• If we want to use some data value in a C program, we can also directly use a value, instead of creating a variable to store the value.

• When we use a value directly in C program without creating a variable, it is known as a Literal.

• A literal is used when we want to use a fixed value in the program, hence literals are also called Constants.

• For example, 1, 100, 'Y', 10.5, etc. Whereas, when we use a variable in C, we can change the value stored in the variable.

In the language, literals can be of 5 types, they are:

1. Integer Literal

2. Character Literal

3. Floating-point Literal

4. String Literals

5. Backslash Character Literals (Escape Sequences)

## 1. Integer Literal

Any numeric value without any decimal or exponential part, used in the C program is an integer literal.

Integer literals are of 3 types:

1. Decimal Numer (base 10)

2. Octal Number (base 8) - Uses digits 0 up to 7. Number 10 is the same as 8 for the octal system.

3. Hexadecimal Number (base 16) - Uses digits 0 up to 9, and alphabets A to F. (A, B, C, D, E, F stands for 10, 11, 12, 13, 14, and 15)

For example,

``````Decimal: 7, -10 etc
Octal: 023, 045 etc
• In the C language, the octal number starts with a 0,

• and the hexadecimal number starts with a 0x.

• You can also use type suffixes with integer values to make it easier for the compiler to understand the type of the integer value.

• If you follow the value with `L`, the compiler will treat it as a `long` value.

• If you add `U` after the value, the compiler treats the value as unsigned.

For example,

``````3500L, 14l    // long value

99U, 89u    // unsigned int value``````

## 2. Floating-point Literals

• Any valid floating-point value when used directly in a C program, is called a floating-point literal.

• The floating-point numbers have a decimal part (fraction) or an exponential part.

For example,

``````100.50
0.000127
-0.77E-5``````

E-5 stands for 10-5

If we add a type suffix `F` or `f` after the value, then the compiler will treat the value as `float` type by default.

For example,

``127.7F, 40f etc.``

## 3. Character Literals

• A valid character datatype value when used directly in the C program is called a character literal.

• The character values are enclosed within a single quote.

For example,

``'A', 'B', 'c', '#', etc.``

## 4. String Literals

• A sequence of characters is called a String.

• A string literal is a value with multiple characters enclosed within double quotes.

• There is no special datatype for a string in C programming, so you cannot create a variable of type string directly.

For example,

``"study", "tonight", "c programming", etc.``

## 5. Backslash Character Literals

• These are also known as Escape sequences.

• The combination of characters with the backslash character `\` have special meaning for the C compiler.

• A backslash character is used to escape characters with special meaning and make them normal characters for the compiler.

• For example, if you use a single quotation mark, the compiler will think that it is the starting or end of a character value, but what if you want a single quote character?

``'\''    \\ this is character with value '``

In the example above the first and the last single quote marks the start and end of the character value, whereas the backslash escape character informs the compiler to treat the single quotation after it as a normal character.

Code Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\" Double quote
\' Single quote
\\ Backslash
\v Vertical tab
So remember `\n` is for Newline and `\t` is for the horizontal tab.