C++ notes

author: Paul Kim

categories: cpp

tags: cpp

Basics of C++

Structure of a program

// my first program in C++
#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
// my second program in C++
#include <iostream>

int main ()
  std::cout << "Hello World! ";
  std::cout << "I'm a C++ program";


C++ supports two ways of commenting code:

// line comment
/* block comment */
/* my second program in C++
   with more comments */

#include <iostream>

int main ()
  std::cout << "Hello World! ";     // prints Hello World!
  std::cout << "I'm a C++ program"; // prints I'm a C++ program

Using namespace std

// my second program in C++
#include <iostream>
using namespace std;

int main ()
  cout << "Hello World! ";
  cout << "I'm a C++ program";

Variables and types


A valid identifier is a sequence of one or more letters, digits, or underscore characters (_). In addition, identifiers shall always begin with a letter. They can also begin with an underscore (_), but such identifiers are (in most cases) considered reserved for compiler-specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere.

The standard reserved keywords that cannot be used for programmer created identifiers are:

alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, 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, 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

Fundamental data types

Character types

| char | Exactly one byte in size. At least 8 bits. | | char16_t | Not smaller than char. At least 16 bit. | | wchar_t | Not smaller than char16_t. At least 32 bits. |

Signed Integer types

| signed char || | signed short int || ||| ||| |||

Unsigned Integer types


© 2021 All rights reserved.