Vision and dreams are the blueprints of soul and achievements.
-Mohammed Ahmed F

Introduction to C++

What is C and why learn it?

C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories. C was originally designed for writing system software but today a variety of software programs are written in C.

C can be used on many different types of computers but is mostly used with the UNIX operating system.

It is a good idea to learn C because it has been around for a long time which means there is a lot of information available on it.

Quite a few other programming languages such as C++ and Java are also based on C which means you will be able to learn them more easily in the future.

Your first program:

The first thing you must do i download the Turbo C++ and now type the following lines of code and then I will explain it. Make sure that you type it exactly as I have or else you will have problems.

Also don't be scared if you think it is too complicated because it is all very easy once you understand it.

#include<stdio.h>

int main()
{
   printf("Hello World\n");
   return 0;
}


#include<stdio.h>

This includes a file called stdio.h which lets us use certain commands. stdio is short for Standard Input/Output which means it has commands for input like reading from the keyboard and output like printing things on the screen.

int main()

int is what is called the return value which will be explained in a while. main is the name of the point where the program starts and the brackets are there for a reason that you will learn in the future but they have to be there.

{}

The 2 curly brackets are used to group all the commands together so it is known that the commands belong to main. These curly brackets are used very often in C to group things together.

printf("Hello World\n");

This is the printf command and it prints text on the screen. The data that is to be printed is put inside brackets.

You will also notice that the words are inside inverted commas because they are what is called a string.

Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas.

The \n is called an escape sequence and represents a newline character and is used because when you press ENTER it doesn't insert a new line character but instead takes you onto the next line in the text editor.

You have to put a semi-colon after every command to show that it is the end of the command.

Table of commonly used escape sequences:

\a- Audible signal

\b- Backspace
\t - Tab
\n - Newline
\v- Vertical tab
\f - New page / Clear screen
\r -Carriage return


return 0;

The int in int main() is short for integer which is another word for number. We need to use the return command to return the value 0 to the operating system to tell it that there were no errors while the program was running.

Notice that it is a command so it also has to have a semi-colon after it.

Save the text file as hello.c and now rum the program.

Indentation:

You will see that the printf and return commands have been indented or moved away from the left side. This is used to make the code more readable.

It seems like a stupid thing to do because it just wastes time but when you start writing longer, more complex programs, you will understand why indentation is needed.

Using comments:

Comments are a way of explaining what a program does. They are put after // or between /* */.

Comments are ignored by the compiler and are used by you and other people to understand your code.

You should always put a comment at the top of a program that tells you what the program does because one day if you come back and look at a program you might not be able to understand what it does but the comment will tell you.

You can also use comments in between your code to explain a piece of code that is very complex.

Here is an example of how to comment the Hello World program:

/* Author: Your name

   Date: yyyy/mm/dd
   Description:
   Writes the words "Hello World" on the screen */
#include<stdio.h>
int main()
{
   printf("Hello World\n"); //prints "Hello World"
   return 0;
}




INTRODUCTION TO C++:

Hello World Program:

Our first C++ program will print the message "Hello World" on the screen. Open a new project or console and start by typing the following line:

#include<iostream>

The above line includes the header file called iostream which will allow us to use the command to print words on the screen. Next you must type:

using namespace std;

This will let us use certain commands without having to type out their full name. Now we will type the main function.

int main()

{
}


The main function is where a program always starts. Every program must have a main function. The word int in front of main is to say what the return value is.

The curly brackets belong to the main function and show where it begins and where it ends. Now we will type the command that prints "Hello World" on the screen between the curly brackets.

cout << "Hello World\n";

The cout command is used to print things on the screen. The << means that the text must be output. The words that you want printed must be surrounded by quotes.

The \n means that the cursor must go the beginning of the next line. Lastly we must return 0 to the operating system to tell it that there were no errors while running the program.

return 0;

The full program should look like this:

#include<iostream>

using namespace std;
int main()
{
   cout << "Hello World\n";
   return 0;
}


Save the file as hello.cpp. You now need to compile the program. You need to open a command prompt and type the command name of your C++ compiler and the name of the C++ file you have just created.

Here is an example of how to do it with Turbo C++:

hello.cpp

If you are given error messages then you have made mistakes which means you should go through this post again and fix them.

If you don't get any errors then you should end up with an executable file which in my case is called hello.exe

Enter hello to run the program and you should see "Hello World" printed on the screen.

Congratulations! You have just made your first C++ program.

-Mohammed Ahmed F, Chief Administrative Officer.

Share this

Related Posts

Dear User,

Thank you for your comment. Hope you like this blog. Kindly share us on Social Media so others can be updated.

-Chief Administrative Officer.

Note: only a member of this blog may post a comment.