C语言常用知识点样例代码

C语言部分知识点

static 的理解,label的使用,enum枚举类型,函数指针,字符串常用函数:strcpy,strcmp,strcat,strchr,strstr,结构体的定义,union联合体的理解,struct 位域,c语言的输入输出,scanf,printf,gets,puts 文件的打开关闭
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<malloc.h>
typedef long long ll; // use an identifier replace a type
#define ONE 1     // use an identifier repalce a value
//typedef 是由编译器执行解释的,#define 语句是由预编译器进行处理的。
void coutdown(){
	static int count=5;
	printf("count = %d\n",count--);
} 

//return pointer
int* 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 pointer
int sum(int a,int b){
	return a+b;
}
int sub(int a,int b){
	return a-b;
}

int sumOfArray(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 var
struct node{
	int a;
	int value;
} n1,n2;
// after define the struct and declare the var
struct node1{
	int a;
	int value;
};
struct node1 n3;

// define the strcut as a type
typedef struct{
	char name[10];
	int age;
} student;

// -- use union  分配的空间为最大长度的类型,而且每个变量的起始位置都一样,所以,这个适合
// 给多个变量共享内存使用,因为他们不同时用
union data{
	int a;
	float b;
	char c[21];
} udata; 

// 位域
typedef struct {
	
	// type  name  width width must less than the len of sizeof(type) 
	unsigned int first:1;
	unsigned int second:1;
	// 定义32个这样的变量,开的空间都是4字节 
} three; 
// 不使用位域
typedef struct {
	unsigned int first;
	char second;
	// 也会采用对齐 
} three1; 
int main(){
	// ----static 无论是修饰局部还是全局,每次调用不会销毁,而是一直共享 
	int i;
	for(i=0;i<5;i++){
		coutdown();
	}
	
	// ----use label
	printf("hhh\n");
	// exe in order once time
	label1:
		printf("hhh1\n");
	label2:
		printf("hhh2\n");
	label3:
		printf("hhh3\n"); 
	//	goto label2;
	
	
	//---- enum
	enum color{
		red=1,green,blue
	} cc;
	// cc is variable
	enum 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 pointer
	int (*p) (int,int) = &sum;
	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);
	
	//--string
	char 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 pointer
	char 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 pointer
	char* 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 damage
	printf("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); 
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值