Basic Program Construction
//first.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
cout<< "Welcome to this course\n";
system("PAUSE");
return 0;
}
Explanation:
- This program consists of a single function called main().
- Because of the parentheses()the compiler comes to know that this a function not a variable.
- The parentheses aren’t always empty. They’re used to hold function arguments (values passed from the calling program to the function).
- Line number 2 and 3 are not part of the function.
- The word int preceding the function name indicates that this particular function has a return value of type int.
- The bodyof a function is surrounded by braces (sometimes called curly brackets).
- Every function must use this pair of braces around the function body.
- A function body can consist of many statements but this function has only three statements (line number 5, 6 and 7)
- You can put several statements on one line and one statement in over two or more lines.
cout<<"Welcome to this course\n";
cout <<"Welcome Students\n" ; return 0;
- We don’t recommend these syntax. it’s nonstandard and hard to read. but it does compile correctly.
- #include is a preprocessor directive, which must be written on one line.
- #include <iostream> tells the compiler to first include standard C++ library named iostream to our project.
- A string constant "Welcome to this course\n" can also be broken into separate lines if u insert a backslash (\) at the line break or divide the string into two separate strings, each surrounded by quotes.
cout
<<"Welcome "
"to this "
"course\n"
;
cout
<<"Welcome \
to this \
course\n"
;
- A programs may consists of many functions but the main() function will be executed first.
- If there is no function called main()in your program, an error will be reported when you run the program.
- main()function may also call other functions.
Comments
Post a Comment