Introduction to C++

Basic Concepts in the C++ Programming Language

Updated: 03 September 2023

Notes from this YouTube Series

Prerequisites

In order to write some C we need to have a few things set up

  • To keep things simple just use a Linux computer, if you want to be fancy you can use WSL for Windows or a VSCode Remote Dev Linux Computer - same same
  • Install a C Compiler - you can use GCC and install it with:
1
sudo apt update
2
sudo apt install build-essential

Validate your installation with

1
gcc --version

If you’d like to use a VSCode Dev Container for that, here you go:

1
FROM mcr.microsoft.com/vscode/devcontainers/base:0-ubuntu-18.04
2
3
ENV DEBIAN_FRONTEND=noninteractive
4
RUN apt-get update \
5
&& apt-get -y install --no-install-recommends\
6
build-essential \
7
# Clean up
8
&& apt-get autoremove -y \
9
&& apt-get clean -y \
10
&& rm -rf /var/lib/apt/lists/*
11
ENV DEBIAN_FRONTEND=dialog

Hello World

Let’s create a file called hello.c with the following:

hello.c

1
#include <stdio.h>
2
3
int main()
4
{
5
printf("Hello, World!\n");
6
7
return 0;
8
}

We can compile this file using gcc hello.c which will create an a.out file, and then run the file using ./a.out

  • The main function is the entrypoint and it should return an int, 0 = OK.
  • We use #include <stdio.h> so that we can use the Standard I.O. library, we need this so we can printf
  • Each statement needs to end with a ;

Variables

To create variables you can use the following syntax will create an int:

1
int x;
2
x = 7;
3
4
int y = 5;

If we want to do a type casting we use the (mytype) before the specific variable we want to case. If we are working with ints for a calculation, without casting the result will be an int:

1
int x = 2;
2
int y = 3;
3
4
double z = y / x; // 1.0000

Versus using correct casting for at least one of the values we get the correct data type:

1
int z = (double) y / x; // 1.500

Variable Names

Variable names should be limited to:

  • A-z
  • a-z
  • 0-9
  • _

And should not start with a number

Printing

In order to print in C we can use the printf function. When printing we make use of a formatting string, such as %i to print an int or %s to print a string

1
int x = 5;
2
printf("%i", x);

Read Input

To read input we make use of the scanf function which will scan user input into a variable

1
int radius
2
3
scanf("%i", &radius)

The & is the address of function, which will allow the function to modify the radius variable, here’s an example of using scanf

1
#include <stdio.h>
2
3
int main()
4
{
5
int radius;
6
7
printf("Please enter a radius for your circle\n");
8
9
scanf("%i", &radius);
10
11
double circumference = 3.14 * 2 * radius;
12
13
printf("Your Curcumferance is: %f\n", circumference);
14
15
return 0;
16
}

In C if we make assign an int to a value that’s actualy a float or double it gets truncated to an int

If we want to read the value into a string (char[] or array in general), we don’t make use of the & operator:

1
printf("Please enter your name: ");
2
3
char name[21];
4
scanf("%s", name); // note - there is no &
5
6
printf("Hello, %s!\n", name);

When taking in a string like above we cannot receive spaces using scanf

Comments

Commenting code can be dome using // for single line comments or /* ..... */ for multi line comments

Data Types

Some of the data types in C are:

  • int - whole numbers
  • double - double prevision decimal number, usually use this
  • float - decimal number, use less memory, can explicitly define this with the f at the end of the number
  • char - denoted by a single quote

Floating Point numbers are stored using scientific notation in memory. To define a value using scientific notation we can do this:

1
int bigNum = 1.2e5;
2
double smallNum = 1.2e-2;
3
4
printf("bigNum: %i\nsmallNum: %f\n", bigNum, smallNum);

Arrays allow us to store groups of data and are identified using []. The string data type in C is based on a char[] which use double quotes when defining. It needs to be noted that the string needs a termination character which is a \0 (you don’t have to type this yourself)

Conversion Characters

For reading text into other data types we make use of conversion characters:

  • %i - int
  • %c - char
  • %s - string
  • %f - decimal
  • %e - scientific notation
  • %g - displays scientific or decimal based on the size of the exponent

We can use all of the above with the printf function, however when scanning in data we use%lf - double as scientific or decimal

Char

C uses the ASCII Character Set using 7 bits, 8 bits would be the extended character set

The char datatype stores characters (duh) and they are denoted by surrounding them in single quotes like: 'a'

To create a new character we can do something like this:

1
char myA = 'A';
2
3
printf("%c", myA);

We can also display the character as an int using %i:

1
char myA = 'A';
2
3
printf("%c", myA);

The range of char values is 0 - 127

Strings

A string is a sequence of characters and is denoted using "...". To declare a string we make use of a char[] like so:

1
char name[] = "Nabeel";

If we want to define a character array without knowing the number of characters needed in advance we need to state the length of the string. We need to make sure we give it one more character than we need as it needs a character to store the “string end” character

1
char name[11]; // note this will only store 10 characters

Boolean

Boolean data can be set using the _Bool data type

1
_Bool isFalse = 0;
2
_Bool isTrue = 1; // we can use any number other than `0` here

We can also enable the true and false keywords as well as the bool type by including stdbool.h

1
#include <stdio.h>
2
#include <stdbool.h>
3
4
int main()
5
{
6
bool isTrue = true;
7
printf("%i\n", isTrue);
8
9
return 0;
10
}

Operators

Some of the basic operators in C are:

Arithmetic

  • + add
  • - subtract
  • * multiply
  • / divide
  • & modulus
  • ++ increment
  • -- decrement

Relational

  • == equality
  • != inequality
  • > greater that
  • < less than
  • >= greater than or equal
  • <= less than or equal

Logical

  • && and
  • || or
  • !

Assignment

  • = assign
  • += add and assign
  • -= subtract and assign
  • *= multiply and assign
  • /= divide and assign
  • %= mod and assign

And then a bunch of bitwise ones that’ I’ll likely never use