Lucas Rufkahr Home Notes Fun

C++ Notes

ToC


Basics

I. Introduction

A C++ program generally consists of preprocessor directives and the main function.

A preprocessor directive tells the C++ preprocessor what to do before compiling. You can use this to include files, create macros, and determine compiling based on conditions. For example, include a file using #include This will include the iostream file from the C++ standard library.

The main function looks like:

int main() {
		std::cout << "Hello world" << '\n';
		return 0;
	}
The int main() { is the declaration of the main function denoting it returns type int. return 0; is the return statement for the function. It is returning the integer 0. This tells the operating system, OK Quit the program safely.

II. Variables

A variable lets you allocate some memory in the computer and use it to store values. You can also recall the values for later. Variables contain a memory address and an identifier. An identifier can be *almost* anything you wish it to be.

Variables naming rules:

All variables have denoted types and they refer to what kind of variable it is. All variables also have a bit size and this denotes the range of values that can be stored.

Variable types:
Variables are always declared before they are initialized. This means you must determine the type and the identifier before you store any values into it.

int main() {
		int a; // Declaration
		a = 1; // Initialization
		int b = 2; // Declaration then initialization
	}


Constant types are variables identified by const in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.

Operators

Control Flow