2.7 – Constants in C language part-2

by subbu on August 16, 2013

Real constants:

Numbers with fractional part are called real constants. These are often called as floating point constants. Real constants are generally represented in two forms are fractional form and exponential form. Fractional form is the general form of representing a floating point value, exponential or scientific form is used when the value is so big or so small. Exponential form has two parts. The part before appearing to E is called mantissa and the part following E is called exponent. Here the mantissa must be multiplied by 10^exponent for example 345.25E-4 is equal to 345.25*10-4 and 4.56E4 is equal to 4.56*104.

real constants in C language

Rules for real constants in fractional form:

  • It must be a number
  • It can be either +ve or –ve but by default it is a positive number
  • It must have one decimal point.
  • No commas and spaces are used in a number

Examples:
+234.45
-34.02
12.0
0.0

Rules for real constants in exponential form:

  • It must be a number
  • The mantissa and exponential parts must be separated with letter e Or E
  • Both mantissa and exponential parts may be positive or negative but by default these are positive
  • It may have only a single decimal point.

Examples:
4.56E4
-5.6E-6
-0.4E7
-10.6E-2

Guess which are valid and invalid:
1) 45.0
2) -123
3) -345.56
4) 23.67.56
5) 354.78E8
6) -34.78E-4
7) 267e6
8) 12,345.36
9) 0.40E-2
10)  $45.32
11)34.
12).786
13)34e2.3

Show Answers
1) Valid
2) Invalid (It is an integer constant)
3) Valid
4) Invalid (Must have a single decimal place)
5) Valid
6) Valid
7) Valid
8) Invalid
9) Valid
10) Invalid (Other symbols can’t b used)
11)Valid
12)Valid
13)Invalid(exponential part must not have decimal points)

Example 1:

#include<stdio.h>
int main()
{
float x,y,z;
x=12675.34;
y=145.658E-2;
z=x+y;
printf("%f",z);
printf("\n%e",z);
return 0;
}

Output:
12676.796875
1.267680e+004

Example 2:

#include<stdio.h>
int main()
{
float x,y,z;
printf("Enter the first real number:");
scanf("%f",&x);
printf("Enter the second real number:");
scanf("%f",&y);
z=x+y;
printf("Sum in fractional: %f",z);
printf("\nSum in exponantial: %e",z);
return 0;
}

Output:
Enter the first real number:12675.34
Enter the second real number:145.658E-2
Sum in fractional: 12676.796875
Sum in exponantial: 1.267680e+004

Examples explained:
C language accepts floating point constants either in fractional or exponential form. The printf() prints in fractional form when %f is used as formatting character, prints in exponential form when %e is used as formatting character.

Example 3:

#include<stdio.h>
int main()
{
printf("%f\n%e",56.78E-2,45.786);
return 0;
}

Output:
0.567800
4.578600e+001

Example explained:
The printf() converts floating point constant from one format to another according to the formatting character we supply

Single character constant:

A single alphabet, digit or any special symbol placed in single inverted commas is called a single character constant. Make sure that both the inverted commas must be pointed to the left. ‘a’ is valid but `a’ is invalid.

Single character constant

Guess which are valid and invalid:

1) ‘X’
2) `v`
3) “c”
4) ‘hello’
5) ‘5’
6) ‘=’
7) ‘\n’

Show Answers

1) Valid
2) Invalid (Single inverted commas are directed towards right)
3) Invalid (Must not be placed in double inverted commas)
4) Invalid (Should have single character only)
5) Valid
6) Valid
7) Valid (\n is considered as single character )

Previous post:

Next post: