Embedded Software Engineer 3 - C language Supplements (1)

1

type of data

  1. Basic type: char short int long float double
    compound types: an array of structures unions
  2. If the data type of a mold, the mold of this example that the variable of the C language. Variables are stored in memory, we need to take up some memory space. Memory for each data type is different on a different machine platforms. (Typically 32-bit CPU) available sizeof operator calculated.

There are signed and unsigned

  1. For other integer number char int type, divided into symbols and unsigned, and for float and double float such, only a signed number, unsigned number no.
  2. Variables are stored in memory. When stored in binary fashion store. For signed and unsigned numbers, the different storage. For type int, unsigned: 4-byte (32-bit) for the entire contents of the stored number; signed number: 32-bit symbol used to keep the highest bit ( 0 for positive, 1 for negative ), the remaining 31 for the stored data.
  3. In absolute values, the range of unsigned represented larger.
  4. For float and doule this floating-point type, they are stored in memory and integer are not the same. Absolutely can not change the access mode of a variable.

Empty type (void)

  1. C language type void, on behalf of any type, not empty meaning. Any type of means that its type is unknown, has not yet specified.
  2. Parameter list function is void: it represents not need to pass parameters to this function call.
  3. Function returns a value of void: indicates that the function does not return a meaningful return value.
  4. * void : any type of pointer. ( Absolutely can not change the access mode of a variable. ) Are fully responsible for their own access type, the compiler can not help you do type checking.

1 details

  1. A byte is eight bits.

  2. strlen : strlen is a function, which is used to calculate the specified string str length, but not including the end character (i.e., a null character).

  3. the sizeof : the sizeof is a unary operator and not a function. Strlen with different functions, which can be an array of the parameter, a pointer, type, object, functions, and so on. Function is guaranteed to accommodate the established maximum target size in bytes.

  4. An array of characters and its two initialization

  1 #include<stdio.h>
  2 int main()
  3 {
  4     char a[] = "abcde"; // 6
  5     char b[] = {'a','b','c','d','e'}; // 5
  6     printf("sizeof(a) = %d,sizeof(b) = %d\n",sizeof(a),sizeof(b));
  7     return 0;
  8 }
  
结果为:sizeof(a) = 6,sizeof(b) = 5

1"abcde"实际上有6个字符,分别为'a','b','c','d','e','\0';
2'\0'这个字符是ASCII码的第一个字符,它的编码值是0'\0'是C语言中定义的字符串的结尾标志。所以当C语言程序中使用"abcde"这种方式初始化时,编译器会自动在字符'e'后面添加一个'\0'。所以变成了6个字符。

pointer

  1. Pointer full name is the pointer variable , and its essence is a variable C language. This variable is special, usually his value will be assigned an address value of a variable, then you can use * p in such a way to indirectly access the variable p points.
  2. int * p; defined pointer variable p, when defining the meaning here is to tell the compiler * p is a pointer variable.
  3. * P = 123; when the pointer variables, * p representative of the pointer variable p points.
  4. Various pointer
指针变量本质是一个变量,指针变量的类型属于指针类型。

1 int *p;// 定义了一个指针类型的变量p,这个p所指向的那个变量是int型。

1 int a;
2 float *p;
3 p = &a;//warning: assignment from incompatible pointer type

各种指针类型和它所指向的变量类型必须匹配,否则结果不可预知。
  1. Preliminary combined with an array of pointers
数组名:做右值时,数组名表示数组的首元素地址,因此可以直接赋值给指针。
1 int a[5];//a 和 &a[0]都表示数组首元素a[0]的地址,而&a表示数组的首地址。
注意:数组首元素的地址和数组的首地址是不同的。前者是数组元素的地址,后者是数组整体的地址。两个含义不同,但数值上是相同的。
1 int a[];
2 int *p;
3 a = p;
//编译报错,因为数组名是个常量,所以不能赋值,数组名不能做左值。
  1. Parameter passing a pointer function is used
1 int add(int a,int b)
//实际调用该函数时,实参将自己拷贝一份,并将拷贝传递给形参进行运算,实参自己实际是不参与的。所以,在函数中,是没法改变实参本身的。

  1 #include<stdio.h>
  2 
  3 int swap(int &a,int &b)
  4 {
  5     int t;
  6     t = a;
  7     a = b;
  8     b = t;
  9 }
 10 int main()
 11 {
 12     int a = 2,b = 3;
 13     swap(a,b);
 14     printf("a = %d\n",a);
 15     printf("b = %d\n",b);
 16     return 0;
 17 }
 
  输出:	a = 2
	 	b = 3
 参数是a,b的地址
  1 #include<stdio.h>
  2 
  3 int swap_pointer(int *a,int *b)
  4 {
  5     int t;
  6     t = *a;
  7     *a = *b;
  8     *b = t;
  9 }
 10 int main()
 11 {
 12     int a = 2,b = 3;
 13     swap_pointer(&a,&b);
 14     printf("a = %d\n",a);
 15     printf("b = %d\n",b);
 16     return 0;
 17 }
 
输出:	a = 3
		b = 2

Local and global variables

  1. While not defined when initialized, the value of the local variable is random, the value 0 is the default global variables.
  2. The use of global variables with file scope (entire file) , and only local variable code block (in {}) scope .
  3. The life cycle, global variables are born in the initialization phase before the program starts running, the entire program ends quit when it died; and local variables born when entering the code where a local variable, died when the code block exits.
  4. Dispensing position, global variables are allocated in the data segment , the local variables in a stack on.
  5. Data segments: the number is stored, the presence of the global variable data section. Code segment: the code is stored, typically read-only. Stack (stack): last out, there is a local variable stack.

Local variables

  1. The called function is the end of the main function calls, ordinary local variables (auto) memory were released; ** static local variable (static) ** memory is not released, and global variables are very similar.
  2. register (register) the type of local variables and the general performance of local variables, referred to as a C language variable fastest . C language will register variables into registers run (common variable is in memory), so register variable access speed quickly. However, it is limited, because the number of registers is limited, and secondly there is a limit register variables in data type, for example, can not define a variable of type double register. Generally only in the kernel or boot code, you need to repeatedly use the same variable will use register variables.

Global Variables

  1. Define and initialize global variables occurs before the main function is to run.
  2. General global variables can be used in individual files, it can be seen in another .c file in the project, so make sure the variable name must be unique. Static global variable is to solve the problem of the same name. It tells the compiler that this variable only in the current of this document use within, so do not worry about duplicate names.

Variable cross-reference file

  1. Use keywords extern declare a variable cross-file

constant

  1. #define N 20
  2. const : const pointers and combined, there are four forms:
    const int * P: P is a pointer, a pointer to an int. p data points is a constant.
    int const * p: p is a pointer, a pointer to an int. p data points is a constant.
    int * const p: p is a pointer, a pointer to an int. p itself is a constant, p is the variable data points.
    const int * const p: p is a pointer, a pointer to an int. p itself is a constant, p data points is a constant.

The introduction of header files

  1. #include <>: contains the system comes to a header file.
  2. #include "": to include the header files in the current directory.
  3. Prevent duplicate file header contains
#ifndef _A_H_
#define _A_H_


#endif
Published 64 original articles · won praise 27 · views 5797

Guess you like

Origin blog.csdn.net/qq_43675371/article/details/104400353