Often when a person first learns to program they focus on a single programming language, which is a perfectly fine way to start out, but it prevents
you from understanding the common programming paradigms that are found in almost any language. Now that I'm experience with many different languages
I'm able to see the similarities between them all, and I can read a new language I've never seen before without much effort because I understand the
basic ideas that underpin the majority of programming languages.
basic concepts apply to both languages. C++ is often described as a complex language which is hard to learn, but that isn't really a valid statement
more terse and requires variable types to be strictly defined, so it seems harder to learn.
This is why I think it's so important to start with variables when you are first learning to program. Without a proper understanding of variables you
wont be able to understand what your program is really doing. Essentially all programming languages have a very similar set of variable types, which
usually include integers, floats, chars, strings, and booleans, which are the 5 main types I will focus on in this first part of my programming guide.
var myVar1 = 123.456;
var myVar2 = 100;
var myVar3 = myVar1 + myVar2;
And some C++ code to do the same thing:
float myVar1 = 123.456;
int myVar2 = 100;
double myVar3 = myVar1 + myVar2;
Here we are declaring 3 different variables, hopefully it is obvious we are summing the 1st and 2nd variables and saving the result in the 3rd
important because if you don't know what variable types your code is really using it can lead to all sorts of bugs.
means a number with decimal places, an integer is a whole number which doesn't require decimal places. In C++ a "float" is a 32 bit floating point
number, a "double" is a 64 bit floating point number, which means it requires twice as many bits but can store more decimal places, allowing for more
precise calculations. An "int" is a 32 bit integer, and I used all 3 of these types for an important reason.
In C++ when you add an integer to a floating point number the integer will first be converted to a float, so myVar2 becomes a float, and the sum is a
float, but then we save it into a double, which is also a float but just has more bits (64 bits), so C++ converts the 32 bit float into a 64 bit float
and saves the result in myVar3. I know this may sound complicated at first glance but you will learn how these basic variables work fairly quickly.
The most important thing to understand here is probably the bits/binary which make up each variable. This is something I wish I had of understood when
I first started programming. It's crucial to understand that every variable you create is really just a bunch of binary data stored in memory. When I
first started programming I had this notion that string variables (aka text) were somehow different to number variables like integers and floats.
The reality is everything is made of bits, text is simply a series of characters, and each character is represented by a number, and those numbers are
stored as binary data. For example lets say we have a 2 bit integer, that means it could hold 4 possible values (00, 01, 10, 11), so we could use that
to represent the values 0 to 3 or perhaps -1 to 2, depending on our requirements. The same exact principle applies to 32 bit integers.
But since they have 32 bits we can store much larger numbers. Starting from 0 we can store a maximum value of 4,294,967,296 inside of an unsigned 32
bit integer. When an integer starts from zero we call it an "unsigned integer", but if we want to store negative numbers then we need a "signed
integer", at the expense of halving our range of positive values. To say an integer is "signed" means that we want the + or - sign to be stored in the
If the first bit is 0 then it's positive, if it's 1 then the number is negative. This is typically something the programming doesn't need to worry
about unless they are doing bit-level operations on a variable. You can happily add an unsigned integer with a signed integer, the signed integer will
just be converted into an unsigned integer before the addition, although your C++ compiler may give you a warning about it, and for a good reason.
If your unsigned integer contains a value outside of the range that a signed integer can hold, you will get very strange results because the
conversion will change the value of the unsigned integer. This can even happen if your integer is already holding the maximum value it can hold and
then you try to increase it even more. The value of the integer will "wrap around" back to the very lowest value it can hold which can cause all sorts
Hopefully you can now see why it's so important to understand what your variables really are and why it isn't a bad thing that C++ requires strongly
precision floats, and they can hold a very large range of values, including integer values since an integer can be stored as a float without any
A floating point number is really still made from bits just like integers, however the bits a formatted to represent decimal values. The way floats
are stored in binary is fairly complicated but isn't really important when it comes to using floats unless you want to do bit-level manipulation on
floats, which is rarely necessary. Most programming languages only allow you to manipulate the bits of an integer since it's often useful and very
fast for the CPU to perform.
We call these "bitwise operations" and almost every programming language has the same set of bitwise operators. Since all numbers are floating point
~~ floor trick
to work). If you really needed to manipulate
the bits of a float you would have to use pointer trickery to treat it as an integer, so I don't think it's possible in a language without pointers
edit on 23/4/2021 by ChaoticOrder because: (no reason given)