In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Write a simple C++ program
Each C++ program contains one or more functions (function), one of which must be named main. The operating system runs C++ programs by calling main. Here is a very simple main function that does nothing but returns a value to the operating system:
[cpp] view plaincopy
Int main ()
{
Return 0
}
The definition of a function consists of four parts: the return type (return type), the function name (function name), a parenthesized formal parameter list (parameterlist, allowed to be empty), and the function body (function body). Although the main function is special to some extent, its definition is the same as other functions.
In this case, the formal parameter list of main is empty (there is nothing in ()). Section 6.2.5 (page 218) will discuss other types of formal parameters for main.
The return type of the main function must be int, that is, the integer type. An int type is a built-in type (built-in type), a type defined by the language itself.
The last part of the function definition is the function body, which is a block of statements (block of statements) that begins with the left curly bracket (curly brace) and ends with the right curly bracket:
[cpp] view plaincopy
{
Return 0
}
The only statement in this block is return, which ends the execution of the function. In this case, return also returns a value to the caller. When a return statement includes a value, the type of this return value must be compatible with the return type of the function. In this case, the return type of main is int, and the return value of 0 is indeed a value of type int.
Notice the semicolon at the end of the return statement. In C++, most C++ statements end with a semicolon. They are easy to ignore, but if you forget to write a semicolon, it can lead to inexplicable compilation errors.
In most systems, the return value of main is used to indicate the status. The return value of 0 indicates success, and the meaning of a non-zero return value is defined by the system and is usually used to indicate the type of error.
Important concept: type
Type is one of the most basic concepts of programming, and we will encounter it again and again in this book. A type defines not only the contents of the data elements, but also the operations that can be performed on such data.
The data processed by the program is stored in variables, and each variable has its own type. If a variable named v is of type T, we usually say that "v has type T", or equivalent, "v is a T-type variable".
, run the program
Compile and run the program
After writing the program, we need to compile it. How to compile the program depends on the operating system and compiler you use. For details about the specific compiler you are using, please refer to the reference manual or ask experienced colleagues.
Many compilers on PCs have an integrated development environment (Integrated Developed Environment,IDE) that packages compilers with other program creation and analysis tools. Such integration environments can be useful tools when developing large programs, but it takes some time to learn how to use them efficiently. Learning how to use this type of development environment is beyond the scope of this book.
Most compilers, including those that integrate IDE, provide a command line interface. Unless you already know IDE, you will find it easy to start learning C++ through the command line interface. The advantage of this way of learning is that you can focus on the C++ language itself (rather than some development tools) first, and IDE is usually easy to learn once you have mastered the language.
Program source file naming convention
Whether you use the command line interface or IDE, most compilers require the program source code to be stored in one or more files. Program files are often called source files (source file). In most systems, the name of the source file ends with a suffix, which consists of a period followed by one or more characters. The suffix tells the system that the file is a C++ program. Different compilers use different suffix naming conventions, the most common including .cc, .cxx, .cpp, .cp, and .C.
Run the compiler from the command line
If we are using the command line interface, the program is usually compiled in a console window (such as a shell window in a UNIX system or a command prompt window in a Windows system). Assuming that our main program is saved in the file prog1.cc, you can compile it with the following command
$CC prog1.cc
Where CC is the name of the compiler program and $is the system prompt. The compiler generates an executable file. The Windows system will name the executable prog1.exe. Compilers in UNIX systems usually name the executable a.out.
In order to run an executable file on the Windows system, we need to provide the file name of the executable file and ignore its extension. exe:
$prog1
On some systems, even if the file is in the current directory or folder. You must also explicitly indicate the location of the file. In this case, we can type
$.\ prog1
"." Followed by a backslash to indicate that the file is in the current directory.
In order to run an executable on the UNIX system, we need to use the full-text file name, including the file extension:
$a.out
If you need to specify a file location, you need to use a "." Followed by a slash to indicate that the executable is in the current directory.
$. / a.out
The method of accessing the return value of main depends on the system. In both UNIX and Windows systems, the return value can be obtained through the echo command after a program has been executed.
On UNIX systems, get the status with the following command
$echo $?
To view the status in the Windows system, you can type
$echo ERRORLEVEL%
Run the GNU or Microsoft compiler
The commands to run the C++ compiler vary from operation to operation and compiler system. The most commonly used compilers are GNU compiler and Microsoft Visual Studio compiler. By default, the command to run the GNU compiler is:
$Gmail +-o prog1 prog1.cc
Here, $is the system prompt. -o prog1 is a compiler parameter that specifies the filename of the executable file. In different operating systems, this command generates an executable file called prog1 or prog1.exe. On UNIX systems, executables have no suffix; on Windows systems, the suffix is .exe. If the-o prog1 parameter is omitted, the compiler will generate an executable file named a.out in the UNIX system and an executable file named a.exe in the Windows system (note: depending on the version of the GNU compiler you are using, you may need to specify the-std=c++0x parameter to turn on support for GNU compiler 11).
The command to run the Microsoft Visual Studio 2010 compiler is cl:
C:\ Users\ me\ Programs > cl / EHsc prog1.cpp
Here, C:\ Users\ me\ Programs > is the system prompt, and\ Users\ me\ Programs is the current directory name (that is, the current folder). The command cl invokes the compiler, and / EHsc is the compiler option that turns on standard exception handling. The Microsoft compiler automatically generates an executable file whose name corresponds to the name of the first source file. The executable file name is the same as the source file name, with the suffix .exe. In this example, the file name of the executable is prog1.exe.
Compilers usually contain options to warn against problematic program structures. It is usually a good habit to turn on these options. We are used to using the-Wall option in GNU compilers and / W4 in Microsoft compilers.
For more information, please refer to the reference manual of the compiler you are using.
Practice
Exercise 1: check the documentation of the compiler you are using to determine the file naming convention it uses. Compile and run the main program on page 2.
Exercise 2: rewrite the program to return-1. The return value of-1 is usually used as an identification of a program error. Recompile and run your program to see how your system handles the error flags returned by main.
This article is excerpted from C++ Primer Chinese version (5th Edition)
Stanley B.Lippman (Stanley Lippman) Josee Lajoie (Joseph Rajoy) Barbara E. Moo (Barbaramo)
Translated by Wang Gang and Yang Jufeng
Published by Electronic Industry Press
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.