Xiyoulinux group 17.18.19 satisfied that the new title summarizes knowledge

Satisfied that the new three-year title main points are summarized


Main points

  1. The return value of printf
  2. sizeof operator escape characters in the character string (\)
  3. The static keyword
  4. const keyword
  5. Implicit type conversion takes place automatically during operation of the different types of data
  6. Byte alignment structure
  7. The size of the end
  8. And a two-dimensional array pointer arithmetic
  9. C language source program from an executable program to four steps
  10. The difference between sizeof and strlen
  11. Function pointers and function pointers function returns

The return value of 1.printf

To this section of code as an example

int main(int argc, char *argv[])
{
int a = 10, b = 20, c = 30;
printf("%d\n", printf("%d ", a+b+c));
return 0;
}

Look closely, this special program in place is a printf function Riga printf function. So, what this program will output it?
The answer is 603
let us interpret what
the first step: first printf output is 60, it is well understood.
The second cloth: the second printf output of 3 Why is it? This is becauseprintf function is the return value, it returns an int value representing the number of characters to be printedTherefore returns 3. (60 have two characters with a space)

2.sizeof operator with an escape character string (\)

int main(int argc, char *argv[])
{
int t = 4;
printf("%lu\n", sizeof(t--));
printf("%d\n", t);
printf("%lu\n", sizeof("ab c\nt\012\xa1*2"));
printf("%lu\n",strlen("ab c\nt\012\xa1*2"));
return 0;
}

The output of this code is 441110, respectively.
becausesizeof operator functions have been compiled. So after t sizeof parentheses decrementing operation as invalid, so the first two are the output 4.
The end of the string ab c \ nt \ 012 \ xa1 * 2 reason why length is 10All ASCII codes can be represented by "\" plus the number (usually 8 hexadecimal digits). And C is defined in a number of letters before adding "\" to indicate that the common ASCII characters can not be displayed, such as \ 0, \ t, \ n and so on, it is called the escape character, because behind the character, is not it could have been ASCII characters mean. In \ If after three numbers are represented by the eight digit hexadecimal numbers, followed by a combination of x and the two-character hexadecimal digits to represent a character, For example, \ 101 is represented as A, \ x41 also denoted A.

3.static keyword

