Cracking C++ (3): Literal, Variable

lamoon·2025년 7월 4일

Cracking C++

목록 보기
3/8

Literal

  • Used to write numbers or strings in code.

Types of literal

  • Decimal / Octal / Hexadecimal / Binary literal: (123, 0173, 0x7B, 0b1111011)
  • Floating-point, double floating-point value: (3.14f, 3.14)
  • Hexadecimal floating-point literal: (0x3.ABCp-10)
  • single character: ('a')
  • zero-terminated array of chars: ("character arr") ('\0' is automatically added at the end of the array)

Digit separator can be used in numeric literals (floating points and numbers) : 23'456'789, 0.123'456f

Variables

  • can be declared anywhere in the code, and can be uninitialized: depends on the compiler (could just report error on run-time)
    • if uninitialized, it will be semi-random value based on what memory it is at the time. (Good practice to declare them; otherwise, they will cause unexpected bugs.)
  • Uniform intialization syntax: introduced in C++11 and used widely (more in later parts)
  • Strongly typed: always have specific type

Numeric

Type Size (in bytes) Range Example
signed int 4 −2³¹ to 2³¹−1 int i{-7};
unsigned int 4 0 to 2³²−1 unsigned int i{7};
signed short int 2 −2¹⁵ to 2¹⁵−1 short i{-7};
unsigned short int 2 0 to 2¹⁶−1 unsigned short i{7};
signed long int 4 or 8 −2³¹ to 2³¹−1 (4 bytes) or −2⁶³ to 2⁶³−1 (8 bytes) long i{-7};
unsigned long int 4 or 8 0 to 2³²−1 (4 bytes) or 0 to 2⁶⁴−1 (8 bytes) unsigned long i{7};
signed long long int 8 −2⁶³ to 2⁶³−1 long long i{-7};
unsigned long long int 8 0 to 2⁶⁴−1 unsigned long long i{7};
float 4 ~±3.4×10⁻³⁸ to ±3.4×10³⁸ float f{-7.5f};
double 8 ~±1.7×10⁻³⁰⁸ to ±1.7×10³⁰⁸ double d{-7.5};
long double 8, 12, or 16 Platform-dependent (typically larger than double) long double ld{-7.5};

Non-numeric

Type Size (in bytes) Range Usage Example
char 1 Typically −128 to 127 or 0 to 255 (platform-dependent) char c{'A'};
unsigned char 1 0 to 255 unsigned char c{65};
signed char 1 −128 to 127 signed char c{-65};
char8_t 1 0 to 255 char8_t c{u8'A'};
char16_t 2 0 to 2¹⁶−1 char16_t c{u'A'};
char32_t 4 0 to 2³²−1 char32_t c{U'A'};
wchar_t 2 or 4 Platform-dependent (commonly 2 bytes on Windows, 4 bytes on Linux) wchar_t wc{L'A'};
bool 1 true or false bool b{true};
  • For char, it can be signed or unsigned depending on compiler
  • In C++17, <cstddef> provides std::byte to represent byte whereas signed and unsigned were used before (e.g. now it uses std::byte b { 42 };)
  • For string, it's implemented as a STL.

Numerical Limit

  • INT_MAX used in C could be used but it's recommended to use std::numeric_limits in <limits>.

Input

cout << "\ndouble:\n"; 
cout << format("Max double value: {}\n", numeric_limits<double>::max()); 
cout << format("Min double value: {}\n", numeric_limits<double>::min()); 
cout << format("Lowest double value: {}\n", numeric_limits<double>::lowest());

Output

double:

Max double value: 1.79769e+308 
Min double value: 2.22507e-308 
Lowest double value: -1.79769e+308
  • min() equals to lowest value in int but for floating point types, it gives smallest positive value that can be represented whereas lowest() gives the most negative value representable (i.e. lowest() = -max())

Zero Initialization

  • {0} = {}: i.e. 0 here is optional and {} is called zero initializer.
    • Primitive types (char, int, etc.) sets to zero
    • Primitive floating types to 0.0
    • Pointer to nullptr
    • Constructs objects with default constructor

Casting

  • converted to other types
float MyFloat { 3.14f };
int i1 { (int)myFloat }; // NOT recommended
int i2 { int(myFloat) }; // not really used
int i3 { static_cast<int>(myFloat) }; // recommended
  • first method is not recommended since C-style cast like (int) allows almost any type to be converted to almost any other type, often without proper checks

Floating-Point Numbers

  • getting difference between two floating-point numbers will result in loss of precision.
  • +/- infinity: dividing non-zero number by zero, std::isinf() to check.
  • NaN: not a number - zero divided by zero, std::isnan() to check.
    • both under <cmath>
    • You can use numeric_limits (e.g. numeric_ limits<double>::infinity)

0개의 댓글