• C Programming Video Tutorials

C - Literals



The term "literal" in computer programming terminology 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 −

  • Using literal representation
  • Using 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".

A literal is thus a value of a certain data type represented directly into 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 element. Different notations are used to represent the values of different data types.

Integer Literals in C

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

Take a look at the following example −

#include <stdio.h>

int main(){

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

Output

On running this code, you will get the following output −

Octal to decimal: 21
Hexadecimal to decimal: 161

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

Example

Take a look at the following example −

#include <stdio.h>

int main(){

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

Output

Run the code and check its output −

binary to decimal: 16

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 */

Here are some 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 in C

A floating-point literal in C is a 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).

A floating point literal is generally used for initializing or setting the value of a float or a double variable in C.

Example

The following assignment examples use floating point literals with a decimal point separating the integer and the fractional part −

#include <stdio.h>

int main(){

   float x = 10.55;
   float y = -1.333;
   printf("x and y are: %f, %f", x, y);
}

Output

You will get the following output −

x and y are: 10.550000, -1.333000

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

While representing decimal form, you must include the decimal point, the exponent, or both. While representing exponential form, you must include the integer part, the fractional part, or both.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

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

Output

When you run this code, it will produce the following 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 in C

A character literal in C is a single character enclosed within single quote symbols. Note that C recognizes straight quotes only. Hence, use ' to form a character literal and not ). Here is an example −

char x = 'I';

Character literals are generally assigned to a char variable that occupies a single byte. Using the %c format specifier outputs the character. Use %d and you’ll obtain the ASCII value of the character.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

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

Output

Run the code and check its output −

x: I
x: 73

Escape Sequences in C

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 an escape sequence consists of more than one characters, it is put inside single quotes. 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 the Enter key.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

   char x = 'I';
   char y = 'J';
   printf("x: %c\ny: %c", x,y);
}

Output

Here you will get this output −

x: I
y: J 

We shall learn more about escape sequences in a later chapter.

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

Example

Take a look at the following example −

#include <stdio.h>

int main(){

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

Output

On running this code, you will get the following output −

x: ⌐
y: -87

String Literals in C

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

Take a look at the following example −

#include <stdio.h>

int main(){

   char arr[] = "Hello World";
   printf("arr: %s", arr);
}

Output

Run the code and check its output −

arr: Hello World

A string literal may contain plain characters, escape sequences, and Unicode characters. For example −

char arr[] = "Hello \
World";

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

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

Similarly, the curly brackets can also be used for a literal representation of a struct value. For example −

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

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

Advertisements