Literals in C programming

Fixed values used inside a program are referred to as Literals in C Programming. These fixed values occupy reminiscence however do not need any reference like variables.

Or as Wikipedia speaks literals are a notation for representing a hard and fast worth inside a supply code.

There are 4 forms of literals in C programming.

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

NoticeLiterals are fixed values not fixed variables.

Learn More

Integer literal

In C programming we are able to specify an integer fixed in 3 ways.

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

An integer literals in C are adopted by a non-compulsory dimension or signal qualifier.

l or L is a dimension qualifier. It specifies the dimension of an integer sort as lengthy.

u or U is an indication qualifier. It specifies an integer sort as unsigned (can retailer solely optimistic values).

Decimal fixed is outlined utilizing digits between 0-9. For instance: 123.

Octal constants is prefixed with 0 adopted by digits between 0-7. For instance: 0123 (in octal) equal to 83 in decimal. All the time be cautious whereas defining octal constants, any quantity begins with Zero should solely comprise digits between 0-7 in any other case compiler will report an error on compilation.

As well as, don’t confuse octal constants as a decimal fixed. For instance: you would possibly assume 012 as a decimal fixed 12, however it’s an octal fixed equal to 10 (in decimal).

Hexadecimal fixed is prefixed with 0x or 0X following hexadecimal characters (i.e. digits from 0-9 and characters from a-f or A-F). For instance0X1A or 0x1a or 0x1A (in hexadecimal) equal to 26 (in decimal).

Legitimate examples of defining integer fixed

In DecimalIn OctalIn HexadecimalDescription
2016u03740u0x7E0uUnsigned integer
2016U03740U0x7E0U
2147483697l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0‭20000000061‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬Lengthy integer
‭2147483697L‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0‭20000000061‬L‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031‬L‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
2147483697ul0‭20000000061‬ul‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031u‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬Unsigned lengthy integer
2147483697uL0‭20000000061‬uL‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031u‬L‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
2147483697Ul0‭20000000061‬Ul‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031Ul‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
2147483697UL0‭20000000061‬UL‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031UL‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬

Notice: Order of the qualifiers doesn’t matter means ul or lu each is identical.
Don’t get puzzled with qualifiers. I’ll cowl them in-depth in knowledge varieties.

Learn More

Float or actual literal

In C programming a float or actual fixed is specified both utilizing decimal or utilizing exponential notation.

Decimal notation

In decimal notation actual fixed is represented as complete quantity, adopted by a non-compulsory decimal level and fractional half. It could be preceded with + or – image representing unfavorable or optimistic quantity.

Legitimate examples of float constants in decimal notation.
+1 1.2 -0.4 0. .3 -.3

Exponential notation

Exponential notations are useful in representing any quantity with a very huge or small magnitude. Numbers similar to7850000000000 in exponential notation expressed as 7.85e120.0000000000785  expressed as 7.85e-011.

In exponential notation, actual constants are expressed in scientific format specifying mantissa and exponent. Legitimate syntax of writing actually fixed in scientific format.

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

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

Guidelines for illustration of actual fixed in exponential notation

  1. Mantissa will be expressed both as decimal or as fractional quantity.
  2. We are able to use both uppercase or lowercase for exponent signal i.e. E or e.
  3. Exponent should be a decimal worth.
  4. + or - signal image could also be prefixed earlier than exponent and mantissa.
  5. Areas usually are not allowed.

Character literal

Character literals are a single character fixed enclosed inside single quotes in C programming. In C programming character fixed occupies single-byte house in reminiscence.

There are a lot of methods to characterize a personality fixed in C programming.

  1. Utilizing character inside a single quote. For instance‘a’ ‘0’ ‘/’ ‘.’ and many others.
  2. Utilizing escape sequence characters. For instance‘\n’ ‘\a’ ‘\b’ and many others.
  3. Utilizing an integer ASCII representing of a personality. For instance65 in ASCII format is used to characterize ‘A’.
  4. Utilizing octal or hexadecimal illustration of integer as escape sequence character. For instance\05 \xA
  5. Utilizing Unicode worth of a personality. For instance\u00A9

Multi-character fixed

Character fixed containing a couple of characters inside a single quote is named multi-character fixed. The compiler received complain in the event you give multi-character fixed in a single quote which is mostly not practiced.

For instance‘abcd’ is a sound character fixed in C. Nonetheless, it is going to solely characterize the final character of the fixed i.e. ‘d’ in our case.

Multi-character fixed shouldn’t be thought of as a very good programming practiced and ought to be ignored.

String literals

It is a sequence of characters enclosed inside double-quotes. This occupies (whole depend of characters + 1) byte house in reminiscence. The extra 1 byte is used for \0 (NULL) character.

A NULL worth is added explicitly on the finish of the string to specify the termination of the string. You can even concatenate string literals utilizing + operator.

Examples of some legitimate string constants
"I really like programming!"
I really like Teche Programmer.\n" + "I really like India.

Essential word: ‘A’ and “A” each seems to be related however in actual each are totally different‘A’ is a personality fixed occupying 1-byte house in reminiscence. Whereas “A” is a string fixed and occupies a 2-byte house in reminiscence.

Learn More at Teche Programmer – – Click Here

Leave a Reply

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