"C Expert Programming" notes (ii) language characteristics of--c language

Source: Internet
Author: User

1. A piece of code, the behavior of the first execution is different from the subsequent execution:

1Generate_initializer (Char*string)2 {3     Static CharSeparator =' ';4printf"%c%s \ n", separator,string);5Separator =',';6}

Can be used to automatically generate lists.

2. The visibility of functions

1 /*  * /2 extern/* */3 4 Static /* */

3. This statement p = N * sizeof * q; How many multiplication sign are there? One or two?

The answer is one. Because sizeof's operand is a type name, both sides must be bracketed, but if the operand is a variable, then parentheses are not necessary.

What about this statement: Apple = sizeof (int) * p;

is the length of int type multiplied by P, or?

4. Priority issues

*P.F is * (P.F), p takes the F offset as a pointer, and then dereference.

int *ap[] is an array of int * (ap[]), pointers.

int *FP () is an int * (FP ()), function pointer.

Val & Mask! = 0 is val & (Mask! = 0).

c = getchar () = EOF is C = (GetChar ()! = EOF)

MSB << 4 + lsb is MSB << (4 + LSB)

i = 1, 2 is (i = 1), 2

5. Remember that the priorities and the binding rules tell you which symbols make up a group of people, and that the order in which they are calculated is always undefined. In this expression:

x = f () + g () * H ();

Multiplication is higher than the addition priority, but the order of calls for f (), g (), and H () is undefined.

6. The Get () function does not check the number of characters that are read, which can easily cause a buffer overflow. So we should completely replace the gets () with fgets ().

Char *fgets (char * restrict S, int n, FILE * restrict stream);

7. ANSI C rules The compiler lexical analysis to take the maximal Munch strategy strategy, select the maximum character sequence can be composed of the scheme.

So z = y+++x is interpreted as Z = y++ + x;

So z = y+++++x? It is interpreted as Z = y++ + + +x, which throws a compilation error. Since y++ returns an rvalue, the auto-increment operation supports only the Lvalue value.

8. The author gives

A//*

*/b

This example says that A/b is represented in the C language, and a is represented in C + +.

However, the new standard ANSI C has been//treated as a C-line comment symbol. So this example is no longer applicable.

9. Returns a string of several scenarios:

Returns a pointer to a string constant (cannot be overwritten)

Using an array of global declarations; (wasting space)

Use static arrays (non-reentrant, and also space-consuming)

Explicitly dynamically allocating memory within a function to save the returned value; (Programmers must reclaim memory)

Requires the caller to allocate memory to hold the return value of the function, specifying the size of the buffer (like fgets ()).

"C Expert Programming" notes (ii) language characteristics of--c language

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.