Integer Types
- Read chapter 3.
- Integers and floats are just a little strange
- C was built when memory was scarce.
- C++ was built on top of C
- So some items in C++ are a little complex.
- int
- The basic data type to represent an integer in a program.
-
int i{0};
- This is
INT_MIN
≤ i ≤ INT_MAX
- To get these predefined constants you must
-
#include <climits>
- Not as important but
-
sizeof(x)
is a function
- It returns the size of the object passed to it in bytes.
- See This reference
- Remember a byte is 8 bits.
- The long is another integer type.
- The C++ standard states that the long is at least as "big" as an int
- In most current implementations it is 64 bits.
- I need it now because I want to compute $2^31$ which will not fit into an int.
-
pow
- this is a function as well.
- It takes two parameters, or input values
- The first is the base
- The second is the exponent
- It returns baseexponent
- To do this, I need to include
cmath
- When the result of a computation does not "fit"
- This produces an incorrect computation.
- Which is called numeric overflow.
- Just a few weird things
- If you start a constant with 0, it thinks it is in octal.
-
0x1f
is a valid hex number
- A
short
is usually a 16 bit number.
- A
char
is a integer type
- But they are weird with I/O
- We will see more of this later.
- An
int
is between $-2^{31} $ and $ 2^{31}-1$
- An
unsigned int
is between $0 $ and $ 2^{32}-1$
- There is a
long long
but in g++ this is just a long.