Loose-Info.com
Last Update 2023/10/17
TOP - 各種テスト - C++ - 定数

概要

整数定数と明示的に定数の型を示すサフィックス
整数定数が割り当てられる型階層の確認
文字定数の出力テスト
文字定数(ワイド文字)の出力テスト
文字定数(エスケープシーケンス)の出力テスト
浮動小数点定数
文字列リテラル(プレフィックス無し、UTF-8)
文字列リテラル(ワイド文字)
文字列リテラルの連結
raw-stringリテラル
ブール値リテラル
ポインタリテラル


整数定数と明示的に定数の型を示すサフィックス


sample.cpp
#include <iostream> #include <iomanip> #include <bitset> int main() { std::cout << "整数定数" << std::endl; unsigned int samp_int1 = 123; /* 10進定数 */ unsigned int samp_int2 = 0x7b; /* 16進定数(0x + 小文字) */ unsigned int samp_int3 = 0x7B; /* 16進定数(0x + 大文字) */ unsigned int samp_int4 = 0X7b; /* 16進定数(0X + 小文字) */ unsigned int samp_int5 = 0X7B; /* 16進定数(0X + 大文字) */ unsigned int samp_int6 = 0173; /* 8進定数(プレフィックス : 0) */ unsigned int samp_int7 = 0b01111011; /* 2進定数(プレフィックス : 0b) */ unsigned int samp_int8 = 0B01111011; /* 2進定数(プレフィックス : 0B) */ std::bitset<8> bin_int7(samp_int7); std::bitset<8> bin_int8(samp_int8); std::cout << "10進定数 " << std::setw(3) << samp_int1 << std::endl; std::cout << std::uppercase; std::cout << "16進定数 0x" << std::setw(2) << std::hex << std::nouppercase << samp_int2 << " --- " << std::dec << samp_int2 << std::endl; std::cout << "16進定数 0x" << std::setw(2) << std::hex << std::uppercase << samp_int3 << " --- " << std::dec << samp_int3 << std::endl; std::cout << "16進定数 0X" << std::setw(2) << std::hex << std::nouppercase << samp_int4 << " --- " << std::dec << samp_int4 << std::endl; std::cout << "16進定数 0X" << std::setw(2) << std::hex << std::uppercase << samp_int5 << " --- " << std::dec << samp_int5 << std::endl; std::cout << " 8進定数 0" << std::oct << samp_int6 << " --- " << std::dec << samp_int6 << std::endl; std::cout << " 2進定数 0b" << bin_int7 << " --- " << std::dec << samp_int7 << std::endl; std::cout << " 2進定数 0B" << bin_int8 << " --- " << std::dec << samp_int8 << "\n" << std::endl; std::cout << "明示的に定数の型を示すサフィックス" << std::endl; std::cout << "unsigned int u または U : " << 123u << "(123u) " << 123U << "(123U)" << std::endl; std::cout << "long l または L : " << 123l << "(123l) " << 123L << "(123L)" << std::endl; std::cout << "long long ll または LL : " << 123ll << "(123ll) " << 123LL << "(123LL)" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 整数定数 10進定数 123 16進定数 0x7b --- 123 16進定数 0x7B --- 123 16進定数 0X7b --- 123 16進定数 0X7B --- 123 8進定数 0173 --- 123 2進定数 0b01111011 --- 123 2進定数 0B01111011 --- 123 明示的に定数の型を示すサフィックス unsigned int u または U : 123(123u) 123(123U) long l または L : 123(123l) 123(123L) long long ll または LL : 123(123ll) 123(123LL)

整数定数が割り当てられる型階層の確認


