看框架总结

来源:互联网 发布:上海知楚仪器 编辑:程序博客网 时间:2024/06/05 20:46

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

原创粉丝点击