Introduction of _c language based on the addendum of C language knowledge point

Source: Internet
Author: User
Tags arrays

C is a long time to use, but it's hard to say that every point in C is very thorough. Although C is not as complex as C + +, but there are many Shing: not how difficult they are, but that they usually do not use much, or with the first intuition, or the lack of experience in the beginning of learning is not deep at all do not remember.
The following things may come from "c expert programming" or the network. The recent discovery of the basics of classic books is often new, for the reason that there may be two:
1, with the growth of their own experience, your understanding may be different, the way of thinking will change, and the things will naturally be new things.
2. Early experience is limited and some points may not be fully understood at all. Now you can understand more profoundly.
This aspect of the book, such as "Code Encyclopedia", a few days ago turned, and have different understanding.
To get to the point:
1. Signed and unsigned comparisons:
printf ("%d\n", sizeof (' A ')): The printed value is 4 (or length of int) instead of 1. Because C has a type elevation, it first promotes ' A ' to the int type and then passes it to sizeof. The arguments in the expression are promoted to int or double, and then the value of the specified type is obtained after the operation and then cropping.
if ( -1 <= sizeof (int)): The return value of sizeof is unsigned int,-1 is converted to Unsignedint by type and then compared ...
This involves type elevation, implicit type conversions. It takes place in an expression and also occurs in a function entry.
2, enumeration in memory size: four bytes.
3, the local variable is also byte-aligned:
e_t G;
e_t F;
e_t E = false;
Char C1;
Char C2;
int i1;
Char C3;
int i2;
printf ("%p,%p,%p,%p,%p,%p,%p,%p\n", &g, &f, &e, &c1, &AMP;C2, &i1, &AMP;C3, &AMP;I2);



--said to be a padded bit.
4, in the macro definition of # and # #: #的功能是将其后面的宏参数进行字符串化操作 (stringfication), simply refers to its reference to the macro variable by replacing it in its left and right after each add a double quotation mark.
and # #被称为连接符 (concatenator), used to connect two token to a token.
5, floating point numbers can not be equal to comparison.
6, void foobar2 () means that the number of functions into the parameters are multiple, uncertain. If the expression does not occur, it should be: void Foobar2 (void)
7. Global variables are initialized to 0, but local variables in the stack are not initialized.
8, inline functions and macros: inline functions are real functions, but it is optimized at compile time.
9, int a[5];    printf ("%x\n", a);    printf ("%x\n", a+1);    printf ("%x\n", &a); printf ("%x\n", &a+1);
The last one, &a+1,&a represents an array, so you should increase the size of the array: 4*5 bytes.
10, 10U represents a unsigned type of Number 10.
11, shift operation of the priority is relatively low, lower than arithmetic.
12, left-shifted n-bit, equal to multiply with 2 of the n-th side. The right shift is equal to the N-second side of 2.
13. Pointers and Arrays:
1), void Fun (char buf[100])
{
printf ("%d, \ n", sizeof (BUF));
}
The printed value is 4, not 100.
2), in a file char p[10] = "";
In another file, declare: extern char *p;
Then, in the declared file sizeof (p), the answer is 4. That is, sizeof calculates the type of declaration.
3 for the compiler, an array is an address, and a pointer is the address of an address.
4 All array name compilers as function parameters are converted to pointers, in all other cases, the declaration of an array is an array, and the pointer is a pointer.
Rules for arrays and pointers in the same situation:
1, the array name in the expression (unlike the Declaration) is treated by the compiler as a pointer to the first element of the array.
2, subscript is always the same as the offset of the pointer.
3. In the declaration of a function, the array name is treated by the compiler as a pointer to the first element of the array. This operation is done by the compiler. The reason is due to efficiency considerations. Because this is a reference pass rather than a value pass. Value delivery requires a copy. It can also be seen that the sizeof is manipulated in the assembly.
The behavior of arry[-1] is undefined.
Summary:
1) a[i] This form of access to A is always rewritten by the compiler as a form of * (A+i).
2 The pointer is always a pointer, and you can't change it to an array, but you can access it in the form of an array.
3 The array is used as the parameter of the function and is rewritten as a pointer by the compiler.
4 What the pointer and array must match.
14, Declaration and definition: the declaration can be multiple, defined only one. A definition is a special declaration that allocates memory for an object. A declaration is an ordinary declaration that describes an object created elsewhere.
precedence rules for declarations:
A: Starting from his name, read in order of precedence:
B: Priority level:
1. The part of the declaration that is enclosed in parentheses.
2. Suffix operator:
The bracket () representation is a function;
The square brackets [] represent an array;
3, prefix operator: * indicating what the pointer;
4, the const immediately after the variable can not modify the modifier variable, followed by the type point to the things can not be modified.
15, multidimensional array:
A[2][3]:a is an array with two elements. Each element is an array with three elements.
Memory layout: a[0][0],a[0][1],a[0][2],a[1][0] ... The address has been getting bigger.
A multidimensional array, an array of arrays as the function's formal parameters, is transformed into an array pointer, a pointer to a group, and a row pointer. is also a pointer in nature.
16, the structure of the default byte alignment generally meet three criteria:
1 The first address of the structure variable can be divisible by the size of its widest base type member;
2) Each member of the structure, relative to the initial address of the structure of the offset (offset) is a member of the size of the integer multiple times, if necessary, the compiler will be among the members with padding bytes (internal adding);
3 The total size of the structure is an integral multiple of the size of the widest base type member of the structure, and if necessary the compiler will add the padding byte after the last member (trailing padding).

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.