sample.cpp
#include <iostream> #include <limits> #include <typeinfo> int main() { std::cout << "整数定数が割り当てられる型階層の確認" << std::endl; std::cout << " (以下、実装依存)\n" << std::endl; std::cout << "各種整数型最大値" << std::endl; /* int 最大値 */ constexpr int mx_int = std::numeric_limits<int>::max(); std::cout << "int 最大値 = " << mx_int << std::endl; /* unsigned int 最大値 */ constexpr unsigned int mx_uint = std::numeric_limits<unsigned int>::max(); std::cout << "unsigned int 最大値 = " << mx_uint << std::endl; /* long int 最大値 */ constexpr long int mx_lint = std::numeric_limits<long int>::max(); std::cout << "long int 最大値 = " << mx_lint << std::endl; /* unsigned long int 最大値 */ constexpr unsigned long int mx_ulint = std::numeric_limits<unsigned long int>::max(); std::cout << "unsigned long int 最大値 = " << mx_ulint << std::endl; /* long long int 最大値 */ constexpr long long int mx_llint = std::numeric_limits<long long int>::max(); std::cout << "long long int 最大値 = " << mx_llint << std::endl; /* unsigned long long int 最大値 */ constexpr unsigned long long int mx_ullint = std::numeric_limits<unsigned long long int>::max(); std::cout << "unsigned long long int 最大値 = " << mx_ullint << "\n" << std::endl; std::cout << "各種整数型の実装定義の型名" << std::endl; std::cout << typeid(int).name() << " --- int" << std::endl; std::cout << typeid(unsigned int).name() << " --- unsigned int" << std::endl; std::cout << typeid(long int).name() << " --- long int" << std::endl; std::cout << typeid(unsigned long int).name() << " --- unsigned long int" << std::endl; std::cout << typeid(long long int).name() << " --- long long int" << std::endl; std::cout << typeid(unsigned long long int).name() << " --- unsigned long long int" << std::endl; std::cout << "\n実装環境において、" << std::endl; std::cout << "long と long long の最大値が同一であるため、" << std::endl; std::cout << "サフィックスLL以外の long long の検証は省略\n" << std::endl; std::cout << "10進定数・サフィックス無し" << std::endl; std::cout << "2147483647(int最大値) 型名 --- [" << typeid(2147483647).name() << "]" << std::endl; std::cout << "2147483648(int最大値+1) 型名 --- [" << typeid(2147483648).name() << "]" << std::endl; std::cout << "9223372036854775807(long int最大値) 型名 --- [" << typeid(9223372036854775807).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックス無し" << std::endl; std::cout << "0x7FFFFFFF(int最大値) 型名 --- [" << typeid(0x7FFFFFFF).name() << "]" << std::endl; std::cout << "0x80000000(int最大値+1) 型名 --- [" << typeid(0x80000000).name() << "]" << std::endl; std::cout << "0xFFFFFFFF(unsigned int最大値) 型名 --- [" << typeid(0xFFFFFFFF).name() << "]" << std::endl; std::cout << "0x100000000(unsigned int最大値+1) 型名 --- [" << typeid(0x100000000).name() << "]" << std::endl; std::cout << "0x7FFFFFFFFFFFFFFF(long int最大値) 型名 --- [" << typeid(0x7FFFFFFFFFFFFFFF).name() << "]" << std::endl; std::cout << "0x8000000000000000(long int最大値+1) 型名 --- [" << typeid(0x8000000000000000).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFF(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFF).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックス無し" << std::endl; std::cout << "017777777777(int最大値) 型名 --- [" << typeid(017777777777).name() << "]" << std::endl; std::cout << "020000000000(int最大値+1) 型名 --- [" << typeid(020000000000).name() << "]" << std::endl; std::cout << "037777777777(unsigned int最大値) 型名 --- [" << typeid(037777777777).name() << "]" << std::endl; std::cout << "040000000000(unsigned int最大値+1) 型名 --- [" << typeid(040000000000).name() << "]" << std::endl; std::cout << "0777777777777777777777(long int最大値) 型名 --- [" << typeid(0777777777777777777777).name() << "]" << std::endl; std::cout << "01000000000000000000000(long int最大値+1) 型名 --- [" << typeid(01000000000000000000000).name() << "]" << std::endl; std::cout << "01777777777777777777777(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックス無し" << std::endl; std::cout << "0b1111111111111111111111111111111(int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111).name() << "]" << std::endl; std::cout << "0b10000000000000000000000000000000(int最大値+1) 型名 --- [" << typeid(0b10000000000000000000000000000000).name() << "]" << std::endl; std::cout << "0b11111111111111111111111111111111(unsigned int最大値) 型名 --- [" << typeid(0b11111111111111111111111111111111).name() << "]" << std::endl; std::cout << "0b100000000000000000000000000000000(unsigned int最大値+1) 型名 --- [" << typeid(0b100000000000000000000000000000000).name() << "]" << std::endl; std::cout << "0b111111111111111111111111111111111111111111111111111111111111111(long int最大値) 型名 --- [" << typeid(0b111111111111111111111111111111111111111111111111111111111111111).name() << "]" << std::endl; std::cout << "0b1000000000000000000000000000000000000000000000000000000000000000(long int最大値+1) 型名 --- [" << typeid(0b1000000000000000000000000000000000000000000000000000000000000000).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111).name() << "]\n" << std::endl; std::cout << "10進定数・サフィックスU(u)" << std::endl; std::cout << "4294967295U(unsigned int最大値) 型名 --- [" << typeid(4294967295U).name() << "]" << std::endl; std::cout << "4294967296U(unsigned int最大値+1) 型名 --- [" << typeid(4294967296U).name() << "]" << std::endl; std::cout << "18446744073709551615U(unsigned long int最大値) 型名 --- [" << typeid(18446744073709551615U).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックスU(u)" << std::endl; std::cout << "0xFFFFFFFFU(unsigned int最大値) 型名 --- [" << typeid(0xFFFFFFFFU).name() << "]" << std::endl; std::cout << "0x100000000U(unsigned int最大値+1) 型名 --- [" << typeid(0x100000000U).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFFU(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFFU).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックスU(u)" << std::endl; std::cout << "037777777777U(unsigned int最大値) 型名 --- [" << typeid(037777777777U).name() << "]" << std::endl; std::cout << "040000000000U(unsigned int最大値+1) 型名 --- [" << typeid(040000000000U).name() << "]" << std::endl; std::cout << "01777777777777777777777U(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777U).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックスU(u)" << std::endl; std::cout << "0b11111111111111111111111111111111U(unsigned int最大値) 型名 --- [" << typeid(0b11111111111111111111111111111111U).name() << "]" << std::endl; std::cout << "0b100000000000000000000000000000000U(unsigned int最大値+1) 型名 --- [" << typeid(0b100000000000000000000000000000000U).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111U(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111U).name() << "]\n" << std::endl; std::cout << "10進定数・サフィックスL(l)" << std::endl; std::cout << "2147483647L(int最大値) 型名 --- [" << typeid(2147483647L).name() << "]" << std::endl; std::cout << "2147483648L(int最大値+1) 型名 --- [" << typeid(2147483648L).name() << "]" << std::endl; std::cout << "9223372036854775807L(long int最大値) 型名 --- [" << typeid(9223372036854775807L).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックスL(l)" << std::endl; std::cout << "0x7FFFFFFFL(int最大値) 型名 --- [" << typeid(0x7FFFFFFFL).name() << "]" << std::endl; std::cout << "0xFFFFFFFFL(unsigned int最大値) 型名 --- [" << typeid(0xFFFFFFFFL).name() << "]" << std::endl; std::cout << "0x7FFFFFFFFFFFFFFFL(long int最大値) 型名 --- [" << typeid(0x7FFFFFFFFFFFFFFFL).name() << "]" << std::endl; std::cout << "0x8000000000000000L(long int最大値+1) 型名 --- [" << typeid(0x8000000000000000L).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFFL(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFFL).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックスL(l)" << std::endl; std::cout << "017777777777L(int最大値) 型名 --- [" << typeid(017777777777L).name() << "]" << std::endl; std::cout << "037777777777L(unsigned int最大値) 型名 --- [" << typeid(037777777777L).name() << "]" << std::endl; std::cout << "0777777777777777777777L(long int最大値) 型名 --- [" << typeid(0777777777777777777777L).name() << "]" << std::endl; std::cout << "01000000000000000000000L(long int最大値+1) 型名 --- [" << typeid(01000000000000000000000L).name() << "]" << std::endl; std::cout << "01777777777777777777777L(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777L).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックスL(l)" << std::endl; std::cout << "0b1111111111111111111111111111111L(int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111L).name() << "]" << std::endl; std::cout << "0b11111111111111111111111111111111L(unsigned int最大値) 型名 --- [" << typeid(0b11111111111111111111111111111111L).name() << "]" << std::endl; std::cout << "0b111111111111111111111111111111111111111111111111111111111111111L(long int最大値) 型名 --- [" << typeid(0b111111111111111111111111111111111111111111111111111111111111111L).name() << "]" << std::endl; std::cout << "0b1000000000000000000000000000000000000000000000000000000000000000L(long int最大値+1) 型名 --- [" << typeid(0b1000000000000000000000000000000000000000000000000000000000000000L).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111L(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111L).name() << "]\n" << std::endl; std::cout << "10進定数・サフィックスU(u)L(l)" << std::endl; std::cout << "4294967295UL(unsigned int最大値) 型名 --- [" << typeid(4294967295UL).name() << "]" << std::endl; std::cout << "18446744073709551615UL(unsigned long int最大値) 型名 --- [" << typeid(18446744073709551615UL).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックスU(u)L(l)" << std::endl; std::cout << "0xFFFFFFFFUL(unsigned int最大値) 型名 --- [" << typeid(0xFFFFFFFFUL).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFFUL(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFFUL).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックスU(u)L(l)" << std::endl; std::cout << "037777777777UL(unsigned int最大値) 型名 --- [" << typeid(037777777777UL).name() << "]" << std::endl; std::cout << "01777777777777777777777UL(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777UL).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックスU(u)L(l)" << std::endl; std::cout << "0b11111111111111111111111111111111UL(unsigned int最大値) 型名 --- [" << typeid(0b11111111111111111111111111111111UL).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111UL(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111UL).name() << "]\n" << std::endl; std::cout << "10進定数・サフィックスLL(ll)" << std::endl; std::cout << "2147483647LL(int最大値) 型名 --- [" << typeid(2147483647LL).name() << "]" << std::endl; std::cout << "9223372036854775807LL(long int最大値) 型名 --- [" << typeid(9223372036854775807LL).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックスLL(ll)" << std::endl; std::cout << "0x7FFFFFFFLL(int最大値) 型名 --- [" << typeid(0x7FFFFFFFLL).name() << "]" << std::endl; std::cout << "0x7FFFFFFFFFFFFFFFLL(long int最大値) 型名 --- [" << typeid(0x7FFFFFFFFFFFFFFFLL).name() << "]" << std::endl; std::cout << "0x8000000000000000LL(long int最大値+1) 型名 --- [" << typeid(0x8000000000000000LL).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFFLL(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFFLL).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックスLL(ll)" << std::endl; std::cout << "017777777777LL(int最大値) 型名 --- [" << typeid(017777777777LL).name() << "]" << std::endl; std::cout << "0777777777777777777777LL(long int最大値) 型名 --- [" << typeid(0777777777777777777777LL).name() << "]" << std::endl; std::cout << "01000000000000000000000LL(long int最大値+1) 型名 --- [" << typeid(01000000000000000000000LL).name() << "]" << std::endl; std::cout << "01777777777777777777777LL(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777LL).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックスLL(ll)" << std::endl; std::cout << "0b1111111111111111111111111111111LL(int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111LL).name() << "]" << std::endl; std::cout << "0b111111111111111111111111111111111111111111111111111111111111111LL(long int最大値) 型名 --- [" << typeid(0b111111111111111111111111111111111111111111111111111111111111111LL).name() << "]" << std::endl; std::cout << "0b1000000000000000000000000000000000000000000000000000000000000000LL(long int最大値+1) 型名 --- [" << typeid(0b1000000000000000000000000000000000000000000000000000000000000000LL).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111LL(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111LL).name() << "]\n" << std::endl; std::cout << "10進定数・サフィックスU(u)LL(ll)" << std::endl; std::cout << "4294967295ULL(unsigned int最大値) 型名 --- [" << typeid(4294967295ULL).name() << "]" << std::endl; std::cout << "18446744073709551615ULL(unsigned long int最大値) 型名 --- [" << typeid(18446744073709551615ULL).name() << "]\n" << std::endl; std::cout << "16進定数・サフィックスU(u)LL(ll)" << std::endl; std::cout << "0xFFFFFFFFULL(unsigned int最大値) 型名 --- [" << typeid(0xFFFFFFFFULL).name() << "]" << std::endl; std::cout << "0xFFFFFFFFFFFFFFFFULL(unsigned long int最大値) 型名 --- [" << typeid(0xFFFFFFFFFFFFFFFFULL).name() << "]\n" << std::endl; std::cout << "8進定数・サフィックスU(u)LL(ll)" << std::endl; std::cout << "037777777777ULL(unsigned int最大値) 型名 --- [" << typeid(037777777777ULL).name() << "]" << std::endl; std::cout << "01777777777777777777777ULL(unsigned long int最大値) 型名 --- [" << typeid(01777777777777777777777ULL).name() << "]\n" << std::endl; std::cout << "2進定数・サフィックスU(u)LL(ll)" << std::endl; std::cout << "0b11111111111111111111111111111111ULL(unsigned int最大値) 型名 --- [" << typeid(0b11111111111111111111111111111111ULL).name() << "]" << std::endl; std::cout << "0b1111111111111111111111111111111111111111111111111111111111111111ULL(unsigned long int最大値) 型名 --- [" << typeid(0b1111111111111111111111111111111111111111111111111111111111111111ULL).name() << "]\n" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 整数定数が割り当てられる型階層の確認 (以下、実装依存) 各種整数型最大値 int 最大値 = 2147483647 unsigned int 最大値 = 4294967295 long int 最大値 = 9223372036854775807 unsigned long int 最大値 = 18446744073709551615 long long int 最大値 = 9223372036854775807 unsigned long long int 最大値 = 18446744073709551615 各種整数型の実装定義の型名 i --- int j --- unsigned int l --- long int m --- unsigned long int x --- long long int y --- unsigned long long int 実装環境において、 long と long long の最大値が同一であるため、 サフィックスLL以外の long long の検証は省略 10進定数・サフィックス無し 2147483647(int最大値) 型名 --- [i] 2147483648(int最大値+1) 型名 --- [l] 9223372036854775807(long int最大値) 型名 --- [l] 16進定数・サフィックス無し 0x7FFFFFFF(int最大値) 型名 --- [i] 0x80000000(int最大値+1) 型名 --- [j] 0xFFFFFFFF(unsigned int最大値) 型名 --- [j] 0x100000000(unsigned int最大値+1) 型名 --- [l] 0x7FFFFFFFFFFFFFFF(long int最大値) 型名 --- [l] 0x8000000000000000(long int最大値+1) 型名 --- [m] 0xFFFFFFFFFFFFFFFF(unsigned long int最大値) 型名 --- [m] 8進定数・サフィックス無し 017777777777(int最大値) 型名 --- [i] 020000000000(int最大値+1) 型名 --- [j] 037777777777(unsigned int最大値) 型名 --- [j] 040000000000(unsigned int最大値+1) 型名 --- [l] 0777777777777777777777(long int最大値) 型名 --- [l] 01000000000000000000000(long int最大値+1) 型名 --- [m] 01777777777777777777777(unsigned long int最大値) 型名 --- [m] 2進定数・サフィックス無し 0b1111111111111111111111111111111(int最大値) 型名 --- [i] 0b10000000000000000000000000000000(int最大値+1) 型名 --- [j] 0b11111111111111111111111111111111(unsigned int最大値) 型名 --- [j] 0b100000000000000000000000000000000(unsigned int最大値+1) 型名 --- [l] 0b111111111111111111111111111111111111111111111111111111111111111(long int最大値) 型名 --- [l] 0b1000000000000000000000000000000000000000000000000000000000000000(long int最大値+1) 型名 --- [m] 0b1111111111111111111111111111111111111111111111111111111111111111(unsigned long int最大値) 型名 --- [m] 10進定数・サフィックスU(u) 4294967295U(unsigned int最大値) 型名 --- [j] 4294967296U(unsigned int最大値+1) 型名 --- [m] 18446744073709551615U(unsigned long int最大値) 型名 --- [m] 16進定数・サフィックスU(u) 0xFFFFFFFFU(unsigned int最大値) 型名 --- [j] 0x100000000U(unsigned int最大値+1) 型名 --- [m] 0xFFFFFFFFFFFFFFFFU(unsigned long int最大値) 型名 --- [m] 8進定数・サフィックスU(u) 037777777777U(unsigned int最大値) 型名 --- [j] 040000000000U(unsigned int最大値+1) 型名 --- [m] 01777777777777777777777U(unsigned long int最大値) 型名 --- [m] 2進定数・サフィックスU(u) 0b11111111111111111111111111111111U(unsigned int最大値) 型名 --- [j] 0b100000000000000000000000000000000U(unsigned int最大値+1) 型名 --- [m] 0b1111111111111111111111111111111111111111111111111111111111111111U(unsigned long int最大値) 型名 --- [m] 10進定数・サフィックスL(l) 2147483647L(int最大値) 型名 --- [l] 2147483648L(int最大値+1) 型名 --- [l] 9223372036854775807L(long int最大値) 型名 --- [l] 16進定数・サフィックスL(l) 0x7FFFFFFFL(int最大値) 型名 --- [l] 0xFFFFFFFFL(unsigned int最大値) 型名 --- [l] 0x7FFFFFFFFFFFFFFFL(long int最大値) 型名 --- [l] 0x8000000000000000L(long int最大値+1) 型名 --- [m] 0xFFFFFFFFFFFFFFFFL(unsigned long int最大値) 型名 --- [m] 8進定数・サフィックスL(l) 017777777777L(int最大値) 型名 --- [l] 037777777777L(unsigned int最大値) 型名 --- [l] 0777777777777777777777L(long int最大値) 型名 --- [l] 01000000000000000000000L(long int最大値+1) 型名 --- [m] 01777777777777777777777L(unsigned long int最大値) 型名 --- [m] 2進定数・サフィックスL(l) 0b1111111111111111111111111111111L(int最大値) 型名 --- [l] 0b11111111111111111111111111111111L(unsigned int最大値) 型名 --- [l] 0b111111111111111111111111111111111111111111111111111111111111111L(long int最大値) 型名 --- [l] 0b1000000000000000000000000000000000000000000000000000000000000000L(long int最大値+1) 型名 --- [m] 0b1111111111111111111111111111111111111111111111111111111111111111L(unsigned long int最大値) 型名 --- [m] 10進定数・サフィックスU(u)L(l) 4294967295UL(unsigned int最大値) 型名 --- [m] 18446744073709551615UL(unsigned long int最大値) 型名 --- [m] 16進定数・サフィックスU(u)L(l) 0xFFFFFFFFUL(unsigned int最大値) 型名 --- [m] 0xFFFFFFFFFFFFFFFFUL(unsigned long int最大値) 型名 --- [m] 8進定数・サフィックスU(u)L(l) 037777777777UL(unsigned int最大値) 型名 --- [m] 01777777777777777777777UL(unsigned long int最大値) 型名 --- [m] 2進定数・サフィックスU(u)L(l) 0b11111111111111111111111111111111UL(unsigned int最大値) 型名 --- [m] 0b1111111111111111111111111111111111111111111111111111111111111111UL(unsigned long int最大値) 型名 --- [m] 10進定数・サフィックスLL(ll) 2147483647LL(int最大値) 型名 --- [x] 9223372036854775807LL(long int最大値) 型名 --- [x] 16進定数・サフィックスLL(ll) 0x7FFFFFFFLL(int最大値) 型名 --- [x] 0x7FFFFFFFFFFFFFFFLL(long int最大値) 型名 --- [x] 0x8000000000000000LL(long int最大値+1) 型名 --- [y] 0xFFFFFFFFFFFFFFFFLL(unsigned long int最大値) 型名 --- [y] 8進定数・サフィックスLL(ll) 017777777777LL(int最大値) 型名 --- [x] 0777777777777777777777LL(long int最大値) 型名 --- [x] 01000000000000000000000LL(long int最大値+1) 型名 --- [y] 01777777777777777777777LL(unsigned long int最大値) 型名 --- [y] 2進定数・サフィックスLL(ll) 0b1111111111111111111111111111111LL(int最大値) 型名 --- [x] 0b111111111111111111111111111111111111111111111111111111111111111LL(long int最大値) 型名 --- [x] 0b1000000000000000000000000000000000000000000000000000000000000000LL(long int最大値+1) 型名 --- [y] 0b1111111111111111111111111111111111111111111111111111111111111111LL(unsigned long int最大値) 型名 --- [y] 10進定数・サフィックスU(u)LL(ll) 4294967295ULL(unsigned int最大値) 型名 --- [y] 18446744073709551615ULL(unsigned long int最大値) 型名 --- [y] 16進定数・サフィックスU(u)LL(ll) 0xFFFFFFFFULL(unsigned int最大値) 型名 --- [y] 0xFFFFFFFFFFFFFFFFULL(unsigned long int最大値) 型名 --- [y] 8進定数・サフィックスU(u)LL(ll) 037777777777ULL(unsigned int最大値) 型名 --- [y] 01777777777777777777777ULL(unsigned long int最大値) 型名 --- [y] 2進定数・サフィックスU(u)LL(ll) 0b11111111111111111111111111111111ULL(unsigned int最大値) 型名 --- [y] 0b1111111111111111111111111111111111111111111111111111111111111111ULL(unsigned long int最大値) 型名 --- [y]

