Write a simple C ++ Program
Each C ++ program contains one or more functions.Main. The operating system calls main to run the C ++ program. The following is a very simple main function, which does nothing but returns a value to the operating system:
[Cpp]View plaincopy
Int main ()
{
Return 0;
}
A function is defined in four parts: return type (return type), function name (function name), parameterlist (empty), and function body (body ). Although the main function is quite special to some extent, its definition is the same as that of other functions.
In this example, the main parameter list is empty ). Section 218th of section 6.2.5) will discuss other main parameter types.
The return type of the main function must be int. Int type is a built-in type built-in type), that is, the type defined by the language itself.
The last part of the function definition is the function body, which is a block of statements starting with the left curly braces (curly brace) and ending with the right curly braces ):
[Cpp]View plaincopy
{
Return 0;
}
The only statement in this block is return, which ends the execution of the function. In this example, return also returns a value to the caller. When a return statement includes a value, the type of the returned value must be compatible with the return type of the function. In this example, the return type of main is int, And the return value 0 is indeed an int value.
Note that 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 will lead to an inexplicable compilation error.
In most systems, the return value of main is used to indicate the status. The return value 0 indicates success. The meaning of a non-zero return value is defined by the system and is usually used to indicate the error type.
Important Concept: Type
Type is one of the most basic concepts of programming, and we will encounter it repeatedly in this book. A type not only defines the content of data elements, but also defines the operations that can be performed on such data.
The data processed by the program is stored in the variable, and each variable has its own type. If the type of a variable named v is T, we usually say "v has type T", or "v is a T type variable ".
, Run the program
Compile and run programs
Compile the program. How to compile a program depends on your operating system and compiler. For details about the usage of the specific compiler you are using, refer to the reference manual or ask experienced colleagues.
Many compilers on PCs have Integrated development environments Integrated Developed Environment, IDE), which pack compilers with other Program Creation and analysis kits. This type of integrated environment may be very useful tools when developing large programs, but it takes some time to learn how to use them efficiently. Learning how to use such a development environment is beyond the scope of this book.
Most compilers, including compilers integrated with IDE, provide a command line interface. Unless you already know about IDE, you will find it easy to start learning C ++ through the command line interface. The advantage of this learning method is that you can focus on the C ++ language rather than some development tools first. In addition, once you have mastered the language, IDE is usually easy to learn.
Naming Conventions for program source files
Whether you use the command line interface or IDE, most compilers require that the program source code be stored in one or more files. Program files are usually called source files ). In most systems, the source file name ends with a suffix, which is composed of one or more characters after a period. The suffix tells the system that the file is a C ++ program. Different compilers use different suffix naming conventions, including. cc,. cxx,. cpp,. cp, And. C.
Run the compiler from the command line
If we are using the command line interface, we usually compile the program in a console window, for example, a shell program window in a UNIX system or a command prompt window in a Windows system. Assume that our main program is saved in the prog1.cc file. You can use the following command to compile it:
$ CC prog1.cc
CC is the name of the compiler program, and $ is the system prompt. The compiler generates an executable file. Windows system will name this executable file prog1.exe. In UNIX systems, compilers usually name executable files a. out.
To run an executable file in Windows, you can omit the extension name. .exe:
$ Prog1
In some systems, even if the file is in the current directory or folder. You must also explicitly specify the file location. In this case, you can type
$. \ Prog1
"." Is followed by a backslash indicating that the file is in the current directory.
To run an executable file in a UNIX system, we need to use the full file name, including the file extension:
$ A. out
If you need to specify the file location, you need to use a "." followed by a slash to indicate that the executable file is located in the current directory.
$./A. out
The method for accessing the return value of main depends on the system. In UNIX and Windows systems, after a program is executed, the return value can be obtained through the echo command.
In a UNIX system, run the following command to obtain the status:
$ Echo $?
To view the status in Windows, enter
$ Echo % ERRORLEVEL %
Run the GNU or Microsoft Compiler
In different operations and compiler systems, the commands used to run the C ++ compiler are also different. The most common compilers are the GNU Compiler and Microsoft Visual Studio compiler. By default, the command to run the GNU Compiler is g ++:
$ G ++-o prog1 prog1.cc
Here, $ is a system prompt. -O prog1 is a compiler parameter that specifies the name of the executable file. In different operating systems, this command generates an executable file named prog1or prog1.exe. In the unixsystem, the executable file is not suffixed with .exe in the Windows system. If-o prog1example is omitted, the compiler will generate an executable file named a.outin the unixsystem. In Windows, an executable file named a.exe will be generated. Note: According to the version of the GNU Compiler used, you may need to specify the-std = c ++ 0x parameter to enable C ++ 11 support ).
The command to run the Microsoft Visual Studio 2010 compiler is cl:
C: \ Users \ me \ Programs> cl/ehsprog1.cpp
Here, C: \ Users \ me \ Programs> is a system prompt, and \ Users \ me \ Programs is the current directory name, that is, the current folder ). The command cl calls the compiler./ehsis the compiler option used to enable standard exception handling. The Microsoft compiler automatically generates an executable file whose name corresponds to the first source file name. The executable file name is the same as the source file name, and is suffixed with .exe. In this example, the executable file name is prog1.exe.
The compiler usually contains some options to warn of problematic program structures. Opening these options is usually a good habit. We are used to using the-Wall option in the GNU Compiler and/W4 in the Microsoft compiler.
For more information, see the reference manual for the compiler you are using.
Exercise
Exercise 1: Check the documentation of the compiler you are using to determine the file naming conventions it uses. Compile and run the main program on page 1.
Exercise 2: rewrite the program and let it return-1. The return value-1 is usually identified as a program error. Recompile and run your program, and observe how your system handles the error mark returned by main.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916150020640? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border: none; "alt =" Center "/>
This article is excerpted from "C ++ Primer Chinese Version 5th".
Description: Stanley B. Lippman (Stanley Lipman) Joseph Lajoie (Joseph lajoy) Barbara E. Moo (Barbara Mo)
Translated by Wang Gang Yang jufeng
Published by Electronic Industry Publishing House