ค่าคงที่

ข้อมูล

จำนวนเต็ม

ค่าคงที่เป็นเก็บในหน่วยความจำเป็นจำนวนเต็ม

1 auto a = 398;
2 auto b = -100;
3 b = 017;
4 b = 0xEA;
5 unsigned c = 89u;
6 long d = 222l;
7 unsigned long f = 222ul;
8 f = 223lu;

ทศนิยม

ค่าคงที่เป็นเก็บในหน่วยความจำเป็นเลขทศนิยม

1 auto a = 3.98;
2 auto b = -10.0;
3 b = 3.844e5;
4 b = 1.671e-27;
5
6 float c = 3.44f;
7 double d = 3.75;
8 long double e = 1.7432L;

ตัวอักษร

ค่าคงที่เป็นเก็บในหน่วยความจำเป็นตัวอักษร

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  auto a = 'A';
 7  auto b = ' ';
 8
 9  char c = 'a';
10  wchar_t d = L'ก';
11  char16_t e = u'ก';
12  char32_t f = U'ก';
13
14  cout << a << endl;
15  cout << b << endl;
16  cout << c << endl;
17  cout << d << endl;
18  cout << e << endl;
19  cout << f << endl;
20
21  return 0;
22}

Character

Description

'\n'

new line

'\r'

carriage return

'\t'

tab

'\v'

vertical tab

'\b'

back space

'\f'

form feed

'\a'

alert

'\''

single quote

'\"'

double quote

'\?'

question mark

'\\'

backslash

ข้อความ

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  string a = "Paul Phoenix";
 7
 8  string text = "Paul Phoenix is going to \
 9solve a CPP problem.";
10
11  string utf8 = u8"Unicode text works here.\n \
12ใช้ภาษาไทยได้ด้วย\n \
13한국어도 사용 가능";
14
15  string raw = R"(None of the escape characters work: \r\n\b\a)";
16
17  cout << utf8 << endl;
18  cout << raw << endl;
19
20 return 0;
21}

ตรรกะและตัวชี้

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  bool skillous = true;
 7  bool practical = false;
 8  void* p = nullptr;
 9
10  return 0;
11}

ค่าคงที่ด้วย const

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5const float E = 2.71828182845f;
 6const long double PI = 3.14159265358979L;
 7const long long MAX = 10000000l;
 8const int MIN = -10000000;
 9
10int main() {
11
12  cout << E << endl;
13  cout << PI << endl;
14  cout << MAX << endl;
15  cout << MIN << endl;
16
17  cout.precision(18);
18  cout << E << endl;
19  cout << PI << endl;
20
21  return 0;
22}

ค่าคงที่ด้วย #define

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5#define E   2.71828182845f
 6#define PI  3.14159265358979L
 7#define MAX 10000000l
 8#define MIN -10000000
 9
10#define NL  '\n'
11
12int main() {
13
14  cout << E << NL;
15  cout << PI << NL;
16  cout << MAX << NL;
17  cout << MIN << NL;
18
19  cout.precision(18);
20  cout << E << NL;
21  cout << PI << NL;
22
23  return 0;
24}