There are three static language usage c
1.static function type function name (parameter list function) ......} {
This usage can be defined inside a function, the function is limited to the scope of the internal source files only.
The advantage of using internal function is: when different people write different functions, do not worry about their own definition of the function, whether of the same name and other file function, because of the same name does not matter.

2.static global variables
This usage can define static global variables, and the first usage of similar effect, the scope is limited to source files.
The benefits are not to worry with the same name.

3.static local variables
This usage can define a static local variable, static variable is initialized only once, and the scope of local variables of the same scope, but global variables with the same lifetime.

The last three static effect of doing one sentence summary. The first static main function is hidden, and secondly because static variables stored in static memory, so it has the persistence and the default value of 0.

4.const keyword

const char *p;
char const *p;
char *const p;

It means the above three expressions 1 and 2 are the same, meaning a value indicating pointer variable p can not be changed.
Equation 3 means that the value of the pointer variable p can not be changed, i.e., a value of the pointer variable p value const.

We can understand * p may be expressed as variable values ​​p, preceded by const, the value const * p is a value that is variable is referred to p const. const added before the pointer p, p has a value indicates a const value, i.e., p can only point variable is currently pointing.

Implicit type conversion takes place automatically during operation of different types of data 5

unsigned int a = 10;
int b = -20;
if (a + b > 0)
{
		
printf("a+b = %d\n", a+b);
}
else
{
		
printf("a = %d b = %d\n", a, b);
}

Code output is above a + b = -10, the reason why there is such output has occurred implicit type conversion automatically because different types of data during operation.
Implicit automatic conversion rules
1, when performing the arithmetic operation, low type (short bytes) can be converted to a high type (byte length); for example: the conversion into a double int type, int converted to type unsigned int, char to int type and the like;
2, assignment expression, the expression on the right of the equal sign of the value of the type automatically converted implicitly to the left of the variable type, and assigned to it;
3, the function call, the value of the argument passed to the parameter, the system first type is automatically converted implicitly the value of the argument type for the parameter, and then assigned to the parameter;
4, has the function return value, the system will first be implicitly automatically return value of the expression casts as a function return type, and then assigned to the called function returns;

6. The structure of the byte alignment

struct icd
{	
	int x;
	char y;
	double z;
}struct idc
{	
	int x; 
	double y; 
	char z;
};

Byte size occupied by two or more code structures 16 and 24 respectively.
Herein involves the structure in memory byte alignment and byte-aligned specific compiler implementation details related to, but in general, to meet three criteria:
1. The structure of the first address of the variable to be divisible by its broadest size basic types of members;
each member 2. The structure of the first address offset with respect to the structure members are multiples of the size, if necessary to compile will add padding bytes between the members.
3. The total size of the structure is a structure type substantially an integral multiple of the widest member of size, if necessary, the compiler adds padding after the last one member.

7. Size end

Big-endian mode, refers to data stored in the high byte of the low address memory, the low byte data stored in the high address memory, such a bit pattern is stored as a string similar to the data processing sequence: address from small to large increases, while data from the high to the low discharge; this is consistent with our reading habits.
Little-endian mode, refers to the high byte stored in high address memory, the low byte data stored in the lower memory address, this data storage mode and the low address bits right effectively combined, high portion of the right to a high value, low weight low address part.

Following storage for 0x11223344
Here Insert Picture Description

8. A two-dimensional array and the pointer arithmetic

Here Insert Picture Description
It can be seen from the figure, although the values of these three expressions are the same in value, but after they were +1 for calculation, the resulting value is completely different.
We know that the pointer is a value variable address, so we can put these values that are pointer values, and & a + 1, & a [ 0] + 1, & a [0] [0] +1 may be considered as a pointer for plus one operation.Pointer plus one unit operation rule is added to the value of the pointer variable is a pointer, Int array such as a pointer p to point to the first element of the array, the value of pointer p is the address of the first variable int array, the p + 1 is the result of the second array of addresses int variables.
Let us explain a knowledge,Malloc period of one-dimensional array can be assigned to a memory pointer is then implemented, and may be implemented in a two dimensional array manner pointer is a pointer to a two pointers, a pointer to point to a plurality of elements. Therefore, we can pointer the way to understand the array.

&a

A is a two-dimensional array, it can be understood as a pointer to a two, and the two & a is the pointer address, may be understood as a pointer to a value that is three pointers. Thus the above-described type further example & a value of int ***, it refers to the type of the variable int **, so & a + 1 is the next address int **, i.e. the address after the entire two-dimensional array.

&a[0]

The same, a [0] is the first line of a two-dimensional array, can be understood as a pointer to one, so & a [0] is a pointer to the value of its two pointers, the type & a [0] value is int **, the variable type is referred to int *, so & a [0] +1 int * is the next address, i.e. the address of the second row of the two-dimensional array.

&a[0][0]

a [0] [0] is an int type element, & a [0] [0] is a pointer to it a pointer value, & a [0] [0] value is of type int *, referring to variables types are int, & a [0] [0] +1 is the next int address, i.e. the first row address of the second two-dimensional array variables.

9.C from the source language into four steps executable program

1. Pre-treatment
at this stage,All statements in the source code pre-processed, for example, the content file contains #include statements replace the statement itself, all macros are expanded defined, according to # ifdef, # if statement condition is satisfied and the like corresponding to portion choice, pre after the processing source code statements no longer contains any pre-generated files .i.
2. Compile
this stage,Compiler source code for lexical analysis, syntax analysis, optimization and other operations, final assembly code generated .s file. This whole process is the most important step, therefore, often the whole process is called compilation.
3. The compilation of
this stage useAssembler for assembly code, and generates a machine language code, stored in a suffix object file .o.
4. Links
main content link is toMutual references between various modules partial deal, the individual object files to generate an executable .exe file

The difference 10.sizeof and strlen

int main(int argc, char *argv[])
{
printf("%lu\n", sizeof("abc"));
printf("%lu\n",strlen("abc"));
return 0;
}

The output value of the above code 3 and 4 respectively.
The return value is the byte size sizeof string, Are a string '\ 0' end (the end of the string will automatically add '\ 0'), so the byte size of the string should be a string length plus trailing '\ 0', i.e. string length plus one.
strlen return value is the length of the stringAnd therefore does not add trailing '\ 0'.

11. The function returns a pointer to the function pointer function

Let's take a look at the function pointer.
Function pointer is a pointer to a function, defined by the following general form
Type name (variable name * pointer) ();
for example: int (* p) (int a);
i.e., pointer p pointing to a parameter contains an int, int and returns a value of the function value.

size_t q(size_t b)
{
return b;
}
size_t (*p(char *str))(size_t a)
{
printf("%s\n", str);
return q;
}
int main(int argc, char *argv[])
{
char str[] = "XiyouLinuxGroup";
printf("%lu\n", p(str)(strlen(str)));
return 0;
}

Let us analyze the above procedure
(1) program to initialize an array of characters str.
(2) We first analyze the followingsize_t (* p (char * str)) (size_t a) this function, which is a function that returns a function pointer, this may be more difficult to understand, we first seize p, because the operator () * higher priority than, so first with p (char * str). Function p (char * str) containing a char * function type parameters, return value of this function is rather special, it's return type size_t (*) (size_t a), i.e. a function pointer that points size_t parameter contains.
(3) function call, and to call the function p str is argument, the function returns a pointer to the function p q pointer, and then call the function q q by pointing a pointer to the function, after the call is completed to print the return value.

Released four original articles · won praise 3 · Views 179

Guess you like

Origin blog.csdn.net/humblehunger/article/details/104157623