打印
[文档下载]

这个不错,介绍C语言的,还很牛X

[复制链接]
1041|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
um002-c.pdf (886.96 KB)



沙发
yiyigirl2014|  楼主 | 2016-11-20 23:22 | 只看该作者
数据类型

使用特权

评论回复
板凳
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 */
}

使用特权

评论回复
5
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 */
};

使用特权

评论回复
6
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];

使用特权

评论回复
7
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';


使用特权

评论回复
8
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 */

使用特权

评论回复
9
yiyigirl2014|  楼主 | 2016-11-20 23:34 | 只看该作者
Volatile variables 类型
这个类型编译器就不会忽略了

使用特权

评论回复
10
yiyigirl2014|  楼主 | 2016-11-20 23:37 | 只看该作者
指针类型
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>
*a_ptr = 5; /* a gets the value 5 */
int32_t b = *a_ptr; /* declares a new variable which
gets the value of a, in this
case b gets the value 5 */
int *ptr1; /* Regular pointer pointing to an int */
const int *ptr2; /* Regular pointer pointing to a constant int */
int *const ptr3; /* Constant pointer pointing to an int */
const int *const ptr4; /* Constant pointer pointing to a constant int*/
data *a_ptr; /* declares a pointer to a
variable of type data */
(*a_ptr).x = 3; /* a gets the value 3. This is
the cumbersome way to do it. */
a_ptr->x = 4; /* a gets the value 4. This is the
common way to do it. */
int32_t array[5];
int32_t *pointer;
pointer = &array[0];


使用特权

评论回复
11
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);

使用特权

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


使用特权

评论回复
13
gaoyang9992006| | 2016-11-21 10:07 | 只看该作者
看帖的代码很不错,下载看看具体什么。

使用特权

评论回复
14
dongnanxibei| | 2016-11-25 19:55 | 只看该作者
内容很简洁又很充实,是本不错的复习资料。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

208

主题

3518

帖子

10

粉丝