My First C++ Program
Okay, so we're done with the theory for now,though
we'll need it every now and then.
Let us take a look at our first C++ Program.
Now,i would recommend using an Integrated
Development Environment to all the beginners. It
has all the tools to begin with software
development: A text editor,a compiler,an
interpreter,a debugger, everything you need to get
started with.
(We'll get to what they are in a moment.)
Dev C++ and CodeBlocks are two of the popular and
free IDEs.
CodeBlocks is cross-platform,meaning it will work
on Windows, Mac or Linux Operating Systems. You can download it here:
http://www.codeblocks.org/downloads
The rest of the tutorials will assume you have
CodeBlocks running on your system. I use CodeBlocks 13.12.
Let us take a look at our first C++ Program.
Click on File->New->Empty File.
Type or Copy-Paste this code:
#include
int main()
{
std::cout<<"Hello World!";
return 0;
}
Save the file as helloworld.cpp.
To run it,press Ctrl+Shift+F9. The window at the
bottom of screen will show some messages.
Press Ctrl+F10. A black screen pops up and displays
"Hello World" and some information. Press enter.
What was that??!!!!
Firstly,C++ is a "free form" language, meaning that
you can write all the code in one line or add
as much empty lines between two statements as you want, and there will be no difference in the
functioning of the program. So,it is generally
considered a good programming practice to add a few "empty" lines between successive statements to make it more readable.
Secondly, it is case-sensitive. It means "Nikhil"
and "NIKHIL" and "nikhil" are all considered
different in C++.
Now,let us go through our code line by line.
The first line of our code contains a '#' symbol.
It is called a "preprocessor directive", meaning
that the command after this symbol is to be
processed before Compilation of the code. The word 'include' tells the compiler that our program needs to include the contents of a header file, named 'iostream' in our program. In a way, it tells the computer that some content of this iostream header file is to be used by our program, helloworld.cpp.
C++ itself has no means of Input/Output. To have
these functionalities, you need to include the
header file "iostream". It is necessary for the
program to understand the statement:
cout<<"Hello World";
The statement:
int main()
is a function declaration. It tells the compiler
that a function named "main" begins here,whose
return type is int.
Functions are nothing but a collection (or set) of
statements that perform a specific task and often return a value. Every C++
Program MUST have a main function. It can have
one,and ONLY ONE main() function. The execution of a program begins from main(), which means the
computer will start reading and understanding your
program from main function.
Everything written inside { } curly braces is a
part of main function.
The statement:
std::cout<<"Hello world";
Tells the computer to display a message "Hello
World" on the screen. You can change the text
inside " " to anything you want to display.
"cout" stands for console output,your monitor.
The '<<' operator is an output operator.
Almost all statements in C++ end with a semicolon
";".
return 0;
tells the compiler that the main() function returns
an integer value of 0. Usually, a non zero valkue
means an error and zero means successful execution of the program.
Too much theory? I still suggest you to go through it atleast twice and try to grasp as much as you can. Don't worry if you don't get all that in one go,almost no one does !
The next tutorial talks about what Compilation and Execution are,along with some more concepts.
[…] NEWS - C++, Some Basic Concepts My First C++ Program: Talking to the world Programming in C++: An […]
ReplyDelete[…] NEWS - Using Operators, Variables and Comments C++, Some Basic Concepts My First C++ Program: Talking to the world Programming in C++: An […]
ReplyDelete