文字定数の出力テスト


sample.cpp
#include <iostream> #include <iomanip> int main() { std::cout << "文字定数\n" << std::endl; std::cout << std::uppercase << std::hex; std::cout << "「" << 'A' << "」 --- 文字コード = " << static_cast<int>('A') << std::endl; std::cout << "「" << 'z' << "」 --- 文字コード = " << static_cast<int>('z') << std::endl; std::cout << "「" << '{' << "」 --- 文字コード = " << static_cast<int>('{') << std::endl; std::cout << std::endl; std::cout << "文字コードリスト" << std::endl; for (int c1=0x2; c1<0x8; c1++) { std::cout << std::setw(2) << std::setfill('0') << c1*0x10 << " "; for (int c2=0; c2<0x10; c2++) { std::cout << static_cast<char>(c1*0x10+c2) << " "; } std::cout << std::endl; } std::cout << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 文字定数 「A」 --- 文字コード = 41 「z」 --- 文字コード = 7A 「{」 --- 文字コード = 7B 文字コードリスト 20 ! " # $ % & ' ( ) * + , - . / 30 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 40 @ A B C D E F G H I J K L M N O 50 P Q R S T U V W X Y Z [ \ ] ^ _ 60 ` a b c d e f g h i j k l m n o 70 p q r s t u v w x y z { | } ~

