【C/C++学习】C语言基础知识积累

把在C语言学习过程的知识记录下来,积少成多。

1、各种特殊字符

\n 换行 ;\r 输出指针移到当前位置 ;\b 去掉一个前面的字符 ;\t  制表对齐 ;\f 换页符

2、常用函数

menset(char[],0,100);//数组置0

strcat(dis,res);//字符串的拼接

strncat(dis.res,n);//字符串的个数拼接

ssprintf(dis,"format",p1,p2);//字符串的组合

sscanf(dis,"format",p1,p2);//字符串的拆分

atof("3.14");//将string转为int

strcmp(s1,s2);//字符串的比较

字符串的截取

  char*c = "my world";
//char*c1 = strchr(c, 'w');//从开始扫描
//char*c1 = strrchr(c, 'w');//从后面扫描
//char*c1 = strstr(c, "wo");//扫描字符串
   char*temp = c + 4;//从开始移位
  char c1[10];
  strncpy_s(c1, temp, 3);

3、可变参数函数的应用

int sum(int n, ...){
int all=0;
va_list varlist;
va_start(varlist, n);
for (int i = 0; i < n; i++){
all += va_arg(varlist, int);
}
va_end(varlist);
return all;
}

4、宏函数的参数连接

(1)#define callcqyd(NAME) cqyd##NAME()

(2)void cqydSayyes(){
printf("cqydSayyes");
        }

(3)callcqyd(Sayyes);

5、宏函数的定义和使用

(1)#define max(a,b) a>b?a:b

 printf("the max number is:%d\n",max(3,9));

(2)#define Loop(Min,Max,Content) \
          for (int index = Min; index < Max; index++){\
    Content;                                \
          }\

Loop(5, 15, printf("the now num is %d\n",index));

6、宏函数的可变参数

#define Log(Level,Format,...) printf(Level);printf(Format,__VA_ARGS__)
#define Log_E(Format,...) Log("Error:",Format,__VA_ARGS__)
#define Log_W(Format,...) Log("Warn:",Format,__VA_ARGS__)

Log_E("your shouble be:%s and %d ", "happy", 100);
Log_W("your shouble not be:%s and %d ", "bad", 0);

7、指针函数

typedef void(*T)();

void(*p)() = &cqydSayyes;
T p = &cqydSayyes;
p();

8、无类型指针

void*j = malloc(8);
int *q = (int*)j;
printf("%ld", sizeof(q[0]));
free(j);

9、结构体的创建

struct File
{
int size;
char*name;
};
typedef struct File MyF;
typedef struct Person
{
char*name;
int age;
}MyP;

int main(){

    /*MyF file;
file.name = "check";
file.size = 100;
printf("File:%s,%d\n",file.name,file.size );*/
MyP p;
p.age = 12;
p.name = "duzi";
printf("Person:%d,%s\n",p.age,p.name);}

10、结构体指针

MyF *createFile(char*name,int size){
MyF*f = (MyF*)malloc(sizeof(MyF));
f->name = name;
f->size = size;
return f;
}
void deleteFile(MyF*f){
free(f);
}


MyF* myFile = createFile("newFile",100);

printf("File:%s,%d",myFile->name,myFile->size);

deleteFile(myFile);

11、共同体

typedef union _Base{
char c;
int i;
}Base;

        Base b;
b.c = 'a';
//b.i = 64;
printf("%d", sizeof(b));

12、不按字节对齐

typedef struct Person
{
char*name;
int age;

}__attribute__((packed)) MyP;

13、文件操作

(1)、写入

FILE*file=fopen("data.txt","w");
//fputc('A',file);
fputs("jiayou duzi",file);
fclose(file);

(2)、读取

FILE*f=fopen("data.txt","r");
char c[20];
fgets(c,50,f);
 printf("%s",c);

(3)格式化的读取和写入

FILE*f = fopen("my.txt", "w");
int i = 0;
for (i = 0; i < 5; i++) {
fprintf(f, "Item is : %d\n", i);
}
fclose(f);


        FILE*f = fopen("my.txt", "r");
if (f != NULL) {
//char cs[20];
int n;
fscanf(f, "Item is : %d\n", &n);
printf("read is %d", n);
fclose(f);
} else {
puts("wrong");
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值