C Language | Literals

Literals

Literals are value constants assigned to the variable in a program. “C” language supports several types of literals.

Integer Literals: An integer literal refers to a sequence of digits(without decimal). There are three types of integers namely

  • Decimal integers
  • Octal integers
  • Hexadecimal integers.

Decimal integers

Decimal integers are a set of numbers made up of digits from 0 to 9, preceded by an optional + or – sign. Valid examples are :

34 , 56 , -89 , 0  //valid

Embedded spaces, commas, and non-digit characters are not permitted.

15 765 20,000     $1900    // all error

Octal integers

An octal integer consist of any combination of digits from 0 to 7 with a leading “0” . Some examples of octal integers are

037, 07 , 0345, 0561

 Hexadecimal integers.

A sequence of digits preceded by 0x or 0X is considered as a hexadecimal integer. It may also include alphabets from A to F or a to f. A to F represent values from 10 to 15. Some valid examples of hexadecimal are

0X2, 0x45, 0Xab, 0xa2

Question:
C program to take input for a number and print its decimal, octal and hexadecimal form?
Sol:

#include<stdio.h>
int main()
{
	int a,b,c;
	printf("Enter any no ");
	scanf("%d",&a);
	printf("Decinal form  = %d\n",a);
	printf("Octal form = %o\n",a);
	printf("Hexadecimal form = %x %X\n",a,a);
	return(0);

}

Output:

Enter any no 10
Decinal form  = 10
Octal form = 12
Hexadecimal form = a A

Question:
C program to take input for a number in octal form and print its decimal, octal and hexadecimal form?
Sol:

#include<stdio.h>
int main()
{
	int a,b,c;
	printf("Enter any no ");
	scanf("%o",&a);
	printf("Decinal form  = %d\n",a);
	printf("Octal form = %o\n",a);
	printf("Hexadecimal form = %x %X\n",a,a);
	return(0);

}

Output:

Enter any no 12
Decinal form  = 10
Octal form = 12
Hexadecimal form = a A

Question:
C program to take input for a number in hexadecimal form and print its decimal, octal and hexadecimal form?
Sol:

#include<stdio.h>
int main()
{
	int a,b,c;
	printf("Enter any no ");
	scanf("%x",&a);
	printf("Decinal form  = %d\n",a);
	printf("Octal form = %o\n",a);
	printf("Hexadecimal form = %x %X\n",a,a);
	return(0);

}

Output:

Enter any no a
Decinal form  = 10
Octal form = 12
Hexadecimal form = a A

Real Literals / Real Constants:

Integer numbers are inadequate to represent the quantities that vary continuously, such as distance, height, temperatures, price, and so on. These are numeric values containing fractional parts (i.e. with decimals).

9.98   0.98   10.78

 A real constant may also be represented in exponential or scientific notation.

10e2    23E+2  234.34e4     23.56e-2 56.4E-3

Embedded white space is not allowed.