A literal represents a fixed value, The value that cannot be changed once defined is called literal or constant. These constant values occupy memory but don't have any reference like variables. It can be of any type like int, char float, octal, hexadecimal etc.
There are five types of literals in C
- Integer literal
- Floating point or real literal
- Character literal
- String literal
- Boolean literal
Integer literal
Integer literal is followed by optional size or sign qualifier. L or l is a size qualifier. It specifies the size of an integer type as long. U or u is a sign qualifier. It specifies an integer type as unsigned (Can store only positive value).
An integer constant can specified by three ways:
- Decimal number: Any number that is from 0 to 9 is called decimal number and it's base is 10
- Octal number: prefixed with 0 followed by digits from 0 to 7. For example: 0126 is a octal number and it's base is 8.
- Hexadecimal number: number prefixed with 0x following digits from 0-9, a-z or A-Z. For example: 0x1A is a hexadecimal number and it's base is 16.
Following are the example of various types of integer literals:
85 // Decimal
0175; // Octal
0xa1b; // Hexadecimal
30 // int
30u // unsigned int
2016U // unsigned int
25L // long
30l // long
895UL // unsigned long
30ul // unsigned long
Note: Always be cautions while defining octal constants, Any numbers preceded with 0 must only contains digits from 0-7 otherwise compiler will report an error on compilation.
Order of the qualifier doesn't matter, ul or lu both are same.
Floating point or Real Literals:
You can represent floating point literals either in decimal form or exponential form.
Decimal form: While representing decimal form you must include the decimal point and it may preceded with + or - symbol representing positive or negative number.
For example: +12, -0.4, 5.6, 3.14159, .50
Exponential form: Exponential notations are helpful in representing any number with very big or small magnitude. It contains two parts, i.e. mantissa and exponent. For example: 96,000,000 and it can be expressed as 9.6e7 in exponential notation.
Syntax:
/* float literal in exponential notation */
[+/-]<Mantissa> <e/E>[+/-]<Exponents>
For Example:
/* Example of float literal in exponential notation */
4.6E-3, +1e23, 9.6e7, -9e2
More on this to be updated soon.....
--
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.