看框架总结
1、使用函數sprintf過程中產生的錯誤:
#include<stdio.h>
#include<string.h>
int main()
{
??? char *string = "a good student";
??? char?*str;
??? sprintf(str,"%s",string);
??? printf("%s\n",str);
??? return 0;??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
}
結果報錯:no stack,原因沒有對str申請空間,解決方案:
?
#include<stdio.h>
#include<string.h>
int main()
{
??? char *string = "a good student";
??? char str[256];
??? sprintf(str,"%s",string);
??? printf("%s\n",str);
??? return 0;??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
}
運行結果:a good student
?
2、ANSI C預定義的宏__TIME__:獲取編譯時時間
程序:
#include<stdio.h>
int main()
{
???? printf("%s",__TIME__);?????????????????????????????????????????????????????????????????????????????????????????????????????????
}
結果:
15:30:47
?
拓展:
__LINE__:在源代碼中插入當前源代碼行號;
__FILE__:在源文件中插入當前源文件名;
__DATE__:在源文件中插入當前的編譯日期
__TIME__:在源文件中插入當前編譯時間;
__STDC__:當要求程序嚴格遵循ANSI C標準時該標識被賦值為1;
?
?
3、文件操作符lseek()
off_t lseek(int fd, off_t offset, int whence);
返回的是fd到whence之間的偏移量,如果fd一開始被定義為0,whence被定義為SEEK_END,則返回的是文件的長度。
?
4、學習使用ifdef...else...endif
int main()
{
??? #ifdef mood
??????? printf("nice\n");
??? #else
??????? printf("woe");
??? #endif
----
??? #ifdef feagure
??????? printf("beautiful");
??? #endif
??? return 0;
}
//result:
woe
?
5、get_current_dir_name()獲取當前目錄
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define _GNU_SOURCE
int main()
{
??? char **str = (char *)strdup(get_current_dir_name());
??? printf("filename:%s\n",str);
}
//result:
//filename:/home/jeanyu/mywork
?
6、學習左移符號<<的使用
#include<stdio.h>
#include<stdint.h>
int main()
{
????? int8_t a;
????? a = 1<<7;
????? printf("%u\n",1<<8);
????? printf("%d\n",a);
????? return 0;
}
//result
//256
?
7、求換行符、tab符等的ACSII值
#include<stdio.h>
int main()
{
>---printf("%d,%d,%d,%d,%d\n",' ','\r','\t','\n','=');
}
//result
//32,13,9,10,61
總結
- 上一篇: excel应用
- 下一篇: linux多线程和锁