文字定数(ワイド文字)の出力テスト


sample.cpp
#include <iostream> #include <iomanip> #include <codecvt> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << "文字定数(ワイド文字)\n" << std::endl; // 文字定数によるワイド文字型の初期化 wchar_t wct = L'\x3041'; char16_t c16 = u'\x3042'; char32_t c32 = U'\x3043'; // 各ワイド文字型のサイズ std::cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << std::endl; std::cout << "sizeof(char16_t) = " << sizeof(char16_t) << std::endl; std::cout << "sizeof(char32_t) = " << sizeof(char32_t) << std::endl; // 格納されているワイド文字の表示 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter1; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter2; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter3; std::cout << "wchar_t : " << converter1.to_bytes(wct) << std::endl; std::cout << "char16_t : " << converter2.to_bytes(c16) << std::endl; std::cout << "char32_t : " << converter3.to_bytes(c32) << std::endl; // 格納されているワイド文字のバイト並び byteseq(reinterpret_cast<unsigned char *>(&wct), sizeof(wchar_t), "wchar_t"); byteseq(reinterpret_cast<unsigned char *>(&c16), sizeof(char16_t), "char16_t"); byteseq(reinterpret_cast<unsigned char *>(&c32), sizeof(char32_t), "char32_t"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 文字定数(ワイド文字) sizeof(wchar_t) = 4 sizeof(char16_t) = 2 sizeof(char32_t) = 4 wchar_t : char16_t : char32_t : wchar_t = 41 30 00 00 char16_t = 42 30 char32_t = 43 30 00 00

