Basic Types Review
Objectives
We would like to :
- Review the basic types in c++
Notes
- Fundamental types on cppreference
- Integer Types
- char - 8 bits
- short - at least 16
- int - at least 16 bits, currently 32
- long - at least 32 bits, currently 64
- long long - at least 64 bits, currently 64
-
- In general, each of these can be signed or unsigned.
- Signed by default.
-
unsigned char
-
unsigned long long int
-
unsigned short
- 8 bits to a byte.
- sizeof
- Takes either a type or an expression.
- Returns the size in bytes.
- In general in n bits
- You can represent $2^n$ things.
- For unsigned
- 0 to $2^{n}-1$
- In 3 bits 0 to $2^3-1$ or 0 to 7
- In 8 bits 0 to 255
- Take a look at ints.cpp
- For signed
- $-2^{n-1}$ to $2^{n-1}-1$
- For three bits, $-2^2$ to $2^2-1$ or -4 to 3
- For 32 bits -2,147,483,648 to 2,147,483,647
- numeric_limits provides a way to discover the numeric limits for a system.
-
numeric_limits< type>
- You need to include <limits>.
- There are many member functions with supply information about a type.
- Some of these only apply to floats.
- Integer overflow occurs when you try to represent a number which is outside the range for a given size.
-
size_t
is used consistently
- This is a nickname for
unsigned int
right now.
- We will learn how to do this soon.
- Integer literals
- Don't start a normal int with a 0, unless it is 0.
- 0nnn is a number in ocatl, n in [0-7[
- 0xnnn is a number in hex n in [0-9,a-f,A-F]
- 0Xnnn is also hex
- 0bnnn is in binary n in [0-1]
- Suffixes (i usually don't use these)
-
suffix | meaning | Example |
---|
u/U | unsigned | 123u |
l/L | long | 123L |
lu/LU | unsigned long | 123LU |
ll/LL | long long | 123LL |
llu/LLU | unsigned long long | 123llu |
- Apparently LL or ll but not lL or Ll
- In c++14 and beyond, 3'000 is valid
- They are like comments
- they are completely ignored.
- So groups of three are not necessary.
- Narrowing, type casting and coercion
- When converting between types we have two choices
- When the programmer does it, we call it type casting or type conversion
- When the compiler does it, we call it coercion or automatic conversion.
- Neither is bad when we will not lose data.
- Changing from an int to a long int is ok.
- This is called widening and we will not lose data.
- Changing from a long int to an int can be problematic
- This is called narrowing and we might lose data.
- I am going to add a flag -Wconversion
- We can cast with one of three different methods
- C style :
(type) expression
- This is ok, but I don't like it.
- i = (int) j
- It gets confusing for larger expressions
- i = (int) 3*j + 10
- Early C++ style:
type(expression)
- This is better
- i = int(j)
- But it gets confusing for complex types
- i = unsigned long long int (j)
- Modern C++ style:
static_cast<type>(express)
- Much more typing but this is best
- i = static_cast<int>(j)
- No confusion
- i = static_cast<unsigned long long int>(3*j+10)
- From now on, I want you to explicitly cast, so you know what is going on.
- You might want to look at the io mainipulators
- dec/hex/oct
- setfill
- char holdFill = cout.fill();
- setw