Andy but why do you need to add the suffix? Like why is it there. We can tell the type because it is written before the name exlong val = 55 //there is no suffix and i know its a long why do i need to add it? Why are the advantages of using it?
We can tell the type because it is written before the name
That is the type of the variable, not necessarily the literal.
An example of where using a suffix to specify a literal has practical purpose:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// with literals....
float foo = 1.0f;
float bar = foo + 1.0f; // float + float = float. OK
//
// without literals...
float foo = 1.0; // compiler MIGHT complain because assigning a double to a float. Or it
// might silently convert without complaining
float bar = foo + 1.0; // float + double = double. This requires conversion from double
// to float, which likely WILL cause the compiler to complain (in at least the form of a
// warning) down casting to a smaller type may incur some precision loss.
1. How do know wide character, long and long double they have the same suffix. How do you differentiate?
wide characters use L as a prefix L'x'
longs use L as a suffix on an integer literal 1L
long doubles use L as a suffix on a floating point literal 1.0L
2. What is integer literals and decimal literals? What is the difference?
I assume by decimal literals you mean floating point literals.
The difference is that integer literals are integers and floating point literals are floating point. =P
1 is an integer, whereas 1.0 is a floating point.
Integers are exact representations of numbers and are perfectly precise. However they can only represent integers and cannot represent any floating point values.
Floating points are approximations of numbers and have limited precision. However they can represent a wider range of numbers.
Thanks disch but what is a double literal...like this
Although integer literals may be stored in signed types, technically speaking, the value of a decimal literal is never a negative number. If we write what appears to be a negative decimal literal, for example, -42, the minus sign is not part of the literal. The minus sign is an operator that negates the value of its (literal) operand