文字定数(エスケープシーケンス)の出力テスト


sample.cpp
#include <iostream> int main() { std::cout << "エスケープシーケンス\n" << std::endl; char c01 = '\''; /* 一重引用符 */ char c02 = '\"'; /* 二重引用符 */ char c03 = '\?'; /* 疑問符 */ char c04 = '\\'; /* バックスラッシュ */ char c05 = '\a'; /* アラート(ベル) */ char c06 = '\b'; /* 後退(バックスペース) */ char c07 = '\f'; /* 改ページ */ char c08 = '\n'; /* 改行 */ char c09 = '\r'; /* 復帰 */ char c10 = '\t'; /* 水平タブ */ char c11 = '\v'; /* 垂直タブ */ char c12 = '\101'; /* 8進整数文字定数 */ char c13 = '\x41'; /* 16進整数文字定数 */ std::cout << "一重引用符 「" << c01 << "」" << std::endl; std::cout << "二重引用符 「" << c02 << "」" << std::endl; std::cout << "疑問符 「" << c03 << "」" << std::endl; std::cout << "バックスラッシュ 「" << c04 << "」" << std::endl; std::cout << "アラート >>> [Enterキー]" << std::endl; std::cin.get(); std::cout << c05 << std::endl; std::cout << "後退(バックスペース) 「a" << c06 << "」" << std::endl; std::cout << "改ページ 「" << c07 << "」" << std::endl; std::cout << "改行 「" << c08 << "」" << std::endl; std::cout << "復帰 「" << c09 << "」" << std::endl; std::cout << "水平タブ(2連) 「" << c10 << c10 << "」" << std::endl; std::cout << "垂直タブ(2連) 「" << c11 << c11 << "」" << std::endl; std::cout << "8進整数文字定数 「" << c12 << "」" << std::endl; std::cout << "16進整数文字定数 「" << c13 << "」" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out エスケープシーケンス 一重引用符 「'」 二重引用符 「"」 疑問符 「?」 バックスラッシュ 「\」 アラート >>> [Enterキー] <--- Enterキー押し下げでベル(画面フラッシュ)発生 後退(バックスペース) 「」 <--- 1文字手前の「a」を削除 改ページ 「 」 改行 「 」 <--- 出力位置が行頭に移動 水平タブ(2連) 「 」 垂直タブ(2連) 「 」 8進整数文字定数 「A」 16進整数文字定数 「A」

