-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C #49
Comments
char name[40];
// 1. wrong
// warning, but 可运行
scanf("%s", &name);
// 2. right, name= char* , already a pointer
scanf("%s", name);
int age;
scanf("%d", &age); |
header reference
|
CFLAGS
|
lib usage guidelibuv# 1. build `libuv.a`
# 2. include libuv header & add libuv.a to srcs
cc -I ./libuv/include my.c libuv.a -o my # |
sizeof
/**
* cc sizeof.c -o sizeof -w
*/
#include <stdio.h>
int main(){
const int days[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
printf("size = %d\n", sizeof days / sizeof days[0]); // 12
printf("equals = %d\n", sizeof days[0] == sizeof(int)); // 1
return 0;
} |
数组 as 函数参数void fn(int *arr);
void fn(int arr[]);
// prototype
void fn(int*);
void fn(int[]); |
constconst int x = 10; // x is a const
const int *px = 10; // *px is a const, px 是指向常量的指针, *px的值不能变
int* const px = 10; // px是常量指针, px 的指向不能变
// 将非常量地址(&x)赋值给指向常量的指针(px)是可行的
int x = 10;
const int *px = &x; // 可行, 因为 px是指向常量的指针, 不能通过指针去修改x
// 将常量数据y, 赋值给普通指针py是不合法的
const int y = 10;
int* py = &y; // 不合法, 因为这样之后可以使用 (*py)去修改y值, 但是y是const值 // 这两种形式一样: *px 为const, 即指向const的指针
const int *px = 10;
int const *px = 10; |
pointersint x = 10;
const int* px = &x; // 取x的地址
const int* py = 10; // 指向10 #include <stdio.h>
int main(){
int x = 10;
const int *px = &x;
printf("px = %lu\n*px = %d", px, *px);
return 0;
}
/*
output:
px = 140734675919640
*px = 10
*/ |
argsint main(int argc, char *argv[]);
// 等同于
int main(int argc, char **argv);
// argv是指向char类型的指针组成的数组
// argv[0] as char* |
Linkage 链接性
int num = 5;
static int num_internal = 10; // internal, 加了static, 本文件内部可见 Storage Duration 存储时期
在文件作用域有效的变量都具有静态存储时期 五种存储类寄存器变量
代码块作用域中的静态存储时期变量在一个函数作用域中的静态变量只初始化一个, 并一直存在: int fn(){
int x = 1;
static int y = 1;
printf("x = %d, y = %d", x++, y++);
}
int main(){
for(int i = 0; i < 3; i++){
fn();
}
return 0;
}
// 输出
// x = 1, y = 1
// x = 1, y = 2
// x = 1, y = 3 外部链接性变量// x.c
int x = 10; // 静态存储时期, 外部链接性
// y.c
extern int x; // 使用extern 表示是在其他文件声明的变量
int y = 20;
void someFn(){
extern int y; // 在一个函数内再次声明 y 是extern的, 纯属画蛇添足, 但要明白可以这样用
} 外部变量只能使用常量表达式来初始化 extern int x;
int y = x + 10; // 不合法 类型限定词
|
malloc & freemallocvoid* mallpc(int size)
int* px = (int*) malloc(sizeof(int)); malloc返回值是void* 类型, 需要手动转为需要的ptr类型 freefree(px); |
structstruct struct_name {
int x,
int y
} 即可定义一个struct, 名为struct_name struct struct_name variable = { 1, 2 };
struct struct_name variable = { .x = 1, .y = 2 }; struct_name 可匿名, 这样
struct struct_name s1,s2;
struct struct_name* ps;
// 使用点号去获取成员
s1.x = s1.y = 10;
// pointer->member
// 指向结构体的指针使用 -> 访问成员
ps = &s1;
ps->x = ps->y = 10; unionunion point {
int x;
double y;
} union同struct, 但是member之间互斥, 同一时间只有一个member能存在, 例如 x, y 不能同时存在, 分配空间时按照占用空间最大的member来分配 |
函数指针char* toUpper(char*); // 一个转换字符串为大写的函数
char* (*pf) (char*); // pf为函数指针
pf = toUpper; // 给函数指针赋值
// 都是可行的
pf("hello world");
(*pf)("hello world"); 结合typedef, 可以定义函数指针的类型 typedef (char* (*S_TO_S)(char*));
char* (S_TO_S fp){
return fp("hello world");
} |
预处理指令
define// 带参数
#define SQUARE(x) (x*x);
// #运算符
// #为替换
#define PRINT(x) printf("#x = %d", x);
PRINT(y); // -> 展开为 printf("y = %d", y);
// ##为连接运算符
#define XNUM(n) x ## n;
XNUM(4) // -> x4 pragma once只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次. 是编译器相关的,有的编译器支持,有的不支持. |
头文件#include <stdio.h>; // 尖括号, 在标准目录中寻找文件
#include "abc.h"; // 双引号, 在当前目录寻找文件, 然后在标准目录中寻找文件 size_thttp://stackoverflow.com/questions/1119370/where-do-i-find-the-definition-of-size-t 说是 |
大小端
os.endianness(); // LE 低字节在低地址处 |
Yeah C.
The text was updated successfully, but these errors were encountered: