C++ syntax

C++ is very similar to the C syntax, and anyone who have coded in C will feel familiar with the language and can usually just code like they did in C since C++ has most of the features that C have but some additions like classes.

The basics of the syntax is as follows:
Lines most times stops with a semi colon.
Variable and function defenitions starts with the type.
Line comments starts with //
Block comments starts with /* and ends with */

Data types in C++

There are many datatypes you can use in C++, here is a list of some of the more common ones:

  • Bool: has only two values, true and false and usually is 8 bit but can be different size dependent on implementation.
  • Whole numbers:
    • int : is a atleast 16 bit signed integer with possible values -2147483648 to 2147483647
    • unsigned int: is a atleast 16 bit unsigned integer with possible values 0 to 4294967295
  • Floating point numbers:
    • float: is usually a 32 bit floating point number with values +/- 3.4e +/- 38 (~7 digits)
    • double: is usually a 64 bit floting point number with values +/- 1.7e +/- 308 (~15 digits)
  • Character/byte
    • char: is usually 8 bit charachter with values -127 to 127 or 0 to 255
    • unsigned char: is usually a 8 bit character equivalent to a byte with values 0 to 255

One byte is usually 8 bits.

There are also other types like long, short, long int, short int, wide char.
and there is also void which is a non value used in function returns and unknown pointers.

To find out how many bytes each datatype uses on your system you can use the following code:

#include <iostream>
using namespace std;

int main() {
    cout << "Size of bool : " << sizeof(bool) << endl;
    cout << "Size of char : " << sizeof(char) << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "Size of double : " << sizeof(double) << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

    return 0;
}

There are also datatypes defined in the standard library and also in platform libraries usually by using typedefs.

Hello World in C++

Welcome to the first tutorial on our website.

We will start with a simple Hello World application in C++, we are using Visual Studio 2017 but this code will also work with any other C++ compiler.

#include <iostream>

int main()
{
  std::puts("Hello World!");
  std::cin.get();
  return 0;
}

Lets break the code down:

#include <iostream>

This includes the C++ headers for console input and output, we need this for std::puts for output and std::cin, i am using puts instead of cout, the reason for this is that puts is faster if you putput a lot of text but for general use cout is good enough.

int main()

This is the main function for the program and tells the compiler where the program should start execution, it is requires but can have different name such as WinMain for a windows gui program, it should always return an integer to tell the operation system if the program has run successfully.

std::puts("Hello World!");
Here is the line of code that outputs the text to the console.
The std part tells the compiler that we are using the C++ standard library, the puts part is the actual function that does the output, the parantheses is the function parameters and the "Hello World!" is the string. We can also use std::cout instead and it looks like this:
std::cout << "Hello World!\n"

As you can see cout uses << which is a stream operator and streams the string into cout, you can also output other types of data directly to cout but as noted earlier, this causes some slight overhead so be carefull if you output a lot of text, for small programs this is not important.

std::cin.get();
This is the input function the get input from the user, we have not specified a parameter here so the function only waits for the user to press ENTER, if you want input from the user you pass a string into the get function like this:
std::string input;
std::getline(std::cin, input);
Just remember to add the string header in the top of your code:
#include <string>

return 0;
Finally we tell the operating system that our program har run successfully.

You can find this code complete with a visual studio project on GitHub

We have added GitHub

All our tutorials and samples is now located on GitHub.com, so you can easily find and download our course material.

We will strive to keep our repos up to date and complete with all the code we add on our website and in our tutorials.

The new website has launched

We have launched our new website with our C++ tutorials and guides, we will over the next few months add our material and code.

The subject we will cover is c++ basics, filehandling, database, i/o, sockets, webserver, webclients, windows gui, gui, some c backwards compability, dlls / dynamically linked libraries, libraries and more.