High-quality programming guide-C/C ++ language Chapter 4 C/C ++ getting started with programming

Source: Internet
Author: User

Chapter 2 C/C ++

4.1 basic concepts of C/C ++ programs

4.1.1. About the startup function main ()

Each C ++ program contains one or more functions and must have a primary function named main. A function is composed of a sequence of statements that execute the function. The operating system calls the main () function to execute the program. The main () function executes the statements that constitute itself and returns a value to the operating system.

1. the prototype of the standard main () function stipulated in the C ++ standard is:
Int main (){/*....*/}

Or

Int main (INT argc, char * argv []) {/*... */}

Any other form is non-standard. It is an extension form of a specific implementation and is portable.
2. return values of the main () function:

In most systems, the return value of the main function is a status indicator. If the return value is 0, the main function is successfully executed. Any other non-zero return values are defined by the operating system. A non-zero return value indicates an error. Each operating system has its own method to tell the user what content the main function returns.

3. Restrictions of the C ++ standard on the main () function:

1) Reload is not allowed;

2) cannot be inline;

3) cannot be defined as static;

4) The address cannot be obtained;

5) cannot be called by the user;

......

4.1.2. Command Line Parameters

Int argc: Number of all parameters (including the name of the executable file itself );

Char * argc []: String pointer array (often written as char ** argc), which contains the name of the executable file and all parameters in sequence.

 

4.1.3. Internal name

In C and C ++, the identifiers (various functions, variables, types, and namespaces) defined by users (programmers) are converted into corresponding internal names according to specific rules.This is the so-called "name-mangling ).

Different implementations of Standard C ++ adopt different name-mangling schemes (standards are not mandatory), which is one of the reasons that the connectors between implementations of different languages are not compatible.

 

4.1.4 connection specifications

Don't understand! Although I have seen such code, I have never used it in practice and have never encountered such a problem. It will be used later.

4.1.5. variables and their initialization

1. Meanings of variables:A variable is the program element used to save data. It is the alias of a memory unit,The value of a variable is to read the value stored in its memory unit, while writing a variable is to write the value to the memory unit it represents.C ++ is also called an object.

2. in C/C ++, global variables (including extern and static) are stored in the static data area of the program and created before the program enters the main () function, the main () function is destroyed after the end, so there is no chance to initialize them in the root of our code, so the Language and Its Implementation provide a default global initiator0. If you do not explicitly provide an initial value (assign value) to a global variable, the compiler automatically converts 0 to the required type to initialize them.

3. differentiate between "initialization" and "assignment": the former occurs when the object (variable) is created, while the latter is after the object is created.

4. The declaration and definition of global variables should be placed at the beginning of the source file.

4.1.6. C Runtime Library
Generally, a C/C ++ program cannot use the C Runtime Library.

4.1.7. Differences between compile time and Runtime
We collectively refer to the phase of compiling the Preprocessor, compiler, and connector as "compile-time ". It is important to identify which structures work at "compile time" and which are important at "RunTime.

4.1.8. compilation unit and independent compilation technology
The independent compilation technology supported by the language implementation and development environment is not defined by the language itself. Each source code file (the source code and all header files included in its recursion) is the smallest compilation unit. Each independent unit can be compiled independently without the need to know the existence and compilation results of other compilation units.

4.2 basic data types and memory Images
1. the ansi c standard does not have a boolean type (bool ). Most C language implementations provide the bool type:
Typedef int bool;
# Define true 1
# Define false 0

2. NULL pointer values:
Null is the value 0 that can be assigned to any type pointer. In the C language environment, its type is void *. In C ++, it is an integer 0. That is:
# Ifdef _ cplusplus
# Define null 0
# Else
# Define null (void *) 0)

This is because implicit conversion from 0 to any pointer type is allowed in Standard C ++. Therefore, in C ++, many programmers prefer to use 0 instead of null to represent a zero-value pointer.

4.3 type conversion
C/C ++ does not directly operate on two different types of operands, the compiler will try to apply implicit conversion rules (usually the type that occupies less memory will be converted to the type that occupies more memory ).
Void pointer.
Implicit conversion.
Forced conversion.
4.4 identifier
4.5 escape sequence
4.6 Operator
Trying to memorize the operator's priority is meaningless. If there are many operators in the code line, use parentheses to determine the order of Calculation for Each subexpression in the expression, so as to avoid using the default priority. However, in some interview questions, such questions are often funny!
4.7 expression
In this section, the author [suggestion 4-1]: In the expression using the operator, try to put the child expression most likely to be false on the left. For the "|" expression, place the subexpression that is most likely to be true on the left of "|. The reason is that this method can improve the program execution efficiency.
With regard to this suggestion, the current C ++ coding standards seldom have such a rule, because this is not worthwhile in terms of readability.
Of course, if the Sub-expression itself is executed for a long time, it affects the program efficiency. It is natural for us to use this method for efficiency optimization.
4.8 basic control structure
4.9 select (judge) Structure
1. [recommended 4-2]: in the IF/else structure, we should try to put the conditions with a higher probability of true before this can improve the performance of this program.
Comment: I agree with the above suggestions without affecting readability! Currently, the C ++ coding specification emphasizes code readability and maintainability. It is not recommended to sacrifice readability for this purpose. Of course, if we find that "Conditional judgment expression efficiency" has affected program performance, it is another matter. In addition, we remember that Martin Fowler has a section in refactoring: replacing nested conditional expressions with Wei statements, and emphasizing the use of code to express intent.

2. Compare variables with zero values ". Well, let's leave it alone.
3. Supplementary description of the IF statement
The author mentioned two encoding styles:
1) if (null = var) is better than if (Var = NULL)
Comment: due to the influence of this book, I often use the first writing method. However, whenever I read my own code again, I always think this writing method is strange and uncomfortable, I wonder if other people have the same feelings as me?

The reason for doing so is: If (null = var) is invalid, but if (Var = NULL), it may cause a casual error. This is indeed the case, but most compilers today prompt warnings for Code such as if (Var = NULL. If you increase the warning level and follow the principle of "compiling your code cleanly under a high warning level", I think the author's reasons are no longer the reasons.
2) if (condition)
Return X;
Return y;
It should be rewritten:
If (condition)
Return X;
Else
Return y;

Or
Return condition? X: Y;
Rating: I think the three writing methods are quite good! In more cases, I seem to be using the first method (that is, the bad style I think ). The last write method is concise, but if both the conditional expression and the variable identifier are long, this write method is not very readable. Writing a line is too long and it is awkward to write two lines. Haha, the benevolent sees benevolence, the wise sees wisdom!

4. Switch statement

......

4.10 repeating Structure
The standard C/C ++ provides three types of loop structures: For, while, do/While.
I prefer the for structure because it is compact and concise! And you are unlikely to forget the incremental loop control variable-and while will often make you make such a mistake. Another point is that if the continue statement appears in the loop body, in the while structure, you need to write a line of code to make sure that the loop control variable is modified, instead of for. More depressing is that we often forget the code.
So when the while clause is used, I think it is difficult or awkward to implement the for clause, for example, an uncertain loop (that is, the loop control condition will find a change in the loop body ).
The author used a lot of space to analyze "the efficiency of loop statements". I didn't do much test on this. In other words, the first time you write code, the readability is better than the performance. Isn't performance important? Amount, important! But that is the case in the optimization phase.

4.11 structured program design principles
4.12 goto/Continue/break statement
The GOTO statement seems useless and may cause many risks and errors. Therefore, it is recommended that you remove it to avoid future problems.

However, the GOTO statement has a merits: it can jump out of the nested loop body, without writing many break statements.

 

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.