Sprout 2021/04/17
字元 | char | 1 byte |
整數 | int | 4 bytes |
單精度浮點數 | float | 4 bytes |
倍精度浮點數 | double | 8 bytes |
字元 | char | 1 byte |
整數 | int | 4 bytes |
單精度浮點數 | float | 4 bytes |
倍精度浮點數 | double | 8 bytes |
#include <iostream>
using namespace std;
int main() {
cout << sizeof(char) << "\n";
cout << sizeof(int) << "\n";
cout << sizeof(float) << "\n";
cout << sizeof(double) << "\n";
}
int | 4 bytes |
---|---|
short int | ? bytes |
long int | ? bytes |
long long int | ? bytes |
int | 4 bytes |
---|---|
short int | 2 bytes |
long int | 4 bytes / 8 bytes |
long long int | 8 bytes |
int | 4 bytes = 32 bits | -2^31 ~ 2^31 - 1 |
---|---|---|
unsigned int | 4 bytes = 32 bits | 0 ~ 2^32 - 1 |
unsigned num = -5; // ???
binary | decimal | binary | decimal |
---|---|---|---|
0111 | 7 | 1111 | -1 |
0110 | 6 | 1110 | -2 |
0101 | 5 | 1101 | -3 |
0100 | 4 | 1100 | -4 |
0011 | 3 | 1011 | -5 |
0010 | 2 | 1010 | -6 |
0001 | 1 | 1001 | -7 |
0000 | 0 | 1000 | -8 |
binary | decimal | binary | decimal |
---|---|---|---|
0111 | 7 | 1111 | -1 |
0110 | 6 | 1110 | -2 |
0101 | 5 | 1101 | -3 |
0100 | 4 | 1100 | -4 |
0011 | 3 | 1011 | -5 |
0010 | 2 | 1010 | -6 |
0001 | 1 | 1001 | -7 |
0000 | 0 | 1000 | -8 |
int | 4 bytes | -2^31 ~ 2^31 - 1 |
unsigned int | 4 bytes | 0 ~ 2^32 - 1 |
short int | 2 bytes | -2^15 ~ 2^15 - 1 |
unsigned short int | 2 bytes | 0 ~ 2^16 - 1 |
long long int | 8 bytes | -2^63 ~ 2^63 - 1 |
unsigned long long int | 8 bytes | 0 ~ 2^64 - 1 |
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
cout << "int range\t\t" << INT_MIN << "\t" << INT_MAX << "\n";
cout << "unsigned int range\t" << 0 << "\t\t" << UINT_MAX << "\n";
cout << "short int range\t\t" << SHRT_MIN << "\t\t" << SHRT_MAX << "\n";
}
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int a = INT_MAX;
cout << "a = " << a << "\n";
cout << "a + 1 = " << a + 1 << "\n";
if (a < a + 1)
cout << "a < a + 1\n";
else
cout << "a >= a + 1\n";
}
#include <iostream>
using namespace std;
int main() {
const double PI;
}
#include <iostream>
using namespace std;
int main() {
const double PI = 3.14;
PI = 6.28;
}
#include <iostream>
using namespace std;
int area(const int w, const int h) {
return w * h;
}
int main() {
cout << area(3, 4);
}
#include <iostream>
using namespace std;
void count() {
int cnt = 0;
cout << cnt++;
}
int main() {
for(int i = 0; i < 5; i++)
count();
}
#include <iostream>
using namespace std;
void count() {
static int cnt = 0;
cout << cnt++;
}
int main() {
for(int i = 0; i < 5; i++)
count();
}
// PI.cpp
#include <iostream>
double PI = 3.14;
// print_PI.cpp
#include <iostream>
using namespace std;
int main() {
extern double PI;
cout << PI << "\n";
}
> g++ PI.cpp call_PI.cpp
> ./a.out
> 3.14
// PI.cpp
#include <iostream>
using namespace std;
static double PI = 3.14;
// print_PI.cpp
#include <iostream>
using namespace std;
int main() {
extern double PI;
cout << PI << "\n";
}