• C Programming Video Tutorials

C - Literals



In computer programming terminology, the term "literal" refers to a textual representation of a value to be assigned to a variable. In C, you can assign a value to a variable in two ways – with literal representation, or with an expression.

The initialization of a variable in C is done as follows −

int x = 10;

On the other hand, an indirect initialization of a variable by assigning it the result of an expression is as follows −

int x = 10;
int y = x*2;

In the first case, 10 is an integer literal assigned to x. In the second case, the result of x*2 expression is assigned to y.

Literals in C

A literal is thus a value of a certain data type represented directly in the source code. Normally, literals are used to set a value of a variable.

On their own, literals don't form any of the programming elements. Different notations are used to represent the values of different data types.

Integer Literals

In the above example, 10 is an integer literal. A positive or negative whole number represented with digits 0 to 9, without a fractional part is a decimal integer literal. It must be within the acceptable range for the given OS platform.

Following examples assign decimal literals to int variables

int x = 200;
int y = -50;

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

int c = 89U;
long int d = 99998L;

C allows you to represent an integer in octal and hexadecimal number systems. For a literal representation of an octal, prefix the number with 0 (ensure that the number uses octal digits only, from 0 to 7). For a hexadecimal literal, prefix the number with 0x or 0X. The hexadecimal number must have 0 to 9, and A to F (or a to f) symbols.

Example

int oct = 025;
int hex = 0xa1;
printf("Octal to decimal: %d\n", oct);
printf("Hexadecimal to decimal: %d\n", hex);

Output

Octal to decimal: 21
Hexadecimal to decimal: 161

Modern C compilers also let you represent an integer as a binary number, you need to add a 0b prefix.

int x = 0b00010000;
printf("binary to decimal: %d", x);

Output

binary to decimal: 16

Examples of Integer Literals

Here are some examples of integer literals −

212         /* valid */
215u        /* valid */
0xFeeL      /* valid */
078         /* invalid: 8 is not an octal digit */
032UU       /* invalid: cannot repeat a suffix */

Following are other examples of various types of integer literals −

85         /* decimal */
0213       /* octal */
0x4b       /* hexadecimal */
30         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */

Floating-point Literals

Any real number with an integer part and a fractional part within the range acceptable to the compiler in use, and represented in digits, decimal point with an optional exponent symbol (e or E) is a floating-point literal. A floating point literal is generally used for initializing or setting the value of a float or a double variable in C.

Example

Following assignment examples use floating point literals with a decimal point eparating integer and fractional part.

float x = 10.55;
float y = -1.333;

Output

x: 10.550000
y: -1.333000

Floating point literals with a high degree of precision are can be stated with the exponentiation symbol e or E. This is called as a scientific notation of a float literal.

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.

Example

float x = 100E+4;
float y = -1.3E-03;
printf("x: %f\n", x);
printf("y: %f\n", y);

Output

x: 1000000.000000
y: -0.001300

Here are some examples of floating-point literals −

3.14159       /* valid */
314159E-5L    /* valid */
510E          /* invalid: incomplete exponent */
210f          /* invalid: no decimal or exponent */
.e55          /* invalid: missing integer or fraction */

Character Literals

A single character enclosed within single quote symbols (note that C recognizes straight quotes only. Hence, use ' to form a character literal and not ‘) represents a character literal.

char x = 'I';

The character literal is generally assigned to a char variable, that occupies a single byte. Using %c format specifier outputs the character, but use %d and you’ll obtain the ASCII value of the character.

Example

char x = 'I';

printf("x: %c\n", x);
printf("x: %d\n", x);

Output

x: I
x: 73

C defines a number of escape sequences as a sequence of characters starting with \ and an alternate meaning attached to the following characters. Even though it consists of more than one characters, it is put inside single quotes as an escape sequence produces the effect of a single non-printable character. For example, '\n ' is an escape sequence that represents a newline character, with the same effect as pressing Enter key.

char x = 'I';
char y = 'J';

printf("x: %c\ny: %c", x,y);

Output

x: I
y: J 

We shall learn more about the escape sequences in a latter chapter.

A character literal can also be a UNICODE representation of a character. Such a literal has /u in the beginning.

Example

char x = '\u09A9';
printf("x: %c\n", x);
printf("x: %d\n", x);

Output

x: ⌐
y: -87

String Literals

A sequence of characters put inside double quotation symbols " forms a string literal. C doesn’t provide a string variable. Instead, we need to use an array of char type to store a string.

Example

char arr[] = "Hello World";

printf("arr: %s", arr);

Output

arr: Hello World

A string literal may contain plain characters, escape sequences, and Unicode characters. plain characters, escape sequences, and universal characters.

char arr[] = "Hello \
World";

You can also have a literal representation of an array by putting its elements inside the curly brackets { and }

int arr[] = {10, 20, 30, 40};

Similarly, the curly brackets can also for a literal representation of a struct value.

struct marks {
   int phy;
   int che;
   int math
};
struct marks m1 = {50, 60, 70};

We shall learn about arrays and structures in details later in this tutorial.

Advertisements