c/c++ 01 – Hello World

This is the first tutorial in the c/c++ crash course. This being my first tutorial please bare with me. I’m running on the assumption you already have a an IDE (Integrated Development Environment) set up like Code::Blocks, Visual Studio, Xamarin or go old school with the most basic of text editors and a command line compiler. They all work just fine.

Yup you guessed it the “Hello World” project. Not very exciting but it gets your foot in the door.

Code:

#include <iostream>		// Basic In/Out Header file gives us access to cout and cin

// int is return type and main is the function name () is the non existing parameter list
int main()
{
    std::cout << "Hello World!" << std::endl;	// Hello World
    std::cin.get();				// Wait so we can read the screen
    return 0;
}

Now theres not much there but ill break each line down one at a time.

#include <iostream>

This line includes the c++ standard In/Out stream functionality, like cout and cin. The # tells the compiler that this is a preprocessor directive. Meaning this line is to be don before all other lines (More on these in a later tutorial).

int main()

The main function of our program.  This is going to be the first block of code run on any c or c++ application. The retrun type is set to int (for integer dont worry well get to types in the next tutoria). This retrun value can be used to inform the user of successful ( or not ) termination of out application.

std::cout << "Hello World!" << std::endl;

This line is where all the magic happens in our program. A few things i would like to tuch on here are “std::” and the “<<“.
std is a name space where the standard library holds the functions from <istream>. In order to access them we need to tell the compiler/linker what namespace the functions are located in.
<<  is our first operator. In this case it tells cout to print the message “Hello World!” onto the console window.

std::cin.get();

I added this line only so that the console will stay open on in a window until enter was pressed.

return 0;

The return value telling the the system that the program exited normally.

 

c/c++ 02 – Data Types

C/C++ Crash Course – Data types

Primitive data types in c++ are the variables we store the data for our program. They come in many different flavors and there’s a few key notes to make in this section. Without going in to too much detail about twos-compliment. The first and foremost question is the data going need to be negative in value. If is does then your gonna want to use a signed variable and if the data is only ever going to start at 0 and go up you can use that extra signed bit to store bigger numbers.  Here’s a quick block of code showing how to initialize the primitive data types and print out their values and width in bytes (8 bits per byte) to the console.

#include <iostream>
#include <iomanip>

using namespace std;// Allows us to easily access objects and members in the std namespace

int main()
{
    // Boolean type
    bool boolean = true;// bools can be either true or false
    // Integer types (signed)
    char character = -127;// chars can be -128 to 127
    char characterA = 'A';// or any ASCII Character
    short int shortInt = 32767;// shorts can be -32768 to 32767
    int integer = 2147483647;// ints can be -2147483648 to 2147483647
    long long int longInt = 9223372036854775807;// long integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

    // Integer types (unsigned)
    unsigned char ucharacter = 250;// unsigned chars can be 0 to 255
    unsigned short int ushortInt = 65500;// unsigned shorts can be 0 to 65,535
    unsigned int uinteger = 42651203;// unsigned ints can be 0 to 4294967295
    unsigned long long int ulongInt = 181147483647;// unsigned long integers can be from 0 to 18,446,744,073,709,551,615

    // Floating point types
    float decimal = 3.14159265358979323846264;// floating point value +/- 3.4 ^ +/- 38  around 7 digits or precision
    double bigdecimal = 3.14159265358979323846264;// Double this big guy can have up to +/- 1.7 ^ +/- 308 around 15 digits or precicion


    cout << "bool is " << sizeof(bool) << " bytes wide and can = " << true << " or " << false << endl << endl;

    cout << "char is " << sizeof(char) << " bytes wide and can = " << characterA << " or " << (int)character << endl;
    cout << "short int is " << sizeof(short int) << " bytes wide and can = " << shortInt << endl;
    cout << "int is " << sizeof(int) << " bytes wide and can = " << integer << endl;
    cout << "long int is " << sizeof(long int) << " bytes wide and can = " << longInt << endl << endl;

    cout << "unsigned char is " << sizeof(unsigned char) << " bytes wide and can = " << (unsigned int)ucharacter << endl;
    cout << "unsigned short int is " << sizeof(unsigned short int) << " bytes wide and can = " << ushortInt << endl;
    cout << "unsigned int is " << sizeof(unsigned int) << " bytes wide and can = " << uinteger << endl;
    cout << "unsigned long int is " << sizeof(unsigned long int) << " bytes wide and can = " << ulongInt << endl << endl;

    cout << "float is " << setprecision(7) << sizeof(float) << " bytes wide and can = " << decimal << endl;
    cout << "double is " << setprecision(15) << sizeof(double) << " bytes wide and can = " << bigdecimal << endl;

    cin.get();

    return 0;
}

 

And the output for that code is:

bool is 1 bytes wide and can = 1 or 0

char is 1 bytes wide and can = A or -127
short int is 2 bytes wide and can = 32767
int is 4 bytes wide and can = 2147483647
long int is 4 bytes wide and can = 9223372036854775807

unsigned char is 1 bytes wide and can = 250
unsigned short int is 2 bytes wide and can = 65500
unsigned int is 4 bytes wide and can = 42651203
unsigned long int is 4 bytes wide and can = 181147483647

float is 4 bytes wide and can = 3.141593
double is 8 bytes wide and can = 3.14159265358979

Now This code is extremely messy and is more an intro in to the limits of each primitive type. I did, how ever,  sneak a new helpful lines in there and I’ll be going over it quickly here.

using namespace std;

If you remember in the last tutorial when ever we wanted to use cout or cin we had to append the namespace like this std::cour or std::cin. the using namespace statement effectively pulls those objects into our current namespace.

Note:

I did a few C style casts ((int)character)as well in order to get the number value from a char. Chars are usually used to store a character, but they can be used just like any other type of integer data type as well. The cout object interprets chars and characters so its easier to print strings and messages  with arrays of this type.