First Program: Hello World in C/C++
It's a tradition to start learning programming language with the Hello World program. So, in this tutorial, we'll look at the Hello World program in C and in C++. They are very similar but there are some differences as C++ uses its new features. You can write code in C language in the C++ program and it will be ok too. C++ is backward compatible with C - it was designed this way.
The Hello World program should just output the phrase "Hello World" on the screen. Let's start with the code right away. We have two versions of Hello World. All code from this tutorial can be compiled in GCC or Visual C++ 2019 (or earlier versions). Let's start with C language version:
Hello World in C Language
Let's look at the source code of "Hello World" in C language:
First string includes the file stdio.h. Files with extension .h in C are header files. #include is a preprocessor directive. It allows us to use already written (by us or other developers) code in the current program. In this case, it includes stdio.h (standard input/output) - this file contains features that are related to input/output in C. We need it to use printf construction in code above.
Let's look at the main construction and then examine it:
Here I put a comment in the braces. A comment is a text that will not be executed i.e. it's not part of the program, it's a reminder or explanation of the code for yourself or other programmers. Here I replaced the actual code with the comment so we can concentrate on main construction.
main is a function that is defined by us. Any program in C language (and C++ too) must have this construction. It consists of a header - the first string, and the body - everything in braces. We'll learn what is int and (void) in later tutorials. For now, just understand that this construction is mandatory for all programs in C. Copy/paste it in yours any future program and put your actual code into braces.
main is called an entry point of the program. When you run a program, the operating system (Windows, Linux...) executes everything inside the braces of main. We have this code inside:
Second string is part of the main construction and is mandatory too. The first string just prints "Hello, World!" on the screen (command line or terminal). To print something on the screen we put the needed text in the double-quotes and put it in the brackets after the word printf. printf is a very powerful function and we'll examine it's capabilities soon.
Pay attention that both strings end with a semicolon. Each statement in C should end with semicolon.
String literals in C and C++Let's talk about the thing inside parentheses after printf. Literally we see there "Hello, World!\n" - the string of characters inside double-quotes. This is called a string literal. When we see in source code characters in double-quotes, that means we see string literals. And what about \n?
Escape Sequences in C/C++There are special sequences of characters in C that are called escape sequences. They are used to "print" new line, tab, unicode characters - these are characters that you can't input from the keyboard. This characters are preceded by \ symbol:
We need to use escape sequences to print \, ', ", ?. \u is used to print Unicode (we'll have a large tutorial about it later) in the console. Here is the result of the code above:
printf function in C language
printf is a function. There are many functions in C. And even more in C++. main is a function too. But let's chew it by small pieces, we'll have a large tutorial about functions in C/C++ programming language. For now, think that function is a piece of code that solves a specific task. Like printf outputs string. Moreover, printf can output formatted string.
printf is a name of a function and we put arguments of a function in brackets. Above we saw that printf accepted one argument - string literal. But printf can accept a different number of arguments to format the output in a specific way. The first argument should always be a string and we can put many format specifiers in it. After the first argument, we place the values for format specifiers in order of the appearance of specifiers in the first argument. All arguments must be separated by comma.
Format specifiers of printf
Let's change C Hello World output (here I used vertical bars to show what exactly is printed and where are the white spaces):
%s format specifier allows to format the string of a text. As you can see above it allows to set space for text and align it. Pay attention to the output of the % sign itself - you repeat it two times. In the last example, there is a printf with two format specifiers.
printf can format other data types: integer numbers and numbers with a floating point. Also it can print separate characters:
%i is used to output integer numbers. %f - floating-point numbers. Pay attention that for 3.14 printf output 3.140000. We'll discuss floating-point numbers later. %c is used to print a separate character. There is a difference between "X" and 'X'. We'll talk about it later too. For now: single quotes are used when you want to represent one character in C language. The interesting thing is with the last example. It uses character format specifier, the argument for it is a number and the result is a character. It's one of the examples that every being in computers is just a bunch of numbers. Later we'll know how the text is encoded with numbers, but here we can see that 88 representing character X. If you change the number you'll see different characters.
Let's look at C++ version of Hello World.
C++ Hello World
Hello World in C++ is very similar to C version:
If you create a project in Visual Studio, you'll have one small difference:
Visual Studio adds this first line to every project, but we can remove it. The code looks very familiar to C Hello World. Let's dive a bit deeper. The thing is: we can have the same code of our C/C++ program for different operating systems. All we need to do is to compile the code to the operating system we want.
#include is a directive. It tells the compiler to insert in this place the specific file. pch.h, iostream and stdio.h - are the names of the files. We include so-called header files - we'll discuss what they are in the future tutorials. We include header files to be able to use some code. We can use cout only if we include iostream and printf only if stdio.h is included. iostream allows us to use C++ input and output in our programs.
Then we see int main() - every program must have this line. main - is the name of the function. And it's the entry point of the program. When we run the executable file, the operating system looks up for the main and executes code in curly braces after the name. The syntax should be always the same: int main() then curly braces and in curly braces, we write our program. Parentheses after the main are empty here, but they may contain void (like we saw in C version) or other things - we'll discuss it later.
To print the line in C++ we need to use cout (we still can use printf, but cout is how to do it in C++ way). std - is the namespace there cout belongs. So std::cout - we tell the compiler to check std namespace and find there cout. << is the insertion operator. It takes the right part and places it to the left part. So it takes "Hello World!\n" and puts it in cout. And as a result, we see text Hello World! on the screen.
Last line returns control to the environment and program ends.
Comments in C++ and C
The first thing we need to learn is comments. Let's see the code:
I've added several comments. A comment is a text that compiler will ignore - so you can put remarks for your teammates or for yourself for the future. A comment that starts with // works till the end of the line. /* */ is the block comment and it can have several lines.
Now we need to put std:: before any entity from the standard namespace and we'll use standard namespace a lot. We can point out that we'll use standard namespace by default. To do this we need to put using namespace std; before main:
Now we can use just cout (and any other symbols from the standard namespace). Later we'll use our own namespaces. And we'll talk about namespaces in future tutorials.
C/C++ Hello World - Summary
We created our first program. Now you need to compile it. You can find needed information in the previous tutorial or in the Software section. After you will compile the initial Hello World program in C/C++ language and run it, you should see on the screen this phrase:
- Compile and run the Hello World program
- Output another text string