How when should I use literals?

closed account (91vUpfjN)
Hi,

I understand how Integer Literals are to be used.

1
2
3
4
int main()
{
    const unsigned short int A = 60000u; //more or less?
}


However I don't see or understand why, when or even where I should use them. What's the point of the suffix? Shouldn't the program already know that it is unsigned?

On another note, what is the point of hexadecimal/octal and when/where/why should I use them? Is there any point to them?

Further reading would be welcomed. I've tried a few tutorial sites to explain why I should or should not use literals but I've had no joy (including cplusplus and tutorialspoint). Similarly with the hexadecimal features in which case I get sent to a couple of mathematics sites that can't decide themselves.
closed account (91vUpfjN)
Self answering here -_-;;;

http://www.parashift.com/c++-faq/numeric-literal-suffixes.html

You should use these suffixes when you need to force the compiler to treat the numeric literal as if it were the specified type.

it is a good discipline for programmers to force all numeric operands to be of the right type, as opposed to relying on the C++ rules for promoting/demoting numeric expressions. For example, if x is of type int and y is of type unsigned, it is a good idea to change x + y so the next programmer knows whether you intended to use unsigned arithmetic, e.g., unsigned(x) + y, or signed arithmetic: x + int(y). The other possibility is long arithmetic: long(x) + long(y). By using those casts, the code is more explicit and that's good in this case, since a lot of programmers don't know all the rules for implicit promotions.

C++ Faq 2012-07-04
Topic archived. No new replies allowed.