C / C++

Low-level programming for high-level tryhards

Tuesday, Apr 28, 2020

C / C++

C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to make utilities running on Unix. Later, it was applied to re-implementing the kernel of the Unix operating system. During the 1980s, C gradually gained popularity. It has become one of the most widely used programming languages, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the International Organization for Standardization (ISO).


Specifying Integer Size

Sometimes you know that you’re only going to need a number that goes up to 255. It doesn’t make sense to create a full int since that is typically encoded as 4-bytes large. A single byte is capable of coding numbers up to 255.

Luckily, C allows you to specifically declare the size of your integer, with the following syntax.

Bytes Bits Signed Type Unsigned Type
1 8 int8_t uint8_t
2 16 int16_t uint16_t
4 32 int32_t uint32_t
8 64 int64_t uint64_t

You’re technically using some of these without even knowing it. Assuming you’re running C on a 64-bit system, these following types are equivalent:

GNU C Compilers

However, with this being said, gcc is a fully functional C++ compiler, and g++ is effectively just a mapping to gcc -xc++ -lstdc++ -shared-libgcc

Setting the Version


Checking Processor

gcc -march=native -Q --help=target | grep -- '-march=' | cut -f3 | head -n 1
# on macOS:        'haswell'
# on AWS EC2:      'skylake-avx512'
# on Raspberry Pi: 'cortex-a72'


Import the <stdio.h> library to get started

Formatted Strings


Initializer List

You can use Initializer Lists to zero out elements.

Initializing a 2D array with initializer lists even supports implied zero values, see below.

Useful <stdio.h> Commands

C++ Style Guide

If you develop software on a MacOS, you might want to use the style guide, which is compatible with clang's auto-formatting capabilities.

The style guide notes outlined below are taken directly from Apple’s llvm and outline some of the details I found to be the most noteworthy.