1、mini C+C+ Language TutorialBy will, 2010-12-131Introduction 12Structure of a program 23Variables and Data Types 44Constants 85Operators 96Basic Input/Output 157Control Structures 198Functions (I) 269Functions (II) 3010Arrays 3411Character Sequences 3912Pointers 4113Input/Output with files 491Introdu
2、ctionC+ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs a
3、s an enhancement to the C language and originally named C with Classes. It was renamed C+ in 1983.As one of the most popular programming languages ever created, C+ is widely used in the software industry. Some of its application domains include systems software, application software, device drivers,
4、 embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C+ compiler software, including the GNU Project, Microsoft, Intel and Embarcadero Technologies. C+ has greatly influenced many other p
5、opular programming languages, most notably C# and Java.1.1 Structure of this tutorialThe tutorial is divided in thirteen sections covering one specific topic each. You can access any section directly from the section index available, or begin the tutorial from any point and follow the links at the b
6、ottom of each section.Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.A good way to gain experience
7、with a programming language is by modifying and adding new functionalities on your own to the example programs that you fully understand. Dont be scared to modify the examples provided with this tutorial, thats the way to learn!1.2 CompilersThe examples included in this tutorial are allconsole progr
8、ams. That means they use text to communicate with the user and to show their results.All C+ compilers support the compilation of console programs. Check the users manual of your compiler for more info on how to compile them.2Structure of a programThe best way to start learning a programming language
9、 is by writing a program. Therefore, here is our first program:12345678910/ my first program in C+#include using namespace std;int main () cout Hello World!; return 0;Hello World!The first panel (in light blue) shows the source code for our first program. The second one (in light gray) shows the res
10、ult of the program once compiled and executed. To the left, the grey numbers represent the line numbers - these are not part of the program, and are shown here merely for informational purposes.The previous program is the typical program that programmer apprentices write for the first time, and its
11、result is the printing on screen of the Hello World! sentence. It is one of the simplest programs that can be written in C+, but it already contains the fundamental components that every C+ program has. We are going to look line by line at the code we have just written:/my first program in C+This is
12、 a comment line. All lines beginning with two slash signs (/) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of w
13、hat our program is.#include Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compilers preprocessor. In this case the directive#include tells the preprocessor to include the iostream standard file. This
14、 specific file (iostream) includes the declarations of the basic standard input-output library in C+, and it is included because its functionality is going to be used later in the program.using namespace std;All the elements of the standard C+ library are declared within what is called a namespace,
15、the namespace with the namestd. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C+ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutoria
16、ls.int main ()This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C+ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defi
17、ned before or after it - the instructions contained within this functions definition will always be the first ones to be executed in any C+ program. For that same reason, it is essential that all C+ programs have amainfunction.The wordmainis followed in the code by a pair of parentheses (). That is
18、because it is a function declaration: In C+, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.Right after these parentheses we can find the body of the mai
19、n function enclosed in braces (). What is contained within these braces is what the function does when it is executed.cout Hello World!;This line is a C+ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action
20、that generates a visible effect in our first program.coutis the name of the standard output stream in C+, and the meaning of the entire statement is to insert a sequence of characters (in this case theHello Worldsequence of characters) into the standard output stream (cout, which usually corresponds
21、 to the screen).coutis declared in theiostreamstandard file within thestdnamespace, so thats why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.Notice that the statement ends with a semicolon character (;). This character
22、is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C+ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).return 0;The return statement causes the main function to finish.
23、 return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0for themainfunction is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C+ console prog
24、ram.You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by/). There were lines with directives for the compilers preprocessor (those beginning by#). Then there were lines that began the decl
25、aration of a function (in this case, the main function) and, finally lines with statements (like the insertion intocout), which were all included within the block delimited by the braces () of the main function.In C+, the separation between statements is specified with an ending semicolon (;) at the
26、 end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans
27、that may read it.CommentsComments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.C+ supports two ways to insert comments:12/ line comment/* block comment *
28、/ The first of them, known as line comment, discards everything from where the pair of slash signs (/) is found up to the end of that same line. The second one, known as block comment, discards everything between the/*characters and the first appearance of the*/characters, with the possibility of in
29、cluding more than one line.We are going to add comments to our second program:1234567891011/* my second program in C+ with more comments */#include using namespace std;int main () cout Hello World! ; /prints Hello World! cout Im a C+ program; /prints Im a C+ program return 0;Hello World! Im a C+ pro
30、gramIf you include comments within the source code of your programs without using the comment characters combinations/,/*or*/, the compiler will take them as if they were C+ expressions, most likely causing one or several error messages when you compile it.3Variables and Data TypesThe usefulness of
31、theHello Worldprograms shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting program just to obtain a simple sentence written on the screen as result. It certainly would have been much faster to type the output senten
32、ce by ourselves. However, programming is not limited only to printing simple texts on the screen. In order to go a little further on and to become able to write programs that perform useful tasks that really save us work we need to introduce the concept ofvariable.Let us think that I ask you to retain t
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1