#include<stdio.h>#include<time.h>#include<string.h>#include<malloc.h>typedeflonglong ll;// use an identifier replace a type#define ONE 1 // use an identifier repalce a value//typedef 是由编译器执行解释的,#define 语句是由预编译器进行处理的。voidcoutdown(){staticint count=5;printf("count = %d\n",count--);}//return pointerint*getnum(int n){int* num =(int*)malloc(n*sizeof(int));//另外,C 语言不支持在调用函数时返回局部变量的地址,除非定义局部变量为 static 变量int i;for(i=0;i<n;i++){
num[i]=rand()%10;}return num;}// function pointerintsum(int a,int b){return a+b;}intsub(int a,int b){return a-b;}intsumOfArray(int arr[],int len,int(*add)(int,int)){int s=0;int i;for( i=0;i<len;i++){
s=add(s,arr[i]);}printf("%d\n",s);}// use struct // define the struct and declare the varstruct node{int a;int value;} n1,n2;// after define the struct and declare the varstruct node1{int a;int value;};struct node1 n3;// define the strcut as a typetypedefstruct{char name[10];int age;} student;// -- use union 分配的空间为最大长度的类型,而且每个变量的起始位置都一样,所以,这个适合// 给多个变量共享内存使用,因为他们不同时用union data{int a;float b;char c[21];} udata;// 位域typedefstruct{// type name width width must less than the len of sizeof(type) unsignedint first:1;unsignedint second:1;// 定义32个这样的变量,开的空间都是4字节 } three;// 不使用位域typedefstruct{unsignedint first;char second;// 也会采用对齐 } three1;intmain(){// ----static 无论是修饰局部还是全局,每次调用不会销毁,而是一直共享 int i;for(i=0;i<5;i++){coutdown();}// ----use labelprintf("hhh\n");// exe in order once time
label1:printf("hhh1\n");
label2:printf("hhh2\n");
label3:printf("hhh3\n");// goto label2;//---- enumenum color{
red=1,green,blue
} cc;// cc is variableenum color c2;for(cc=red;cc<=blue;cc++){printf("%d\n",cc);}int c;printf("address:%p\n",c);// -- get num;int n;// scanf("%d",&n);int* nump;srand((unsigned)time(NULL));
nump =getnum(5);for(i=0;i<5;i++){printf("%d ",*(nump+i));}//---use function pointerint(*p)(int,int)=∑printf("\nsum:%d\n",p(3,4));int arr[5]={1,2,3,4,5};// int (*fp)(int,int) [2]={&sum,&sub};sumOfArray(arr,5,sum);//--stringchar s1[]="hhh",s2[]="aaaa";char s3[20];strcpy(s3,s1);strcpy(s1,s2);// strcmp s1 less than s3 will return -1 eqaul return 0 bigger return 1;printf("%s?=%s,%d\n",s1,s3,strcmp(s1,s3));strcat(s3,s2);printf("%s\n",s3);// find the first pos of a return a pointerchar name[]="solicucu hhh hhh";char strh[]="hhh";printf("first c at %c\n",*strchr(name,'c'));// find the first pos of str"hhh" return a pointerchar* pos =strstr(name,strh);int len =strlen(pos);printf("first hhh at %s and len is %d\n",pos,len);// -- use strcut
student s;struct node ns;// node ns; // can not declare as this
ns.a =1;
ns.value =2;printf("sum of node:%d\n",ns.a+ns.value);strcpy(s.name,"littlejun");
s.age=18;printf("student:%s,age:%d\n",s.name,s.age);// use union// 分配的大小,满足最大长度需求,且是类型最长长度(这里是4)的整数倍 printf("sizeof udata is %d\n",sizeof(udata));
udata.a =10;printf("data:\na:%d\n",udata.a);
udata.b =100.1;printf("data:\nb:%f\n",udata.b);strcpy(udata.c,"solicucu");// udata.c = "solicucu"; can not use as this// the data is damageprintf("data:\na:%d\nb:%f\nc:%s\n",udata.a,udata.b,udata.c);// 位域 // 按序分配,如果不够,再多分配4个字节,对于int来说// 也就是一个int 可以存储32个变量 printf("sizeof three1 is %d\n",sizeof(three1));printf("sizeof three is %d\n",sizeof(three));
three t;
t.first =1;// t.second = 3; // 溢出 printf("first=%u second=%u\n",t.first,t.second);/// 输入 输出int ia;float fa;double da;char ca;// scanf("%d%f%lf%c",&ia,&fa,&da,&ca);// %[flags][width][.precision][length]specifier// flags + 强制显示+号如果为正数,0不够就填充0,- 左对齐 // width 描述宽度的数字//.precision 保留小数位数 printf("%04d\n%.2f\n%lf\n%-4c\n",ia,fa,da,ca);// getchar() & putchar() 函数/*
for(i=1;i<=5;i++){
ca = getchar(); //可以读入空格,每次一个字符
putchar(ca);
} */char line[10];// gets() & puts() 函数for(i=0;i<0;i++){gets(line);// 读取一整行 if(strlen(line)==0)break;// 空行break puts(line);}
FILE * fp;// r : 只读 w:只写, a 写为追加模式 r+ 读写,w+ 读写,从头开始,a+ 读写,追加模式
fp =fopen("text.txt","a+");// fprintf(fp,"hhh\n");/*
int fputc( int c, FILE *fp ); 写入一个字符c,写入成功,返回该字符值
int fputs( const char *s, FILE *fp ); 写入一个字符串 如果写入成功,它会返回一个非负值
int fgetc( FILE * fp ); 读取一个字符
char *fgets( char *buf, int n, FILE *fp );
函数 fgets() 从 fp 所指向的输入流中读取 n - 1 个字符。
它会把读取的字符串复制到缓冲区 buf,并在最后追加一个 null 字符来终止字符串。
如果这个函数在读取最后一个字符之前就遇到一个换行符 '\n' 或文件的末尾 EOF,则只会返回读取到的字符,包括换行符
*/fscanf(fp,"%s",line);// 遇到空格会停止 puts(line);fgets(line,10,fp);// 整行读,但是最长是10 puts(line);fclose(fp);}