A computer program is a sequence of instructions that tell the computer what to do.
Statements and expressions
The most common type of instruction in a program is the statement. A statement in C++ is the smallest independent unit in the language. In human language, it is analogous to a sentence. We write sentences in order to convey an idea. In C++, we write statements in order to convey to the compiler that we want to perform a task. Statements in C++ are terminated by a semicolon.
There are many different kinds of statements in C++. The following are some of the most common types of simple statements:
1 2 3 | int x; x = 5; cout << x; |
int x
is a declaration statement. It
tells the compiler that x is a variable. All variables in a program
must be declared before they are used. We will talk more about
variables shortly.
x = 5
is an assignment statement. It assigns a value (5) to a variable (x).
cout << x;
is an output statement. It outputs the value of x (which we set to 5 in the previous statement) to the screen.
The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).
For example, the statement x = 2 + 3;
is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.
Functions
In C++, statements are typically grouped into units called functions. A function is a collection of statements that executes sequentially. Every C++ program must contain a special function called main(). When the C++ program is run, execution starts with the first statement inside of main(). Functions are typically written to do a very specific job. For example, a function named Max() might contain statements that figures out which of two numbers is larger. A function named CalculateGrade() might calculate a student’s grade. We will talk more about functions later.
Libraries
Libraries are groups of functions that have been “packaged up” for reuse in many different programs. The core C++ language is actually very small and minimalistic — however, C++ comes with a bunch of libraries, known as the C++ standard libraries, that provide programmers with lots of extra functionality. For example, the iostream library contains functions for doing input and output. During the link stage of the compilation process, the libraries from the C++ standard library are the runtime support libraries that are linked into the program (this will be discussed further in lesson 1.4).
Taking a look at a sample program
Now that you have a brief understanding of what statements, functions, and libraries are, let’s look at a simple hello world program.
Consider our hello world program:
1 2 3 4 5 6 7 8 | #include <iostream> int main() { using namespace std; cout << "Hello world!" << endl; return 0; } |
Line 1 is a special type of statement called a preprocessor directive. Preprocessor directives tell the compiler to perform a special task. In this case, we are telling the compiler that we would like to use the iostream library. The iostream library contains code that tells the compiler what cout and endl do. In other words, we need to include the iostream library in order to write to the screen.
Line 3 declares the main() function, which as you learned above, is mandatory. Every program must have a main() function.
Lines 4 and 8 tell the compiler which lines are part of the main function. Everything between the opening curly brace on line 4 and the closing curly brace on line 8 is considered part of the main() function.
Line 5 is our first statement (you can tell it’s a statement because it ends with a semicolon). As you learned in the explanation for line 1, cout and endl live inside the iostream library. However, within iostream, they live inside a special compartment named std (short for standard). This using statement tells the compiler to look inside a compartment named std if it can’t find cout or endl defined anywhere else. In other words, this statement is also necessary so the compiler can find cout and endl, which we use on line 6.
Line 6 is our output statement. Cout is a special object that represents the console/screen. The << symbol is an operator (much like + is an operator) called the output operator. Cout understands that anything sent to it via the << operator should be printed on the screen. Endl is a special symbol that moves the cursor to the next line.
Line 7 is a new type of statement, called a return statement. When an executable program finishes running, it sends a value to the operating system that indicates whether it was run successfully or not. The return value of main() is used for this purpose. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”. Non-zero numbers are typically used to indicate that something went wrong, and the program had to abort. We will discuss return statements in more detail when we discuss functions.
Conclusion
All of the programs we write will follow this template, or a variation on it. We will discuss each of the lines above in more detail in the upcoming sections.
Quiz
The following quiz is meant to reinforce your understanding of the material presented above.
1) What is the difference between a statement and an expression?
2) What is the difference between a function and a library?
3) What symbol do statements in C++ end with?
Quiz Answers
To see these answers, select the area below with your mouse.
![]() |
![]() |
![]() |
Hey I was wondering if you could tell me why when I compile and run a program (and its sucessful)the consel window appears and then dissapears…. it wold be nice to actually see what I am doing….
at the bottom of the main function before you return a value type in system(“PAUSE”);
Using #include ,clrscr() and getch() may help
I,m new at programming and put the code
1. #include
2.
3. int main()
4. {
5. using namespace std;
6. cout < < "Hello world!" << endl;
7. return 0;
8. }
in mij compiler (Dev-C++)
when I compile and execute this nothing happens or is this normal.
that is because you forgot to include your library in Line 1. you need to include this in order for your code to be executed. library =
and line 5 should come b4 ln3
#include
int main()
{
using namespace std;
int x;
x = 5;
cout << x;
system("pause");
return 0;
}
You need to use pause command to pause the program in compilers like dev-c++
Hope it helps
#include
using namespace std;
int main()
{
using namespace std;
cout<< "Hello world!";
return 0;
}
u can also right
#include
#include
void main ()
{
cout<<"hello world";
getch();
}
Comment by asselman
2010-03-05 03:03:18
I,m new at programming and put the code
1. #include
2.
3. int main()
4. {
5. using namespace std;
6. cout < < "Hello world!" << endl;
7. return 0;
8. }
on line 6 you have left space here 6. cout < <
it should be cout << and not cout < <
To fix up that code put the using namespace std; out-side the main function for example
#include
using namespace std;
int main ()
{
cout <<"\nYour Code Here";
return 0;
}
Always remember Preprocessors go outside functions GL
very good website this. thanks to the Admin of this website and the people who made this possiable to us. Great lessons
thanks again
Albanian.
[...] 1.1 Structure of a program [...]
Plz explain using namesapace std; in more understandable manner alex sir.
using namespace std;
is basically using "std" throughout the entire code.
Instead of writing
std::cout:: << "Hello World!" << std::endl;
or something like that. You won't have to include all that "std::" if you have
using namespace std;
I tried downloading C++ onto my flashdrive and succeeded but now I can’t write any codes. I tried downloading what this site recommended and my computer wouldn’t let it. What website can I go to download a functional C++ onto my flash drive?
When writing these programs are we meant to go the command prompt that is also downloaded. For whenever I type the HELLO WORLD code I get the message “<< was unexpected at this time"
Also I'm unable to write in lines so I use a semicolen when I'm finished writing what is a line on this site. I downloaded the software that was recommended but it's not working like this site is showing. I'm I doing something wrong?
I have a question about the following:
I have seen it used like is shown below. That is before the main function.
In this tutorial it is used inside the main function. In Visual Studio Express 2008 having
either before the main function or inside the main function produce the same result when the hello world code presented here is run. Is there a “correct” location for
I found the answer in lesson 7.11
How can I see the statements? for example this code the right way?
]czoxMTpcInggPSAgMiArIDM7XCI7e1smKiZdfQ==[
gr8 work
Good stuff!!!
Endl is a special symbol that moves the cursor to the next line
This is incorrect. The special symbol that moves the cursor to the next line is ‘\n’. Abuse of std::endl for this purpose is bad practice which leads to inefficient programs.
thanks
Learn Everything, Languages, Download Books, free PDF, IT news, Latest updates
On mac (xcode) there will be one error if you follow it exactly as shown on this page. Instead use this one
#include
int main () {
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
Even if you have ignore white space on it will still err. So do not give "{" its own line thats bad programming on xcode. it took me a week to figure that out, because i was wondering why it wasnt "build & run" so i started to play around with spacing, after i had already asked 10 different sites why didnt this code work, turns out it does, just requires a certain spacing requirement. My guess is you cant use "{}" without telling it why its there. thats probably a bad explaination or wrong explaination.
Or your compiler/IDE sucks. Any standard C++ compiler which follows the specifications should be able to compile with all the whitespace you need.
note “#include iostream” seem to get removed when “><" are used
ohhh… every nice i understand!
thanks! learncpp.com
Visit The IT Blog for eBooks N Solutions with Lots of Hacking Tricks
Which is suitable font and text color when I’m running Visual C++ in High Contrast mode of my Windows 7?
Depends on your needs. Experiment a bit, and find your own perfect color scheme.
I started doing some things on my own hehehe
#include
int main()
{
int x;
int y;
int d;
int f;
x = 5;
y = 5;
d = x + y;
f = d + d;
using namespace std;
cout << x + y << endl;
cout << f << endl;
return 0;
Consistency issue.
For beginners, the move from
using namespace std;
to
std::
might be confusing.
Hence I suggest amending the example code accordingly, or leaving a note at the end of the tutorial highlighting the interchangeability feature of the two.
[...] the section Introduction to programming, we had defined an expression as “A mathematical entity that evaluates to a value”. However, [...]
[...] the section Introduction to programming, we had defined an expression as “A mathematical entity that evaluates to a value”. However, [...]
Hey there!
I am wondering what “int” does in “int main()”.
What does it do and when do i use it?
I also read somewhere that you can use “void” instead of “int”.
What does “void” mean and what does it do?
[...] 1) Show Solution [...]
sir i m trying to do the first program of c++ langauge which is “hello world!” program i m trying to use the program formatting of u given on the web site.
#include
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
but my c langauge setup compiler show error in the program what can i do to run the program in my c langauge compiler.
error r of following type.
1.unable to open include file 'IOSTREAM'.
2.undefine symbol 'std'.
3.undefine symbol 'cout'.
4.undefine symbol 'endl'.
so please tell me the complete solution of this program on my gmail id which is gauravparuthi17@gmail.com.
sir i want to know that
what does ‘using namespace’ stand for?
why we use this in our programs?
so please tell me the complete information of this identity on my gmail id which is gauravparuthi17@gmail.com.
Return value from program will be given to OS and when we try to get last program status echo $? It will get back.
#include
using namespace std;
int main ()
{
return 89;
}
g++ hello.cpp
> a.out
>echo $?
>89
find programs in all languages DAA complier design and various programs in here.. click the link below
http://bhaincode.blogspot.in/2013_03_01_archive.html
[...] the section Introduction to programming, we had defined an expression as “A mathematical entity that evaluates to a value”. However, [...]
Guys! HELP! I am really new and I made a simple program of a ” password enter ” and it is not working! can some one fix it?
#include “stdafx.h”
#include
using namespace std;
int jellyfish;
int x;
int main()
{ cout << " Enter the pass here " <> x;
cout << "wrong answer" <> jellyfish;
cout << " youre logged in " << jellyfish << endl;
return 0;
}
hello this is a new user.I have a curiosity.
here in the sample example..we have used using name std but i am not taught about name std but did the same program as follows
#include
#include
void main()
{
cout<<"Hello World";
getch();
}
I think both will produce the same result and i find using name std and endl really complicated.Are they important??Can't i ignore them??
[...] Reference: http://www.learncpp.com/cpp-tutorial/11-structure-of-a-program/ [...]
As you said that cout prints anything sent to it via << operator.
I want to ask if there is any other operator which can send anything to cout.
Is there any other use of cout other than printing on screen ?
If yes, then how and if not then what is the need of << operator ?
We could have directly used cout "hello world".
Hi Alex ,
How standard libraries of C++ adds extra functionality and what do u mean by:
During the link stage of the compilation process, the libraries from the C++ standard library are the runtime support libraries that are linked into the program …
Why we need operators?
thanks for every one who do this site thanx much
Thanks for this site owner’s