Details that are easy to ignore in C language (article 2), ignore Article 2

Source: Internet
Author: User

Details that are easy to ignore in C language (article 2), ignore Article 2

Preface:The purpose of this article is to record the details that are easy to ignore in the C language. I plan to take some time every day to read books and stick to it. Today is the first article, maybe next month's today is the second article, and next year's today is the first article ?...... I firmly believe that good memory is not as bad as writing.


Article 1: details easily overlooked in C language (article 1)


1,The C language only contains one-dimensional arrays, And the array size must be determined as a constant during compilation. In C, the array element can be any object or another array, that is, the array.


2,The C language allows redundant commas (,) in the initialization list. For example: int days [] = {1, 2, 3,}; function: facilitates automatic code generation.


3,Because a [I] is equivalent to * (a + I), and because a + I and I + a have the same meaning, therefore, a [I] And I [a] have the same meaning.


4,In the expressions using the "&" operator, try to place the subexpression most likely to be false on the left of "&". In the expressions using the operator "|, place the subexpression most likely to be true on the left of "|. Because C/C ++ uses the"Sudden death": If the subexpression on the left of" & "is calculated as false, the entire expression is false, and the subexpression following it does not need to be computed; if the subexpression on the left side of "|" is calculated as true, the entire expression is true, and the subsequent subexpression does not need to be calculated. This method can improve program efficiency.


5,For multi-dimensional arrays, the correct Traversal method depends on the sequence in which the language arranges the storage space of array elements. For a multi-dimensional array of C/C ++,The traversal efficiency of "first and second columns" must be better than that of "first column and second row"No matter whether the number of rows is much larger than the number of columns, the opposite or even close. The efficiency is mainly influenced by the Memory Page switching times and cache hit rate caused by large arrays, rather than the number of loops.


6,Why does the fgetc () function return an int instead of an unsigned char?

If a function call error occurs or fgetc () is read at the end of the file, an EOF (-1) is returned. If the EOF is stored in the unsigned char type, the result is 0xff, and 0xff is a meaningful ASCII code byte, it cannot be distinguished from the byte 0xff or the end letter EOF. If the EOF is stored in the int type, the result is 0 xffffffff. If the read byte 0xff is converted to the int type 0x000000ff, The EOF and byte 0xff can be correctly distinguished.


7,If a function has no parameters, use void instead of empty. Standard C interprets the empty parameter list as a parameter of any type and number, standard C ++ interprets the empty parameter list as unacceptable. Pay special attention to these differences when porting C/C ++ programs.


8,Check the correctness and efficiency of the return Statement at the "exit" of the function body.

(1)The return statement cannot return "Pointer" or "Reference" of "stack memory" because the memory unit is automatically released when the function body ends.

// The syntax of char * Func (void) {char str [] = "hello world"; return str; // This statement has a hidden danger, memory units pointed to by str will be released} // a feasible statement const char * Func (void) {const char * p = "hello world"; return p; // return the address of the String constant}

(2)If the return value of a function is an object, the efficiency of the return statement should be considered.

Return string (s1 + s2); // Statement 1 string result (s1 + s2); // Statement 2 return result;

Statement 1 is not equivalent to Statement 2. Statement 1 indicates creating a temporary object and returning it. Statement 2 executes three tasks:

1)The result object is created and the corresponding constructor is called for initialization;

2)Call the copy constructor to copy the result to the external storage unit that saves the returned value.

3)Result The Destructor called at the end of the function is destroyed.

Note:Create a temporary object and return it. The compiler can directly create and initialize the temporary object in an external storage unit, saving the overhead of copying and structure analysis and Improving the efficiency. Internal data type variables do not have constructor and destructor. Creating Temporary variables and returning them does not increase much efficiency, but will make the program more concise. For example:

int x, y;return x + y;  


9,Do not omit the type of the returned value. If the function does not return the value, it should be declared as void. In the standard C language, all functions without type descriptions are automatically processed by int type. This method does not have any advantages, but is easy to be misunderstood as returning the void type. The C ++ language has a strict static security check and does not allow the above situation. Since C ++ programs can call C functions, to avoid confusion, we stipulate that any C/C ++ function must have a return value type.


10,If the parameter is a pointer and only used for input, you should add const before the type to prevent the memory unit pointed to by the pointer from being accidentally modified in the function body. If the input parameter is passed as a value, use the "const &" method to transfer the object, because the creation and destruction of the reference will not call the object's constructor and destructor, thus improving efficiency.


Statements in C else are ignored.

In
Scanf ("% c", & c1 );
Getchar ();
That is:
Int main () {char c1, c2 = 'a'; printf ("are you worried? \ N "); printf (" \ n "); printf (" A has B not \ n "); scanf (" % c ", & c1); getchar (); system ("cls"); if (c1! = 'A') {printf ("What do you worry about. \ N ");} else {printf (" can you solve it? \ N "); printf (" A can B cannot \ n "); scanf (" % c ", & c2); system (" cls "); if (c2 = 'A') {printf ("What do you worry about. \ N ");} else {printf (" What do you worry about. \ N ") ;}} return 0 ;}

The details that are easy to be ignored in the Water Margin lie at 20 points!

Watch TV on your own
 

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.