Here are some common C language errors, can be used as a C language to learn notes, when needed can look back.
1. About "+ +"
#includeint Main () {int a,b,cd;a=10;b=a++;c=++a;d=10*a++; printf ("a=%d b=%d c=%d d=%d\n", a,b,c,d); return 0;}
About the self-added decrement operator used in the sentence can be summed up: a++ first Use after the addition, ++a first self-added and then quoted. This makes it easy to produce results:
[Email protected]:~/code$./testa=13 b=10 c=12 d=120
2. Swap variables
The first method, the middle variable method, shows the second, without adding an intermediate variable:
int a=1,b=2;a=a+b;b=a-b;a=a-b;
3.char* str= "Hello World"; sizeof (STR) =12,strlen (str) =11. Because the strlen function does not include the end of the string as a string length ' \ '.
4.char A[10];int Len=strlen (a); Len is not equal to 10, it should be noted that strlen asks for the total number of characters from the first element of the string to ' + ' (does not contain ' s '), and this array is not initialized. Although syntactically correct, the usage is wrong. If you change to sizeof (a), the result is 10.
What does 5.int (*s[10]) (int) mean?
This statement looks complicated, but we'll analyze it step by step. First S[10] is an array, *s[10] stating that his element is a pointer. What kind of pointers? As a whole, this statement: Int (*) (int), not much more a function pointer. So further, s[10] is an array of pointers with 10 elements, each pointing to a function, and the function has an int parameter and returns the value of type int. This looks like a very troublesome, soon is an array of pointers, why so troublesome. For good understanding, we can look at this: Int (*) (int) s[10].
Similar to the array pointers, we can also understand this so as to deepen the memory. For example, int (*s) [10];s is a pointer to an array, so defining a pointer is hard to understand. So let's not be fooled by what he looks like: Int (*) [ten] s; this conforms to the rule defined by the general pointer.
6. The following statement:
char* dest= "Hello,world";
char* src= "Hello";
Puts (strcpy (DEST,SRC));
This is wrong because dest points to a string constant and cannot modify one of its character values. In fact, the correct wording should be the const char* dest= "Hello,world", but in general no const compile time will not be wrong. But if dest[2]= ' a '; then it will go wrong, even if the sentence is grammatically correct. if Char a[100]; char* dest=a; Because the elements in the array are variables that can be modified, they can be printed successfully. If you have the following statement: Char str[100]= "Hello,world", it is completely correct, because it is only initialized with the Hello world.
7. To enter the following string: Edsionte is a goog boy. It is not possible to use the scanf function. Because the scanf function takes the ENTER key and the SPACEBAR as a string to enter the end of the flag. The gets function only takes the return key as the Terminator for the string input. You can therefore use the GET function to receive a string containing the space bar.
8. Macro definitions and precompilation are not part of the C language statement; The struct definition belongs to the C language statement and should end with a semicolon.
9. A shared body can define several different types of member variables, but only one variable at a time. That is, these variables use a memory space in common, and the size of the memory space is determined by the longest-length variables in these variables.
10. What does a,&a,&a[0],a+1,&a+1,&a[0]+1 represent for an array of int a[5]?
A:A is the name of this array, which represents the first address of the first element of the array, which is the first address of a[0].
&a: Is the first address of the array. Note If you print the values of a and &a, the two are the same, but the meanings are different.
The first address of &a[0]:a[0], equivalent to a.
A+1: The address of the next element in the array, that is, a[1].
&a+1: This is a pointer to the next array, which is an integer array of length 5.
&a[0]+1: Equivalent to a+1.
11. In a typical 32-bit machine, the pointer type is 4 bytes, even if the pointer is pointing to a character, because the pointer is still the address. For an array of int a[10], then sizeof (a) =10*4, but if p is a pointer to this array, then sizeof (p) is 4.
12. Adding an integer to a pointer and a binary representation of the pointer with the same integer meaning is different. For example int a[10];int *p=a, then p+1 points to the second element of this array, not to add 1 to the first address of the a[0] element.
13. Define a variable of an enumeration type:
enum
return_value{
RET_TURE;
RET_FALSE;
RET_RANDOM;}
Each enumerated constant in an enumeration type is an shaping constant represented by an identifier. Each enumeration constant in the enumerated type above is not assigned a value, but the default value is 0,1,2.
C Language Notebook