define
define的基礎用法: 將標識符定義為后面的常量
#define 標識符 常量 //最后沒有分號
- C語言中以#開頭的均為預處理命令,且以#開頭的都是宏定義,用宏名表示一個常量,好處是"方便程序的修改"
#define NUM 5
位運算符
<< 二進制左移運算符,將一個運算對象的各二進制位全部左移若干位(左邊的二進制位丟棄,右邊補0)。如60(00111100) << 2 得到240
|或運算符
按二進制位進行或運算
- 輸入輸出規(guī)范
c語言中符合%#的意思是帶格式輸出
比如,%#x的意思是在輸出前面加上0x,%#b的意思是在輸出前面加上0b,c語言中的%#x意思是帶格式0x的16進制輸出。
進階用法
- glob庫函數(shù)的使用
如果glob返回的是非零值,則說明路徑不存在
glob("path",0, NULL, &buf);// 從path獲取配置文件并讀取到buf中
#include <stdio.h>
#include <glob.h>
int main(int argc, const char *argv[])
{
glob_t buf;
int i;
i = glob("/Users/panlanlan/Documents/project/baidu/c/test_bvrouter/1",0, NULL, &buf);
printf("%d\n", i);
for(i=0; i < buf.gl_pathc; i++)
{
printf("buf.gl_pathv[%d]= %s \n", i, (buf.gl_pathv[i]));
}
globfree(&buf);
return 0;
}
- 獲取當前路徑
getcwd()函數(shù)的使用
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(void)
{
char *path = NULL;
path = getcwd(NULL,0);
puts(path);
free(path);
return 0;
}
- GCC diagnostic push的使用
test1函數(shù)就是被忽略的,main()函數(shù)只輸出test2的結(jié)果
/************************************************************************/
//記錄當前的診斷狀態(tài)
#pragma GCC diagnostic push
//關(guān)閉警告,診斷忽略沒有返回值
#pragma GCC diagnostic ignored "-Wreturn-type"
int test1(void)
{
return;
}
//恢復到之前的診斷狀態(tài)
#pragma GCC diagnostic pop
/************************************************************************/
int test2(void)
{
return;
}
int main(int argc, char* argv[])
{
test1();
test2();
return 0;
}
- printf()和fprintf()的區(qū)別
fprintf是將字符輸出到流(文件)的,printf是輸出到標準設備(stdout)的。
int fprintf( FILE *stream, const char *format, [ argument ]...);
int printf( const char *format [, argument]... );
- strdup():復制字符串
參考 - chdir():改變當前的工作目錄
參考 - container_of(ptr,type,member)的理解
container_of(ptr,type,member)指由結(jié)構(gòu)體中元素member和member在結(jié)構(gòu)體中的地址找到結(jié)構(gòu)體的首地址
#define offset_of(type,member) ((int)&(((type *)0)->member))
#define container_of(ptr,type,member) ({ \
const typeof(((type*)0)->member) *__mptr = ptr; \
(type *)((char *)__mptr - offset_of(type,member)); \
})
struct mytest{
char i;
int j;
char *k;
};
int main(){
struct mytest temp;
struct mytest *p = &temp;
p->i = 100;
p->j = 10;
printf("&temp = %p\n",&temp);
printf("&temp.k = %p\n",&temp.k);
printf("&((struct mytest *)0)->k = %d\n",((int)&((struct mytest *)0)->k));
printf("&temp = %p \n",container_of(&temp.j,struct mytest,j));
printf("&temp = %p \n",container_of(&temp.k,struct mytest,k));
printf("======test=====\n");
printf("&temp = %p \n", &p->i);
printf("&temp = %p \n", &(p->i));
return 0;}
輸出結(jié)果
&temp = 0x7ffee4018508 //結(jié)構(gòu)體首地址
&temp.k = 0x7ffee4018510 // 結(jié)構(gòu)體中元素k所在的地址,為了對齊字節(jié),每個元素占4個字節(jié)
&((struct mytest *)0)->k = 8 //結(jié)構(gòu)體中從首地址到k元素的長度
&temp = 0x7ffee4018508 //container_of(&temp.j,struct mytest,j)
&temp = 0x7ffee4018508 //container_of(&temp.k,struct mytest,k)
======test=====
&temp = 0x7ffee4018508
&temp = 0x7ffee4018508
還涉及一個運算符優(yōu)先級的問題,順手也做了小測試
&p->i
- &和->的優(yōu)先級高低問題
通過上述測試,發(fā)現(xiàn)->優(yōu)先級高于&
- 高階用法
- 對于uint16_t,uint32_t,uint64_t的使用,在程序中加入typedef unsigned __int16 uint16_t;typedef unsigned __int32 uint32_t;typedef unsigned __int64 uint64_t;的輸入輸出形式:
https://blog.csdn.net/u010464679/article/details/45671705
malloc和free的使用
https://www.cxybb.com/article/weixin_43800761/107116376