C++ program structure
Let's look at a simple piece of code that prints the word Hello World .
#include <iostream> using namespace std; // main() is where the program starts executing intmain () { cout << "Hello World"; // 输出 Hello World return 0; }
Next, we explain the above program:
- The C++ language defines header files that contain information that is necessary or useful in a program. In the above program, the header file <iostream> is included .
- The next line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively new concept in C++.
- The next line // main() is where the program begins execution is a one-line comment. Single-line comments begin with // and end at the end of the line.
- The next line int main() is the main function, where the program execution begins.
- The next line cout << "Hello World"; will display the message "Hello World" on the screen.
- The next line returns 0; terminates the main( ) function and returns the value 0 to the calling process.
C++ identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier begins with the letters AZ or az or the underscore _, followed by zero or more letters, underscores, and numbers (0-9).
Punctuation characters such as @, &, and % are not allowed within C++ identifiers. C++ is a case-sensitive programming language. Therefore, in C++, Manpower and manpower are two different identifiers.
Several valid identifiers are listed below:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
C++ keywords
The following table lists reserved words in C++. These reserved words cannot be used as constant names, variable names, or other identifier names.
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
trigram
A trigram is a sequence of three characters used to represent another character, also known as a three-character sequence. A three-character sequence always begins with two question marks.
Three-character sequences are less common, but the C++ standard allows certain characters to be specified as three-character sequences. In the past, this was an essential method to represent characters that were not on the keyboard.
Three-character sequences can appear anywhere, including strings, character sequences, comments, and preprocessing directives.
The most commonly used three-character sequences are listed below:
三字符组 | 替换 |
---|---|
??= | # |
??/ | \ |
??' | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
If you want to have two consecutive question marks in the source program, and do not want to be replaced by the preprocessor, this situation occurs in character constants, string literals or program comments, the alternative is to use automatic string concatenation : "...?" "?..." or escape sequence: "...?\?...".
Starting with Microsoft Visual C++ version 2010, the compiler no longer automatically replaces trigrams by default. If you need to use trigraph substitution (eg for compatibility with old software code), you need to set the compiler command line option /Zc:trigraphs
g++ still supports trigrams by default, but will give compile warnings.
Exercises on input and output
#include <stdlib.h> #include <iostream> using namespace std; // Requirement: Prompt the user to input an integer, and print the integer on the screen in octal, decimal, and hexadecimal respectively. // Requirement: Prompt the user to enter a boolean value (0/1), and in Boolean way The value is printed on the screen int main(void) { cout << " Please enter an integer: " << endl; int x = 0 ; cin >> x; cout << oct << x << endl; // octal cout << dec << x << endl; // decimal cout << hex << x << endl; // hex cout < < " Please enter a boolean value (0, 1): " << endl; bool y = false ; cin >> y; cout << boolalpha << y << endl; system("pause"); return 0; }