// double slashes like this, up to the end of the line.
/* Comments can also be written starting with a slash
followed by a star, and ending with a star followed by
a slash. As you can see, comments written in this way
can span more than one line. */
/* This program prompts the user to enter any year.
It then outputs entered number. */
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter the current year \n";
cin >> year;
cout << "The year you entered is= " << year << endl;
return 0;
}
This program illustrates some features C++. After the comment lines, it begins with the statement...
#include <iostream>
This statement is called an include directive. It tells the compiler and the linker that the program will need to be linked to a library of routines that handle input from the keyboard and output to the screen (specifically the cin and cout statements that appear later). The header file "iostream" contains basic information about this library. You will learn much more about libraries of code later in this course.
After the include directive is the line:
Program variables are not like variables in mathematics. They are more like symbolic names for "pockets of computer memory" which can be used to store different values at different times during the program execution. These variables are first introduced in our program in the variable declaration.
using namespace std;
This statement is called a using directive. The latest versions of the C++ standard divide names (e.g. cin and cout) into subcollections of names called namespaces. This particular using directive says the program will be using names that have a meaning defined for them in the std namespace (in this case the iostream header defines meanings for cout and cin in the std namespace).
Some C++ compilers do not yet support namespaces. In this case you can use the older form of the include directive (that does not require a using directive, and places all names in a single global namespace):
#include <iostream.h>
Much of the code you encounter in industry will probably be written using this older style for headers.
Because the program is short, it is easily packaged up into a single list of program statements and commands. After the include and using directives, the basic structure of the program is:
int main()
{
First statement;
... ...
Last statement;
return 0;
}
All C++ programs have this basic "top-level" structure. Notice that each statement in the body of the program ends with a semicolon. In a well-designed large program, many of these statements will include references or calls to sub-programs, listed after the main program or in a separate file. These sub-programs have roughly the same outline structure as the program here, but there is always exactly one such structure called main. Again, you will learn more about sub-programs later in the course.
When at the end of the main program, the line
return 0;
means "return the value 0 to the computer's operating system to signal that the program has completed successfully". More generally, return statements signal that the particular sub-program has finished, and return a value, along with the flow of control, to the program level above. More about this later.
The example program uses a variable:
int year;
Program variables are not like variables in mathematics. They are more like symbolic names for "pockets of computer memory" which can be used to store different values at different times during the program execution. These variables are first introduced in our program in the variable declaration,which signals to the compiler that it should set aside enough memory to store four variables of type "int" (integer) during the rest of the program execution. Hence variables should always be declared before being used in a program. Indeed, it is considered good style and practice to declare all the variables to be used in a program or sub-program at the beginning. Variables can be one of several different types in C++.
“;” Is statment terminator and \n is an escape sequence that moves the program to the next line.
“;” Is statment terminator and \n is an escape sequence that moves the program to the next line.
No comments:
Post a Comment