Skip to main content

C++ Basic Syntax

In this section, we will discuss some basic concepts of C and C++. We discuss both C and C++ since you will likely see both.

Builtin Numeric Types

C++ has a number of builtin integer types.

KeywordDescription
charA signed 1-byte integer. Can represent ASCII character codes. Always 1 byte.
intA signed integer. Typically 4 bytes.
floatA fractional number. Typically 4 bytes.
doubleA large fractional number. Typically 8 bytes.
int main() {
int x;
float x;
double x;
}

There are also modifiers which can be applied to these types. There are two primary types of modifiers: sign modifiers and size modifiers.

sign modifiers apply to int and char specifically.

KeywordDescription
signedThe number can be negative
unsignedThe number cannot be negative

size modifiers apply to int and double types specifically.

KeywordDescriptionApplies to doubles?
shortDecrease length of typeNo
longIncrease length of typeYes
long longIncrease length of type moreYes

Below are a few examples of the above:

int main() {
// int examples
long int a;
long long int b;
unsigned int c;
long unsigned int d;
unsigned long int e;
short int f;
short g;
long h;

// Double examples
long double g;
long long double h;
}

Sized Types

Certain types are guaranteed to have a specific size. They are included in the stdint.h header file. Having a specific size to types is frequently useful.

KeywordDescription
int8_tSigned 8-bit integer
int16_tSigned 16-bit integer
int32_tSigned 32-bit integer
int64_tSigned 64-bit integer
uint8_tUnsigned 8-bit integer
uint16_tUnsigned 16-bit integer
uint32_tUnsigned 32-bit integer
uint64_tUnsigned 64-bit integer

Examples are below:

#include <stdint.h>

int main() {
int8_t a;
int16_t b;
uint64_t c;
}

Simple Arrays

Arrays provide a way to define many instances of a single type quickly. Arrays are stored on the stack, and have limited space. Typically, an array shouldn't exceed more than 16KB of memory. This is not a hard rule, but I find it to be generally safe. Larger allocations should be made on the heap using a memory allocator, which is described later.

An example is below:

int main() {
int hello[24];

hello[0] = 0;
hello[1] = 0;
}

We create an array containing 24 ints. We then set the first two elements of the array to 0.

Arrays can also be initialized as follows:

int main() {
int hello[24] = {
0, 1, 2, 3, 4, 5, 0
};

int hello2[24] = {0};
}

The first five elements of hello are initialized to 0 through 5. Elements 6 and onwards are initialized to 0.

For hello2, all elements are initialized to 0.

Structs

structs can be used to logically group data.

For example, we can create a struct to represent a wallet. The wallet contains money (in cents), driver's license, and a health insurance card.

#include <stdint.h>

struct Wallet {
uint8_t cents_;
char license_[32];
char health_id_[32];
}; // notice the ; here

int main() {
struct Wallet wallet;
wallet.pennies_ = 200;
strcpy(wallet.license_, "dontpullmeover");
strcpy(wallet.health_id_, "donthurtme");
}

Our wallet contains 200 cents, a license with the text "dontpullmeover", and a health insure ID which states "donthurtme".

In addition, structs can be initialized using a special syntax to reduce lines of code. Here we initialize the Lemonade struct. The lemonade can have a certain amount of sugar, water, lemon juice, and coloring.

struct Lemonade {
int sugar_; // grams
int water_; // mL
int lemon_; // mL
int color_[3]; // (Red, Green, Blue)
}

int main() {
// NOTE: 255, 255, 0 is yellow on the RGB color wheel
struct Lemonade sour = {0, 100, 10,
255,255,0};
struct Lemonade sweet = {20, 100, 10,
255,255,0};
}

Memory Allocation and Pointers

Malloc + Free

The very vast majority of data must be stored using a memory allocator. The C-style way to do this is with malloc and free. Generally, it's bad practice to directly use malloc and free. However, sometimes it is unavoidable. malloc allocates memory, free releases memory. When you fail to release memory using free, it is referred to as a memory leak.

#include <cstdlib>  // malloc + free
#include <cstring> // memset

int main() {
// Allocate
int *data = (int*)malloc(64 * sizeof(int));
// Clear
memset(data, 0, 64 * sizeof(int));

// Set integer 10 to 15
data[10] = 15;

// Allocate + clear
int *data2 = (int*)calloc(64, sizeof(int));

// Set integer 10 to 15
data2[10] = 15;