浮動小数点定数


sample.cpp
#include <iostream> #include <cmath> #include <iomanip> int main() { std::cout << "浮動小数点定数\n" << std::endl; double samp_d1 = 123.123; /* 10進浮動小数点定数 */ double samp_d2 = 123.; /* 10進浮動小数点定数(少数部無し) */ double samp_d3 = .123; /* 10進浮動小数点定数(整数部無し) */ double samp_d4 = 1.23e2; /* 10進浮動小数点定数(指数表記) */ double samp_d5 = 1.23E-2; /* 10進浮動小数点定数(指数表記) */ /* 16進浮動小数点定数 */ double samp_d6a = 0xA.8p0; double samp_d6b = (10.0 + 8.0/16.0) * std::pow(2.0, 0.0); double samp_d7a = 0xAF.CDp2; double samp_d7b = (10.0 * 16.0 + 15.0 + 12.0/16 + 13.0/256.0) * pow(2.0, 2.0); std::cout << "10進浮動小数点定数 " << std::scientific << samp_d1 << std::endl; std::cout << "10進浮動小数点定数(少数部無し) " << std::scientific << samp_d2 << std::endl; std::cout << "10進浮動小数点定数(整数部無し) " << std::scientific << samp_d3 << std::endl; std::cout << "10進浮動小数点定数(指数表記) " << std::scientific << samp_d4 << std::endl; std::cout << "10進浮動小数点定数(指数表記) " << std::scientific << std::uppercase << samp_d5 << std::endl; std::cout << "16進浮動小数点定数 " << std::scientific << std::uppercase << samp_d6a << " " << samp_d6b << std::endl; std::cout << "16進浮動小数点定数 " << std::scientific << std::uppercase << samp_d7a << " " << samp_d7b << std::endl; std::cout << "\n明示的に定数の型を示すサフィックス" << std::endl; std::cout << "float" << std::endl; std::cout << " f または F" << std::endl; std::cout << " " << std::scientific << std::setprecision(10) << 1.2345678901F << "(1.2345678901F) " << 1.2345678901 << "(1.2345678901)" << std::endl; std::cout << "long double" << std::endl; std::cout << " l または L" << std::endl; std::cout << " " << std::scientific << std::setprecision(17) << 1.23456789012345678L << "(1.23456789012345678L) " << 1.23456789012345678 << "(1.23456789012345678)" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 浮動小数点定数 10進浮動小数点定数 1.231230e+02 10進浮動小数点定数(少数部無し) 1.230000e+02 10進浮動小数点定数(整数部無し) 1.230000e-01 10進浮動小数点定数(指数表記) 1.230000e+02 10進浮動小数点定数(指数表記) 1.230000E-02 16進浮動小数点定数 1.050000E+01 1.050000E+01 16進浮動小数点定数 7.032031E+02 7.032031E+02 明示的に定数の型を示すサフィックス float f または F 1.2345678806E+00(1.2345678901F) 1.2345678901E+00(1.2345678901) long double l または L 1.23456789012345678E+00(1.23456789012345678L) 1.23456789012345669E+00(1.23456789012345678)

文字列リテラル(プレフィックス無し、UTF-8)


sample.cpp
#include <iostream> #include <iomanip> // バイト列表示用関数 void byteseq(const unsigned char *cp, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; int i = 0; while (*(cp + i) != '\0') { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i++]) << " "; } std::cout << std::endl; } int main() { std::cout << "文字列リテラル" << std::endl; // 文字列リテラル const char samp1[] = "あいうえお"; // 文字列リテラル(接頭辞u8 : UTF-8) const char samp2[] = u8"あいうえお"; std::cout << "samp1 = " << samp1 << std::endl; std::cout << "samp2 = " << samp2 << std::endl; byteseq(reinterpret_cast<const unsigned char *>(samp1), "接頭辞無し"); byteseq(reinterpret_cast<const unsigned char *>(samp2), "接頭辞(u8)"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 文字列リテラル samp1 = あいうえお samp2 = あいうえお 接頭辞無し = E3 81 82 E3 81 84 E3 81 86 E3 81 88 E3 81 8A 接頭辞(u8) = E3 81 82 E3 81 84 E3 81 86 E3 81 88 E3 81 8A

文字列リテラル(ワイド文字)


sample.cpp
#include <iostream> #include <iomanip> #include <codecvt> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << "文字列リテラル(ワイド文字)" << std::endl; // 文字列リテラル(wchar_t) const wchar_t wct[] = L"あいうえお"; // 文字列リテラル(char16_t) const char16_t c16[] = u"あいうえお"; // 文字列リテラル(char32_t) const char32_t c32[] = U"あいうえお"; std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter1; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter2; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter3; std::cout << "wct = " << converter1.to_bytes(wct) << std::endl; std::cout << "c16 = " << converter2.to_bytes(c16) << std::endl; std::cout << "c32 = " << converter3.to_bytes(c32) << std::endl; byteseq(reinterpret_cast<const unsigned char *>(wct), 5 * sizeof(wchar_t), "wchar_t"); byteseq(reinterpret_cast<const unsigned char *>(c16), 5 * sizeof(char16_t), "char16_t"); byteseq(reinterpret_cast<const unsigned char *>(c32), 5 * sizeof(char32_t), "char32_t"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 文字列リテラル(ワイド文字) wct = あいうえお c16 = あいうえお c32 = あいうえお wchar_t = 42 30 00 00 44 30 00 00 46 30 00 00 48 30 00 00 4A 30 00 00 char16_t = 42 30 44 30 46 30 48 30 4A 30 <--- char16_t型は2バイト単位 char32_t = 42 30 00 00 44 30 00 00 46 30 00 00 48 30 00 00 4A 30 00 00

文字列リテラルの連結


