@@ -129,7 +129,7 @@ Hello world
129129- ` Variable ` : an object has a name(identifier)
130130- ` Initialization ` : provides an initial value for a variable.
131131
132- 1 . ** Initialization**
132+ ### 4. 1. Initialization
133133``` cpp
134134// 1. default
135135int a;
@@ -167,7 +167,7 @@ Foo f4(3.14); // OK direct init works with explicit → calls Foo(double)
167167```
168168
169169
170- 2. ** iostream**
170+ ### 4. 2. iostream
171171- `std::cin >> x`: console input to x
172172- `std::cout << x`: console output
173173- `\n` vs `std::endl`:
@@ -194,17 +194,17 @@ int main() {
194194```
195195
196196
197- 3 . ** Keywords and identifiers**
197+ ### 4. 3. Keywords and identifiers
198198- Keywords: alignas alignof and and_eq asm auto bitand bitor bool break case catch char char8_t (since C++20) char16_t char32_t class compl concept (since C++20) const consteval (since C++20) constexpr constinit (since C++20) const_cast continue co_await (since C++20) co_return (since C++20) co_yield (since C++20) decltype default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new noexcept not not_eq nullptr operator or or_eq private protected public register reinterpret_cast requires (since C++20) return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq
199199
200200- Identifiers: ` snake_case ` vs ` camelCase `
201201> When working in an existing program, use the conventions of that program. Use modern best practices when writing new programs.
202202
203- 3 . ** Literals vs Operators**
203+ ### 4.4 . Literals vs Operators
204204- `Literals`: fixed values like 42 , 3.14 , ' A' , " Hello" , true , nullptr .
205205- `Operators`: symbols that act on values (+ - * / %, == != < >, && || !, = += -=, etc.).
206206
207- 4 . ** Expression**
207+ ### 4.5. Expression
208208- An expression is anything that produces a value.
209209``` cpp
2102105 + 3 // expression → evaluates to 8
@@ -214,7 +214,7 @@ true && flag // logical expression
214214```
215215
216216## 5. C++ Basic: Functions and Files
217- 1. ** Function**
217+ ### 5. 1. Function
218218- Syntax:
219219```cpp
220220returnType functionName(); // forward declaration
@@ -229,7 +229,7 @@ returnType functionName() // This is the function header (tells the compiler abo
229229- A ` forward declaration ` allows us to tell the compiler about the existence of an identifier before actually defining the identifier.
230230class B;
231231
232- 2 . ** name space**
232+ ### 5. 2. name space
233233- ` namespace ` is a way to group names (variables, functions, classes) together and avoid name conflicts. It guarantees that all identifiers within the namespace are unique
234234``` cpp
235235// Issue
@@ -253,7 +253,7 @@ int main() {
253253
254254- `using namespace`: this is a using-directive that allows us to access names in the std namespace with no namespace prefix
255255
256- 3. ** Preprocessor**
256+ ### 5. 3. Preprocessor
257257- The preprocessor is a process that runs on the code before it is compiled.
258258```cpp
259259#include <iostream> // insert file contents
@@ -264,7 +264,7 @@ std::cout << NAME;
264264#endif
265265```
266266
267- 4 . ** Header files**
267+ ### 5. 4. Header files
268268- Header files are files designed to propagate declarations to code files.
269269- Include header files:
270270``` cpp
@@ -288,10 +288,166 @@ g++ -o main -I./source/includes -I/home/abc/moreHeaders main.cpp
288288#include " myOtherHeader.h"
289289```
290290
291- 5 . ** Header guard**
291+ ### 5.5. Header guard
292292- Header guards prevent the contents of a header from being included more than once into a given code file.
293293- For cross-platform library code, ` #ifndef ` is safest.
294294- For modern projects using GCC/Clang/MSVC, ` #pragma once ` is simpler and safe.
295295
296- 6 . ** Others**
297- ` https://www.learncpp.com/cpp-tutorial/how-to-design-your-first-programs/ `
296+ ### 5.6. Others
297+ ` https://www.learncpp.com/cpp-tutorial/how-to-design-your-first-programs/ `
298+
299+
300+ ## 6. Debugging C++ Programs
301+
302+ ## 7. Fundamental Data Types
303+ - Memory can only store bits. Data type help compiler and CPU take care of encoding the value into the sequence of bits.
304+
305+ ### 7.1. Basic datatype (Primitive type)
306+
307+ | Types | Category | Meaning | Example |
308+ | ----------------------------------------------------------------------| ------------------------| ----------------------------------------------| -----------|
309+ | float, double, long double | Floating Point | a number with a fractional part | 3.14159 |
310+ | bool | Integral (Boolean) | true or false | true |
311+ | char, wchar_t, char8_t (C++20), char16_t (C++11), char32_t (C++11) | Integral (Character) | a single character of text | 'c' |
312+ | short int, int, long int, long long int (C++11) | Integral (Integer) | positive and negative whole numbers, including 0 | 64 |
313+ | std::nullptr_t (C++11) | Null Pointer | a null pointer | nullptr |
314+ | void | Void | no type | n/a |
315+
316+ ### 7.2. Sizeof
317+ - We can use ` sizeof ` can be used to return the ` size of a type in bytes ` .
318+
319+ | Category | Type | Minimum Size | Typical Size |
320+ | -----------------| ---------------| --------------------| --------------------|
321+ | Boolean | bool | 1 byte | 1 byte |
322+ | Character | char | 1 byte (exactly) | 1 byte |
323+ | Character | wchar_t | 1 byte | 2 or 4 bytes |
324+ | Character | char8_t | 1 byte | 1 byte |
325+ | Character | char16_t | 2 bytes | 2 bytes |
326+ | Character | char32_t | 4 bytes | 4 bytes |
327+ | Integral | short | 2 bytes | 2 bytes |
328+ | Integral | int | 2 bytes | 4 bytes |
329+ | Integral | long | 4 bytes | 4 or 8 bytes |
330+ | Integral | long long | 8 bytes | 8 bytes |
331+ | Floating point | float | 4 bytes | 4 bytes |
332+ | Floating point | double | 8 bytes | 8 bytes |
333+ | Floating point | long double | 8 bytes | 8, 12, or 16 bytes |
334+ | Pointer | std::nullptr_t| 4 bytes | 4 or 8 bytes |
335+
336+ ### 7.3. Signed/ Unsigned
337+ - a signed integer can hold both positive and negative numbers (and 0).
338+ - Unsigned integers are integers that can only hold non-negative whole numbers.
339+
340+ ### 7.4. Fixed-width integers and size_t
341+ - ` fixed-width integers ` : C++11 provides an alternate set of integer types that are guaranteed to be the same size on any architecture
342+ ``` cpp
343+ #include < cstdint>
344+ // Fixed-width integer types
345+
346+ std::int8_t i8 ; // 1 byte signed range: -128 to 127
347+ std::uint8_t u8 ; // 1 byte unsigned range: 0 to 255
348+
349+ std::int16_t i16 ; // 2 bytes signed range: -32,768 to 32,767
350+ std::uint16_t u16 ; // 2 bytes unsigned range: 0 to 65,535
351+
352+ std::int32_t i32 ; // 4 bytes signed range: -2,147,483,648 to 2,147,483,647
353+ std::uint32_t u32 ; // 4 bytes unsigned range: 0 to 4,294,967,295
354+
355+ std::int64_t i64 ; // 8 bytes signed range: -9,223,372,036,854,775,808
356+ // to 9,223,372,036,854,775,807
357+ std::uint64_t u64 ; // 8 bytes unsigned range: 0 to 18,446,744,073,709,551,615
358+ ```
359+
360+ - ` fast integers ` : guarantee at least # bits, but pick the type that the CPU can process fastest (even if it uses more memory).
361+ - ` least integers ` : guarantee at least # bits, but pick the type that uses the least memory (even if it’s slower).
362+ ``` cpp
363+ #include < cstdint> // for fast and least types
364+ #include < iostream>
365+
366+ int main ()
367+ {
368+ std::cout << "least 8: " << sizeof(std::int_least8_t) * 8 << " bits\n";
369+ std::cout << "least 16: " << sizeof(std::int_least16_t) * 8 << " bits\n";
370+ std::cout << "least 32: " << sizeof(std::int_least32_t) * 8 << " bits\n";
371+ std::cout << '\n';
372+ std::cout << "fast 8: " << sizeof(std::int_fast8_t) * 8 << " bits\n";
373+ std::cout << "fast 16: " << sizeof(std::int_fast16_t) * 8 << " bits\n";
374+ std::cout << "fast 32: " << sizeof(std::int_fast32_t) * 8 << " bits\n";
375+
376+ return 0;
377+ }
378+ ```
379+
380+ ### 7.5. std::size_t
381+ - The type returned by the ` sizeof ` .
382+ - size_t is an unsigned integral type that is used to represent the size or length of objects.
383+
384+ ``` cpp
385+ #include < cstddef> // for std::size_t
386+ #include < iostream>
387+
388+ int main ()
389+ {
390+ std::cout << sizeof(std::size_t) << '\n';
391+
392+ return 0;
393+ }
394+ ```
395+
396+ ### 7.6. Scientific notation
397+ - ` e ` /` E ` : to represent the “times 10 to the power of” part of the equation. (e.g. 5.9722 x 10²⁴ -> 5.9722e24)
398+
399+ ### 7.7. Floating point number
400+ - ` std::cout ` has a default precision of 6
401+ - We can override the default precision that std::cout shows by using an output manipulator function named ` std::setprecision() `
402+
403+ - ` f ` suffix means float
404+ - ` rounding error ` : cannot be represented exactly in binary floating-point, so printing with high precision reveals a tiny rounding error.
405+ ``` cpp
406+ #include < iomanip> // for std::setprecision()
407+ #include < iostream>
408+
409+ int main ()
410+ {
411+ double d{0.1};
412+ std::cout << d << '\n'; // use default cout precision of 6 -> 0.1
413+ std::cout << std::setprecision(17); // -> 0.10000000000000001
414+ std::cout << d << '\n';
415+
416+ return 0;
417+ }
418+ ```
419+
420+ - ` Inf ` : which represents infinity. Inf is signed, and can be positive (+Inf) or negative (-Inf). (5/0)
421+ - ` NaN ` : which stands for “Not a Number”. (mathematically invalid)
422+
423+ ### 7.8. Boolean values
424+ - ` std::boolalpha ` : use to print true or false (and allow std::cin to accept the words false and true as inputs)
425+
426+ ### 7.9. Chars
427+ - The integer stored by a ` char ` variable are intepreted as an ` ASCII character ` .
428+ - ` std::cin.get() ` this function does not ignore leading whitespace
429+ - ` Escape sequences ` :
430+
431+ | Name | Symbol | Meaning |
432+ | -----------------| ------------| ----------------------------------------------------------|
433+ | Alert | ` \a ` | Makes an alert, such as a beep |
434+ | Backspace | ` \b ` | Moves the cursor back one space |
435+ | Formfeed | ` \f ` | Moves the cursor to next logical page |
436+ | Newline | ` \n ` | Moves cursor to next line |
437+ | Carriage return | ` \r ` | Moves cursor to beginning of line |
438+ | Horizontal tab | ` \t ` | Prints a horizontal tab |
439+ | Vertical tab | ` \v ` | Prints a vertical tab |
440+ | Single quote | ` \' ` | Prints a single quote |
441+ | Double quote | ` \" ` | Prints a double quote |
442+ | Backslash | ` \\ ` | Prints a backslash |
443+ | Question mark | ` \? ` | Prints a question mark * (no longer relevant)* |
444+ | Octal number | ` \{number} ` | Translates into char represented by octal |
445+ | Hex number | ` \x{number} ` | Translates into char represented by hex number |
446+
447+
448+ - ` 't' ` : Text between single quotes is treated as a char literal, which represents a single character.
449+ - ` "text" ` : Text between double quotes (e.g. “Hello, world!”) is treated as a C-style string literal, which can contain multiple characters.
450+
451+ ### 7.10. Type conversion
452+ - ` implicit type conversion ` : e.g. ` double d { 5 }; ` // okay: int to double is safe
453+ - ` explicit type conversion ` : ` static_cast<new_type>(expression) `
0 commit comments