Literals in C

Spread the love

Constant values used within a program are known as Literals. These constant values occupy memory but do not have any reference like variables.

There are four types of literals in C

  1. Integer literal
  2. Float literal
  3. String literal
  4. Character literal

Integer literal

Integer literals are used to represent and store the integer values. Integer literal is followed by an optional size or sign qualifier.

  • l or L is a size qualifier. It specifies size of an integer type as long.
  • u or U is a sign qualifier. It specifies an integer type as unsigned (can store only Non-Negative value values).

In C programming we can specify an integer constant in three ways:

  1. Decimal number (base 10).
  2. Octal number (base 8).
  3. Hexadecimal number (base 16).

Decimal number

A non-zero decimal digit.

example:- 123, 124, 10

Octal number

Octal constants is prefixed with 0followed by digits between 0-7.

example:- 0456, 0765

Hexadecimal number

Hexadecimal constant is prefixed with 0x or 0X following hexadecimal characters (i.e. digits from 0-9 and characters from a-f or A-F)

example:- 0XA4C, 0xBEA

Floating Point Literals

Floating Point Literals are used to represent and store real numbers. Floating Point literals can be stored either in decimal form or exponential form.

While representing decimal form, you must include the decimal point, the exponent, or both; and while representing exponential form, you must include the integer part, the fractional part, or both.If it does not contain either of these, then the compiler will throw an error.

Decimal Form- +11.2, 0.40

Exponential Form- In exponential notation real constants are expressed in scientific format specifying mantissa and exponent.  syntax of writing real constant in scientific format.

[+/-] <Mantissa> <e/E> [+/-] <Exponent>

Valid examples of real constants in exponential notation- 0.1e1 0e-5 5e25 +5E+10 -5.2e0

Rules for representation of real constant in exponential notation

  1. Mantissa can be expressed either as decimal or as fractional number.
  2. We can use either uppercase or lowercase for exponent sign i.e. E or e.
  3. Exponent must be a decimal value.
  4. + or - sign symbol may be prefixed before exponent and mantissa.
  5. Spaces are not allowed.

String Literal

A string literal represents multiple characters enclosed within double-quotes “”. It contains an additional character, ‘\0’ (null character), which gets automatically inserted. This null character specifies the termination of the string. We can use the ‘+’ symbol to concatenate two strings.

We can break a long line into multiple lines using string literals and separating them using white spaces.

String1= “program”;

String2= “br”;

To concatenate the above two strings, we use ‘+’ operator

“program ” + “br”= program br

Character Literal

Character literals is a single character constant enclosed in single quotes. In C programming character constant occupies single byte space in memory.

A character literal can be a plain character (e.g., ‘a’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u00A9’).

Disclosure: This website is supports by its reader. This means, if you buy any product / service through link on this website, we may earn a small commision withought costing you extra. Learn how you can support Us and why we need this?