sample.cpp
#include <iostream> #include <iomanip> #include <codecvt> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << "文字列リテラルの連結\n" << std::endl; // シングルバイト文字列 const char samp1[] = "ab"; // シングルバイト文字列 シングルバイト文字列 const char samp2[] = "ab" "cd"; // ワイド文字列 const wchar_t samp3[] = L"ab"; // シングルバイト文字 ワイド文字列(wchar_t) const wchar_t samp4[] = "ab" L"cd"; // ワイド文字列(wchar_t) ワイド文字列(wchar_t) const wchar_t samp5[] = L"ab" L"cd"; // シングルバイト文字 ワイド文字列(char16_t) const char16_t samp6[] = "ab" u"cd"; // ワイド文字列(char16_t) ワイド文字列(char16_t) const char16_t samp7[] = u"ab" u"cd"; // シングルバイト文字 ワイド文字列(char32_t) const char32_t samp8[] = "ab" U"cd"; // ワイド文字列(char32_t) ワイド文字列(char32_t) const char32_t samp9[] = U"ab" U"cd"; // シングルバイト文字列 シングルバイト文字列 シングルバイト文字列 const char samp10[] = "ab" "cd" "ef"; // ワイド文字列(wchar_t) シングルバイト文字列 ワイド文字列(wchar_t) const wchar_t samp11[] = L"ab" "cd" "ef"; std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv_wct; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv_c16; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv_c32; std::cout << "samp1 = " << samp1 << std::endl; std::cout << "samp2 = " << samp2 << std::endl; std::cout << "samp3 = " << conv_wct.to_bytes(samp3) << std::endl; std::cout << "samp4 = " << conv_wct.to_bytes(samp4) << std::endl; std::cout << "samp5 = " << conv_wct.to_bytes(samp5) << std::endl; std::cout << "samp6 = " << conv_c16.to_bytes(samp6) << std::endl; std::cout << "samp7 = " << conv_c16.to_bytes(samp7) << std::endl; std::cout << "samp8 = " << conv_c32.to_bytes(samp8) << std::endl; std::cout << "samp9 = " << conv_c32.to_bytes(samp9) << std::endl; std::cout << "samp10 = " << samp10 << std::endl; std::cout << "samp11 = " << conv_wct.to_bytes(samp11) << std::endl; std::cout << std::endl; byteseq(reinterpret_cast<const unsigned char *>(samp1), 2 * sizeof(char), "samp1"); byteseq(reinterpret_cast<const unsigned char *>(samp2), 4 * sizeof(char), "samp2"); byteseq(reinterpret_cast<const unsigned char *>(samp3), 2 * sizeof(wchar_t), "samp3"); byteseq(reinterpret_cast<const unsigned char *>(samp4), 4 * sizeof(wchar_t), "samp4"); byteseq(reinterpret_cast<const unsigned char *>(samp5), 4 * sizeof(wchar_t), "samp5"); byteseq(reinterpret_cast<const unsigned char *>(samp6), 4 * sizeof(char16_t), "samp6"); byteseq(reinterpret_cast<const unsigned char *>(samp7), 4 * sizeof(char16_t), "samp7"); byteseq(reinterpret_cast<const unsigned char *>(samp8), 4 * sizeof(char32_t), "samp8"); byteseq(reinterpret_cast<const unsigned char *>(samp9), 4 * sizeof(char32_t), "samp9"); byteseq(reinterpret_cast<const unsigned char *>(samp10), 6 * sizeof(char), "samp10"); byteseq(reinterpret_cast<const unsigned char *>(samp11), 6 * sizeof(wchar_t), "samp11"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 文字列リテラルの連結 samp1 = ab samp2 = abcd samp3 = ab samp4 = abcd samp5 = abcd samp6 = abcd samp7 = abcd samp8 = abcd samp9 = abcd samp10 = abcdef samp11 = abcdef samp1 = 61 62 samp2 = 61 62 63 64 samp3 = 61 00 00 00 62 00 00 00 samp4 = 61 00 00 00 62 00 00 00 63 00 00 00 64 00 00 00 samp5 = 61 00 00 00 62 00 00 00 63 00 00 00 64 00 00 00 samp6 = 61 00 62 00 63 00 64 00 samp7 = 61 00 62 00 63 00 64 00 samp8 = 61 00 00 00 62 00 00 00 63 00 00 00 64 00 00 00 samp9 = 61 00 00 00 62 00 00 00 63 00 00 00 64 00 00 00 samp10 = 61 62 63 64 65 66 samp11 = 61 00 00 00 62 00 00 00 63 00 00 00 64 00 00 00 65 00 00 00 66 00 00 00

raw-stringリテラル


