Lecture Handout 5
Lecture Handout 5
Constants refer to fixed values that the program may not alter during its execution. These fixed
values are also called literals. We can say that the literals represent the fixed values that cannot be
modified. It also contains memory but does not have references as variables. For example, const
int =10; is a constant integer expression in which 10 is an integer literal.
Types of literals
o Integer literal
o Float literal
o Character literal
o String literal
Integer literal
It is a numeric literal that represents only integer type values. It represents the value neither in
fractional nor exponential part.
It is defined by representing the digits between 0 to 9. For example, 45, 67, etc.
It is defined as a number in which 0x or 0X is followed by the hexadecimal digits (i.e., digits from
0 to 9, alphabetical characters from (a-z) or (A-Z)).
L or l: It is a size qualifier that specifies the size of the integer type as long.
U or u: It is a sign qualifier that represents the type of the integer as unsigned. An unsigned
qualifier contains only positive values.
Example of integer literal.
#include <stdio.h>
int main()
{
const int a=23; // constant integer literal
printf("Integer literal : %d", a);
return 0;
}
Output
Integer literal : 23
Float literal
It is a literal that contains only floating-point values or real numbers. These real numbers contain
the number of parts such as integer part, real part, exponential part, and fractional part. The
floating-point literal must be specified either in decimal or in exponential form. Let's understand
these forms in brief.
Decimal form:The decimal form must contain either decimal point, exponential part, or both. If it
does not contain either of these, then the compiler will throw an error. The decimal notation can
be prefixed either by '+' or '-' symbol that specifies the positive and negative numbers.
#include <stdio.h>
1. int main()
2. {
3. const float a=4.5; // constant float literal
4. const float b=5.6; // constant float literal
5. float sum;
6. sum=a+b;
7. printf("%f", sum);
8. return 0;
9. }
Output
10.100000
Exponential form
The exponential form is useful when we want to represent the number, which is having a big
magnitude. It contains two parts, i.e., mantissa and exponent. For example, the number is
2340000000000, and it can be expressed as 2.34e12 in an exponential form.
the following are the rules for creating a float literal in exponential notation:
o In exponential notation, the mantissa can be specified either in decimal or fractional form.
o An exponent can be written in both uppercase and lowercase, i.e., e and E.
o We can use both the signs, i.e., positive and negative, before the mantissa and exponent.
o Spaces are not allowed
Character literal
A character literal contains a single character enclosed within single quotes. If multiple characters
are assigned to the variable, then we need to create a character array. If we try to store more than
one character in a variable, then the warning of a multi-character character constant will be
generated. Let's observe this scenario through an example.
1. #include <stdio.h>
2. int main()
3. {
4. const char c='ak';
5. printf("%c",c);
6. return 0;
7. }
In the above code, we have used two characters, i.e., 'ak', within single quotes. So, this statement
will generate a warning as shown below.
Warning generated:
o It can be represented by specifying a single character within single quotes. For example,
'a', 'b', etc.
o We can specify the escape sequence character within single quotes to represent a character
literal. For example, '\n', '\a', '\b'.
o We can also use the ASCII in integer to represent a character literal. For example, the ascii
value of 65 is 'A'.
o The octal and hexadecimal notation can be used as an escape sequence to represent a
character literal. For example, '\023', '\0x12'.
String literal
For example,
String1= "javatpoint";
String2= "family";
To concatenate the above two strings, we use '+' operator, as shown in the below statement:
"javatpoint " + "family"= javatpoint family
Note: If we represent a single character, i.e., 'b', then this character will occupy a single byte
as it is a character literal. And, if we represent the character within double quotes "b" then
it will occupy more bytes as it is a string literal.