Find a job written interview those things (1)---c,c++ Foundation and Programming Style (2)

Source: Internet
Author: User

Author: Cold Young Yang
Time: August 2013.
Source: http://blog.csdn.net/han_xiaoyang/article/details/10515417.
Disclaimer: All rights reserved, reproduced please indicate the source, thank you.


Iv. Expressions and basic statements 4.1 operators and compound expressions

First of all, a very important point is the precedence of the C/s + + operator, the following figure is a summary of the table, the combination of the law of the special operator has been marked with bold bold.

To be honest, the operator precedence and associativity in the table above are very difficult to memorize. Although there is a table, but also can not check the table every time, so we write the program as far as possible to follow the following rules:

If there are more operators in the line of code, use parentheses to determine the order in which the expressions are operated, and avoid using the default precedence.

Example: Word = (high << 8) |  Low and if (a | b) && (A & c))

Some suggestions for compound expressions:

1 do not write complex expressions that are too complicated. For example i = a >= b && c < d && C + F <= g + H; it's too complicated.

2 Do not have a multi-purpose compound expression. For example d = (A = B + c) + R; should be split into two statements

3 do not confuse the compound expression in the program with the "Real mathematical expression".

4.2 If statement

If statements appear to be the simplest and most commonly used statements in the C ++/c language, many programmers write an if statement in an implicitly incorrect way. At the same time in the written interview some companies are still very keen to examine this point, as the basis for the applicant's basic knowledge of the criteria for evaluation.

The most frequently examined statements are those that are compared with 0 values:

1 Boolean variable and 0 value comparison

Do not compare a Boolean variable directly to True, FALSE, or 1 or 0. Assuming the Boolean variable name is flag, it compares the standard if statement with the 0 value as follows:

if (flag) //means flag is true

Other uses are in bad style, for example:

if (flag = = TRUE)

if (flag = 1)

if (flag = = FALSE)

if (flag = 0)

2) Integer variable and 0 value comparison

You should use an integer variable of "= =" or ". = "Direct comparison with 0." Assuming the name of the integer variable is value, the standard if statement that compares the 0 value is as follows:

if (value = = 0)

if (value!= 0)

Can not imitate the style of Boolean variables and write

if (value)//Can make people misunderstand value is a Boolean variable

if (!value)

3 floating point variable and 0 value comparison

You cannot use the floating-point variable "= =" or ". = "Compare with any number. Be aware that there is a precision limit for both F loat and double type variables. So be sure to avoid using the floating-point variable "= =" or ". = "In contrast to numbers, it should be managed to be converted into" >= "or" <= "forms.

Assuming that the name of the floating-point variable is x, you should

if (x = = 0.0)//Implied error comparison

Converted to

if ((X>=-epsinon) && (X<=epsinon )

Where Epsinon is the allowable error (i.e. precision)

4 pointer variable and 0 value comparison

You should use the pointer variable with "= =" or ". = "Compared to NULL. The 0 value of the pointer variable is "NULL" (recorded as null). Although the value of NUL L is the same as 0, the two meanings are different. Assuming the name of the pointer variable is p, it compares the standard if statement with the 0 value as follows:

if (p = = null) //p is explicitly compared with NULL, emphasizing that p is a pointer variable

if (P!= NULL)

Don't write

if (p = = 0)//easy to misunderstand p is an integer variable

if (P!= 0)

Or

if (p)//easy to misunderstand P is a Boolean variable

if (!p)

On the supplementary point of the IF statement: if (null = P), the odd format is not written incorrectly by the programmer to prevent the IF (P = null) from being mistakenly written as if (P = null) and intentionally inverting p and null. The compiler considers if (P = null) to be legitimate, but indicates that if (N ull = p) is incorrect, because NULL cannot be assigned.

4.3 about circular statements

In the C++/C Loop statement, the FOR statement is used most frequently, and the W hile statement is followed by a do statement that is rarely used. From the For loop, here are some points to note:

1 in multiple loops, if possible, the longest cycle should be placed in the outermost layer, the shortest loop at the outermost level, to reduce the number of times the CPU cuts across the loop layer.

For example, the following illustration is more efficient than left execution:

2 If there is a logical judgement in the circulation, and the number of cycles is very large, it is advisable to move the logical judgment to the outside of the circulation body. The program in the right box below, the simplicity is slightly reduced, but the efficiency of implementation is greatly improved.

3 Do not modify the loop variable in the For loop body to prevent the for loop from losing control.

4) It is suggested that the value of the cyclic control variable of the For statement should be used as "half open and half closed interval". As shown on the left:

4.4 About switch Statements

The switch statement format looks like this:

Note the point:

1 do not forget to add a break at the end of each CAS e statement, or you will cause multiple branches to overlap (unless you intentionally make multiple branches overlap).

2 Don't forget the last default branch.  Even if the program really does not need default processing, it should also retain the statement default:break; This is not superfluous, but to prevent others from thinking that you have forgotten to handle the default.

Five, constant

The C language uses #define来定义常量 (called Macro constants). C + + language in addition to #define外还可以用const来定义常量 (called the const constant), a constant is an identifier whose value is constant during run time.

5.1. The meaning of constants

This in the written interview time also tested several Ah, is a comparative basis of the details of things. The main thing to summarize is that there are three disadvantages to not using constants:

1) The readability (legibility) of the program becomes worse. Programmers themselves forget what numbers or strings mean, and users are more unaware of where they come from and what they represent.

2 Enter the same number or string in many parts of the program, and there is no writing error.

3 If you want to modify the number or string, it will change in many places, both troublesome and error prone.

But note that when we define constants, we try to use the intuitive Changshilai to represent numbers or strings that will appear multiple times in the program.

For example:

#define MAX/* C Language Macro constant * *

const int MAX = 100; Const constants for C + + languages

const float PI = 3.14159; Const constants for C + + languages

5.2 C + + constants Two different ways of defining

The C + + language can use const and #define to define constants in two ways, but the former is recommended because:

1const constants have data types, and macro constants do not have data types . The compiler can perform type security checks on the former. For the latter, only character substitutions, no type security checks, and the substitution of characters may produce unexpected errors (marginal effects).

2 Some integrated debugging tools can debug const constants, but you cannot debug macro constants .

5.3 related rules defined by constants

Simply put, when defining constants, it's best to follow these rules:

1 The constants that need to be exposed are placed in the header file, and the constants that do not need to be exposed are placed on the head of the definition file. For ease of management, the constants of different modules can be centrally stored in a common header file.

2 If a constant is closely related to other constants, this relationship should be included in the definition, and no isolated values should be given.

For example: const float RADIUS = 100;

const float diameter = RADIUS * 2;

5.4 About constants in a class

A very important point is also a common mistake, and sometimes we want some constants to be valid only in the class, so it is assumed that you should use const to modify the data members. However, the const data member is constant only for an object lifetime and is mutable for the entire class, because the class can create multiple objects, and the values of the const data members of different objects can be different.

Therefore: You cannot initialize a const data member in a class declaration . Because the object of the class is not created, the compiler does not know what the value of the size is. the initialization of a const data member can only be done in the initialization table of the class constructor .

If you want to build constants that are constant in the entire class . Const data members are not finished, and should be implemented using enumerated constants in the class.

Class A

{...

enum {SIZE1 = +, SIZE2 = 200}; Enumeration constants

int ARRAY1[SIZE1];

int array2[size2];

};

Enumeration constants do not occupy the storage space of objects, and they are evaluated at compile time. The disadvantage of an enumerated constant is that its implied data type is an integer, its maximum value is limited, and it cannot represent a floating-point number (such as pi=3.14159).

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.