What is a suffix and when is it used

closed account (EwCjE3v7)
What is a suffix and when is it used..Can someone give me examples.

And literals .you cannot change literals ..what does that mean

Like int x = 4..I don't get it

Thank you
Last edited on
If you're talking about the type suffixes, then see the Literals section on this page.

Constants
http://www.cplusplus.com/doc/tutorial/constants/

e.g.

1
2
3
    unsigned int uint = 75u;     // or U
    long         lval = 75L;     // or l (capital L looks less like a 1)
    float        fval = 3.1415f; // or F (f is usually used) 


Andy
closed account (EwCjE3v7)
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 ex long 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. 
closed account (EwCjE3v7)
Oh I get it now. Thanks so i should start using suffix now. Thanks Disch and Andy. :D

Two more question.
1. How do know wide character, long and long double they have the same suffix. How do you differentiate?

2. What is integer literals and decimal literals? What is the difference?
Last edited on
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.
closed account (EwCjE3v7)
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


How is -42 a double
Last edited on
How is -42 a double

It isn't. "Decimal" doesn't mean that it's a floating-point number. It means that it's base 10 - as opposed to octal or hexadecimal.
closed account (EwCjE3v7)
Oh ok..thanks
Topic archived. No new replies allowed.