[文档下载] 这个不错,介绍C语言的,还很牛X

[复制链接]
1226|13
 楼主| yiyigirl2014 发表于 2016-11-20 23:19 | 显示全部楼层 |阅读模式
um002-c.pdf (886.96 KB, 下载次数: 374)



 楼主| yiyigirl2014 发表于 2016-11-20 23:22 | 显示全部楼层
数据类型
1.png
 楼主| yiyigirl2014 发表于 2016-11-20 23:23 | 显示全部楼层
声明和初始化
float length; /* declares the variable "length"
as a floating number */
length = 20.3; /* initialize "length" by giving
it the value 20.3 */

{
int a = 1; /* a gets the value 1 */
int b = 2; /* b gets the value 2 */
{
int c = a + b; /* a has the value 1, b has the value 2,
c gets the value 3 */
a = 5; /* a gets the value 5 */
int d = a + b; /* a has the value 5, b has the value 2,
d gets the value 7 */
}
}

 楼主| yiyigirl2014 发表于 2016-11-20 23:24 | 显示全部楼层
外部变量
External variables
File 1:
int a = 2; /* declares and initialize a variable a */
File 2:
extern int a; /* explicit declaration of a */
int main(){
printf("%d\n",a); /* a can now be used as a
global variable in File 2 */
}

 楼主| yiyigirl2014 发表于 2016-11-20 23:27 | 显示全部楼层
Static variables
static int x = 0;

Constant variables

const float pi = 3.14;

Enum


enum color{
red; /* red is given the value 0 by default */
blue; /* blue gets the value 1 */
green; /* green gets the value 2 */
yellow = 5; /* yellow gets the specified value 5 */
};

 楼主| yiyigirl2014 发表于 2016-11-20 23:28 | 显示全部楼层
Arrays 数组

int32_t A [3]; /* declares an array of size 3 */
A[0] = 5; /* initialize the three elements
A[1] = 7; of the array. Could also have written
A[2] = 1; int32_t A = {5,7,1}; */
A[2]; /* gets access to the second value of A */


char message [100] = "Hello World!";
/* allocates memory to 100 data characters. Remember to have
enough space for the null character! */


三维数组
int B [3][5][3];
 楼主| yiyigirl2014 发表于 2016-11-20 23:29 | 显示全部楼层
Structs 结构体


struct data{
int x;
int A[3];
char y;
};
struct data a; /* declares a new variable
of type data */
a.x = 3;
a.A[0]= 1; /* initialize the three member
a.A[1]= 3; variables of a */
a.A[2]= 4;
a.y = 'B';


 楼主| yiyigirl2014 发表于 2016-11-20 23:31 | 显示全部楼层
Typedef 自定义类型
如果想定义个类型为秒类型怎么做呢?
float time;

typedef float sec;
sec time; /*declares a variable time of type sec */

这样我们就可以用sec作为一个变量类型了。
或如下结构体类型,定义了一个类型名为data的类型的变量a.
typedef struct{
int x;
int A[3];
char y;
} data;
data a; /* declares a new variable of type data */

 楼主| yiyigirl2014 发表于 2016-11-20 23:34 | 显示全部楼层
Volatile variables 类型
这个类型编译器就不会忽略了
 楼主| yiyigirl2014 发表于 2016-11-20 23:37 | 显示全部楼层
指针类型
  1. int32_t a;  <span style="line-height: 1.5;">/* declaration af a variable a.</span><span style="line-height: 1.5;">Allocates space in memory. */</span>
  1. *a_ptr = 5; /* a gets the value 5 */
  2. int32_t b = *a_ptr; /* declares a new variable which
  3. gets the value of a, in this
  4. case b gets the value 5 */
  1. int *ptr1; /* Regular pointer pointing to an int */
  2. const int *ptr2; /* Regular pointer pointing to a constant int */
  3. int *const ptr3; /* Constant pointer pointing to an int */
  4. const int *const ptr4; /* Constant pointer pointing to a constant int*/
  1. data *a_ptr; /* declares a pointer to a
  2. variable of type data */
  3. (*a_ptr).x = 3; /* a gets the value 3. This is
  4. the cumbersome way to do it. */
  5. a_ptr->x = 4; /* a gets the value 4. This is the
  6. common way to do it. */
  1. int32_t array[5];
  2. int32_t *pointer;
  3. pointer = &array[0];


 楼主| yiyigirl2014 发表于 2016-11-20 23:39 | 显示全部楼层
int a; /* BSS */
int b = 3; /* DATA */
int main(){
int i; /* Stack */
char *ptr = malloc(7); /* The char pointer is put on the stack
and the allocated memory is put on
the heap.*/
}

开辟和释放内存
d_array = (type *) malloc (numberOfElements * sizeof(type));
free (d_array);

 楼主| yiyigirl2014 发表于 2016-11-20 23:48 | 显示全部楼层
      内联函数是指用inline关键字修饰的函数。在类内定义的函数被默认成内联函数。定义内联函数从源代码层看,有函数的结构,而在编译后,却不具备函数的性质。内联函数不是在调用时发生控制转移,而是在编译时将函数体嵌入在每一个调用处。编译时,类似宏替换,使用函数体替换调用处的函数名。
  1. inline int sum(int a, int b);
  2. int main(){
  3. c = sum(1,4);
  4. }
  5. inline int sum(int a, int b){
  6. return a + b;
  7. }


gaoyang9992006 发表于 2016-11-21 10:07 | 显示全部楼层
看帖的代码很不错,下载看看具体什么。
dongnanxibei 发表于 2016-11-25 19:55 | 显示全部楼层
内容很简洁又很充实,是本不错的复习资料。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部