A Brief Introduction to the C ++ Language

Source: Internet
Author: User

In this tutorial, the C language is introduced in depth, from basic knowledge to the latest functions of ANSI-C standards, the content covers from the basic concepts such as arrays, classes to polymorphism, templates and other advanced concepts. Based on practical principles, each section combines examples of programs that can work, so that readers can get started with internships from the first lesson.
Next we will start with a simple program to look at the composition structure of a C program.
// My first program in C
# Include <iostream. h>
Using namespace std;
Int main (){
Cout <"Hello World! ";
Return 0;
}
Display result Hello World!

The source code of our first program is displayed on the left. The code file name is hellowworld. cpp. The output result after the program is compiled and executed is displayed on the right. The method for editing and compiling a program depends on the compiler you are using. The compilation method may vary depending on whether it has a graphical interface or version, for details, refer to the instructions for using the compiler.
The above program is the first program that most beginners learn to write. Its running result is "Hello World!" displayed on the screen! . Although it may be one of the simplest programs that can be written by C, it already contains the basic composition structure of each C program. Next we will analyze each part of its composition structure one by one:
// My first program in C
This is a comment line. All Program lines starting with two diagonal lines (//) are considered as comments. These comments are written in the program source code by the programmer for simple interpretation or description of the program, it does not affect the running of the program. In this example, this line of comment gives a brief description of what this program is.
 
# Include <iostream. h>
The sentence starting with the # sign is the instruction statement of the Preprocessor. They are not executable code, but just instructions to the compiler. In this example, the sentence # include <iostream. h> tells the compiler's Preprocessor to include the standard header file (iostream. h) of the input and output stream in this program. This header file includes the declaration of the basic standard input-output library defined in C. This is included because its functions will be used later in this program.
Using namespace std;
All elements of the C standard function library are declared in a namespace, which is the std namespace. Therefore, in order to be able to access its functions, we use this statement to express the elements we will use as defined in the standard namespace. This statement is frequently used in C programs using the standard function library. It is also used in most code examples in this tutorial.
Int main ()
This row is the initial declaration of the main function. Main function is the starting point for running all C Programs. Whether it is at the beginning, end, or center of the code-the code in this function is always the first to be executed when the program starts to run. In addition, for the same reason, all C Programs must have a main function.
The main is followed by a pair of parentheses (), indicating that it is a function. All functions in C have a pair of parentheses (), which can contain some input parameters. As shown in the example, the content of the main function is enclosed by curly brackets ({}) immediately after its declaration.
Cout <"Hellow World! ";
This statement is the most important in this program. Cout is the standard output stream (usually the console, that is, the screen) in C. This sentence inserts a string ("Hello World" in this example) into the output stream (console output). The declaration of cout is in the header file iostream. h, so to use cout, you must include the header file at the beginning of the program.
Note that this sentence ends with a semicolon. A semicolon indicates the end of a statement. Each statement in C must end with a semicolon. (One of the most common mistakes C programmers make is to forget to write a semicolon at the end of the statement ).
Return 0;
The return Statement (return) causes the main function main () to end execution and returns the code followed by the statement (0 in this example. This is the most common method of program termination without any errors during program execution. In the following example, you will see that all C Programs end with similar statements.
You may notice that not all rows in the program will be executed. The program may have annotation lines (beginning with //), indication lines of the compiler Preprocessor (starting with #), and function declaration (main function in this example ), finally, program statements (for example, cout <) are called, and all the statements are enclosed in braces ({}) of the main function.
In this example, the program is written in different rows for easy reading. In fact, this is not necessary. For example, the following program
Int main ()
{
Cout <"Hello World ";
Return 0;
}
It can also be written as follows:
Int main () {cout <"Hello World"; return 0 ;}
The above two procedures are identical.
In C, the statement separator is semicolon. The Branch only writes code to facilitate reading.
The following program contains more statements:
// My second program in C
# Include <iostream. h>
Int main ()
{
Cout <"Hello World! ";
Cout <"Im a C program ";
Return 0;
} The displayed result is Hello World! Im a C program
 

In this example, we call the cout <function twice in two different statements. Once again, it is explained that the branch program code is only for our convenience, because this main function can also be written in the following form without any problems:
Int main () {cout <"Hello World! "; Cout <" Im to C program "; return 0 ;}

For convenience, we can also divide the code into more lines to write:
Int main ()
{
Cout <
"Hello World! ";
Cout
<"Im a C program ";
Return 0;
}
The running result is exactly the same as that in the preceding example.
This rule does not apply to pre-processor indication rows (rows starting with #) because they are not real statements. They are read and ignored by the pre-processor and do not generate any code. Therefore, each of them must be in a separate line, with no semicolon (;) at the end (;)
Comments)
Comments are part of the source code, but they are ignored by the compiler. They do not generate any Execution Code. The purpose of using annotations is to allow programmers to insert explanatory content in the source program.
C supports the following two methods:
// Line comment
/* Block comment */
The first method is line comment, which tells the compiler to ignore any content from // to the end of the row. The second type is block annotation (segment annotation), which tells the compiler to ignore all content between the/* symbol and the */Symbol and may contain multiple lines of content.
In our second program below, we inserted more comments.
/* My second program in C
With more comments */
# Include <iostream. h>
Int main ()
{
Cout <"Hello World! "; // Says Hello World!
Cout <"Im a C program"; // says Im a C program
Return 0;
} The displayed result is Hello World! Im a C program
 
If you insert comments in the source program without the // or/* and */symbols, the compiler regards them as C statements, one or more error messages will appear during compilation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.