sample.cpp
#include <iostream> #include <iomanip> #include <codecvt> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << "raw-stringリテラル\n" << std::endl; // 区切り文字列(d-char-sequence)無し const char samp1[] = R"("a ぁあ")"; // 区切り文字列(d-char-sequence)有り(1) const char samp2[] = R"d-char("a ぁあ")d-char"; // 区切り文字列(d-char-sequence)有り(2) const char samp3[] = R"+++("a ぁあ")+++"; // 接頭辞が「u8」(UTF-8) const char samp4[] = u8R"+++("a ぁあ")+++"; // 接頭辞が「L」(wchar_t) const wchar_t samp5[] = LR"+++("a ぁあ")+++"; // 接頭辞が「u」(char16_t) const char16_t samp6[] = uR"+++("a ぁあ")+++"; // 接頭辞が「U」(char32_t) const char32_t samp7[] = UR"+++("a ぁあ")+++"; std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv_wct; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv_c16; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv_c32; std::cout << R"--("samp1 R"(...)" = )--" << samp1 << std::endl; std::cout << R"--("samp2 R"d-char(...)d-char" = )--" << samp2 << std::endl; std::cout << R"--("samp3 R"+++(...)+++" = )--" << samp3 << std::endl; std::cout << R"--("samp4 u8R"+++(...)+++" = )--" << samp4 << std::endl; std::cout << R"--("samp5 LR"+++(...)+++" = )--" << conv_wct.to_bytes(samp5) << std::endl; std::cout << R"--("samp6 uR"+++(...)+++" = )--" << conv_c16.to_bytes(samp6) << std::endl; std::cout << R"--("samp7 UR"+++(...)+++" = )--" << conv_c32.to_bytes(samp7) << std::endl; byteseq(reinterpret_cast<const unsigned char *>(samp1), 11 * sizeof(char), "samp1"); byteseq(reinterpret_cast<const unsigned char *>(samp2), 11 * sizeof(char), "samp2"); byteseq(reinterpret_cast<const unsigned char *>(samp3), 11 * sizeof(char), "samp3"); byteseq(reinterpret_cast<const unsigned char *>(samp4), 11 * sizeof(char), "samp4"); byteseq(reinterpret_cast<const unsigned char *>(samp5), 7 * sizeof(wchar_t), "samp5"); byteseq(reinterpret_cast<const unsigned char *>(samp6), 7 * sizeof(char16_t), "samp6"); byteseq(reinterpret_cast<const unsigned char *>(samp7), 7 * sizeof(char32_t), "samp7"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out raw-stringリテラル "samp1 R"(...)" = "a ぁあ" "samp2 R"d-char(...)d-char" = "a ぁあ" "samp3 R"+++(...)+++" = "a ぁあ" "samp4 u8R"+++(...)+++" = "a ぁあ" "samp5 LR"+++(...)+++" = "a ぁあ" "samp6 uR"+++(...)+++" = "a ぁあ" "samp7 UR"+++(...)+++" = "a ぁあ" samp1 = 22 61 0A E3 81 81 E3 81 82 22 00 samp2 = 22 61 0A E3 81 81 E3 81 82 22 00 samp3 = 22 61 0A E3 81 81 E3 81 82 22 00 samp4 = 22 61 0A E3 81 81 E3 81 82 22 00 samp5 = 22 00 00 00 61 00 00 00 0A 00 00 00 41 30 00 00 42 30 00 00 22 00 00 00 00 00 00 00 samp6 = 22 00 61 00 0A 00 41 30 42 30 22 00 00 00 samp7 = 22 00 00 00 61 00 00 00 0A 00 00 00 41 30 00 00 42 30 00 00 22 00 00 00 00 00 00 00

ブール値リテラル


sample.cpp
#include <iostream> #include <iomanip> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << std::uppercase << std::hex; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << "ブール値リテラル\n" << std::endl; bool samp1 = true; bool samp2 = false; std::cout << "samp1 = " << samp1 << std::endl; std::cout << "samp2 = " << samp2 << std::endl; std::cout << std::endl; byteseq(reinterpret_cast<const unsigned char *>(&samp1), sizeof(bool), "samp1"); byteseq(reinterpret_cast<const unsigned char *>(&samp2), sizeof(bool), "samp2"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out ブール値リテラル samp1 = 1 samp2 = 0 samp1 = 01 samp2 = 00

ポインタリテラル


sample.cpp
ポインタリテラル nullptr の使用
#include <iostream> #include <iomanip> // バイト列表示用関数 void byteseq(const unsigned char *cp, size_t nlen, const char *tname) { std::cout << static_cast<const void *>(cp) << " : "; std::cout << tname << " = "; for (size_t i=0; i<nlen; i++) { std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(cp[i]) << " "; } std::cout << std::endl; } int main() { std::cout << std::hex; std::cout << "ポインタリテラル\n" << std::endl; // 定数により変数を初期化 char s[] = "あ"; int n = 1; double d = 1.2; // 変数のアドレスでポインタを初期化 char *p_char = s; int *p_int = &n; double *p_double = &d; std::cout << "各変数の格納値" << std::endl; std::cout << "s = " << s << std::endl; std::cout << "n = " << n << std::endl; std::cout << "d = " << d << std::endl; std::cout << std::endl; std::cout << "各変数のアドレス" << std::endl; std::cout << "&s[0] = " << static_cast<void *>(&s[0]) << std::endl; std::cout << "&n = " << &n << std::endl; std::cout << "&d = " << &d << std::endl; std::cout << std::endl; std::cout << "ポインタに格納された変数のアドレス" << std::endl; std::cout << "p_char = " << static_cast<void *>(p_char) << std::endl; std::cout << "p_int = " << p_int << std::endl; std::cout << "p_double = " << p_double << std::endl; std::cout << std::endl; std::cout << "ポインタ自身のアドレスと格納値 : nullptr代入前" << std::endl; byteseq(reinterpret_cast<const unsigned char *>(&p_char), sizeof(p_char), "p_char "); byteseq(reinterpret_cast<const unsigned char *>(&p_int), sizeof(p_int), "p_int "); byteseq(reinterpret_cast<const unsigned char *>(&p_double), sizeof(p_double), "p_double"); std::cout << std::endl; // ポインタリテラルをポインタに代入 p_char = nullptr; p_int = nullptr; p_double = nullptr; std::cout << "ポインタ自身のアドレスと格納値 : nullptr代入後" << std::endl; byteseq(reinterpret_cast<const unsigned char *>(&p_char), sizeof(p_char), "p_char "); byteseq(reinterpret_cast<const unsigned char *>(&p_int), sizeof(p_int), "p_int "); byteseq(reinterpret_cast<const unsigned char *>(&p_double), sizeof(p_double), "p_double"); }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out ポインタリテラル 各変数の格納値 s = あ n = 1 d = 1.2 各変数のアドレス &s[0] = 0x7fff4c591d5c &n = 0x7fff4c591d58 &d = 0x7fff4c591d50 ポインタに格納された変数のアドレス p_char = 0x7fff4c591d5c p_int = 0x7fff4c591d58 p_double = 0x7fff4c591d50 ポインタ自身のアドレスと格納値 : nullptr代入前 0x7fff4c591d48 : p_char = 5c 1d 59 4c ff 7f 00 00 0x7fff4c591d40 : p_int = 58 1d 59 4c ff 7f 00 00 0x7fff4c591d38 : p_double = 50 1d 59 4c ff 7f 00 00 ポインタ自身のアドレスと格納値 : nullptr代入後 0x7fff4c591d48 : p_char = 00 00 00 00 00 00 00 00 0x7fff4c591d40 : p_int = 00 00 00 00 00 00 00 00 0x7fff4c591d38 : p_double = 00 00 00 00 00 00 00 00

実行環境

GNU bash, version 5.1.16
GCC-12.2.0
GNU C Library 2.36
GNU Binutils 2.39


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。