C Language | Real Literals

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.

To display float values in different format

%f : by default displays six digits after decimal
%.2f : will display 2 digits after decimal
%.3f : will display 3 digits after decimal
%g : will display the number as it is

#include<stdio.h>
int main()
{
	float n=15.456;
	printf("n = %f\n",n);
	printf("n = %.2f\n",n);
	printf("n = %.4f\n",n);
	printf("n = %g\n",n);
    return(0);
}

Output:

n = 15.456000
n = 15.46
n = 15.4560
n = 15.456

To display the number in scientific format

%e : will display number is scientific format and six digits after decimal
%E : will display number is scientific format and six digits after decimal
%.2e : will display number is scientific format and two digits after decimal
%.4E : will display number is scientific format and four digits after decimal

#include<stdio.h>
int main()
{
	float n=15.456;
	printf("n = %e\n",n);
	printf("n = %E\n",n);
	
	printf("n = %.2e\n",n);
	printf("n = %.4E\n",n);
	
	return(0);
}

Output:

n = 1.545600e+001
n = 1.545600E+001
n = 1.55e+001
n = 1.5456E+001

Single character Literal:

A single character literal (simply character constant) contains a single character enclosed within a pair of single quote marks.

‘a’ ‘5’ ‘x’ ‘A’

‘xy’ //invalid

To take input for a character and display it

%c : is used to take input for a character

#include<stdio.h>
int main()
{
	char ch;
	printf("Enter any character ");
	scanf("%c",&ch);
	printf("Character = %c\n",ch);
	return(0);
}

Output:

Enter any character A
Character = A