zoukankan      html  css  js  c++  java
  • 以前学C的记录

    2010.1.x
    			1	
    printf()	'-'是左对齐;
    小数点‘.’也占一位 eg: %8.3f  整数占4位,小数点1位,小数3位,
    整数不够位补空格,超过照样输出,
    小数不够补O,超过位四舍五入.
    %08.3f 和 %08d,整数不够位补0
    			2	
    scanf不能格式化输入,只能用一个 整数 指定输入的总位数 scanf("%8f",a);
    无论getchar()输入多少,只收下第一个字符;
    scanf("%c",C);输入多少个字符,只收下第一个字符	
    			3	
    eg: swith()内,没有符合的case就直接跳过,什么都不做;
    #include <stdio.h>
    main()
    {
    	char s[100];
    	int c,i;
    	scanf("%c",&c);
    	scanf("%d",&i);
    	scanf("%s",s);
    	printf("%c\n",c);
    	printf("%c,%d\n",c,i);
    	printf("%c,%d,%s\n",c,i,s);
    }
    //输入123 456 789
    //输出1
    //     1,23
    //     1,23,456
    //c只能放一个字符,第一个字符1,
    //23被输入整数的i收下,遇到空格scanf停止输入
    //456被s[100]收下,遇到空格scanf停止输入
    //导致789没人要
    			4.
    #include <stdio.h>
    #include <string.h>
    main()
    {
    	char a[] = {'a','b','c','d','e','f','g','h','\0'};
    	int i,j;
    	i = sizeof(a);
    	j = strlen(a);
    	printf("%d,%d",i,j);
    }
    输出	i = 9	
    	j= 8	strlen不算‘\0’
    #include <stdio.h>
    #include <string.h>
    main()
    {
    	char a[30] = {'a','b','c','d','e','f','g','h'};
    	char b[30] = {'1','\0','2'};
    	int i,j;
    	i = sizeof(a);
    	j = strlen(a);
    	printf("%d,%d",i,j);
    }
    输出	i = 30	
    	j= 8	strlen不算‘\0’
    #include <stdio.h>
    #include <string.h>
    main()
    {
    	char a[30] = {'\0','b','c','d','e','f','g','h'};
    	int i,j;
    	i = sizeof(a);
    	j = strlen(a);
    	printf("%d,%d",i,j);
    }
    输出	i = 30	
    	j= 0	strlen不算‘\0’,遇到'\0'停止
    #include <stdio.h>
    main()
    {
    	char a[30] = {'a','b','c','d','e','f','g','h'};
    	int i,j;
    	for(i = 0;i < 30;i++)
    		printf("%c\n",a[i]);
    }
    输出abcdefgh后面的都为a
    int,char数组后没没赋值的,遇到%c输出a,遇到%d输出0
    			5
    if ((c = getchar()) != 0)
    getchar一定要加(),否则先!=再=
    背运算符优先级表
    			6	
    #include <stdio.h>
    main()
    {
    	int a[2];
    printf("%d",a[3]+1);
    }
    不会编译错误
    另外定义数组a[i] ( i>0且不能为空 ),否则编译错误
    			7
    stcmp(A,B)比较字符A>B返回正数,A=B返回0,A<B返回负数(比较ASCII比较第一个对不同字符,这两的字	符决定大小)
    			8
    #include <stdio.h>
    main()
    {
    	int y = 1;
    	printf("%d,%d",y,++y);	
    }
    输出2,2   			C语言函数参数计算次序从右向左(←)
    #include <stdio.h>
    func (int a, int b)
    {
    	int c;
    	c = a + b;
    	return c;
    }
    main()
    {
    	int x = 6,r;
    	r = func (x,x+=2);
    	printf("%d",r);
    }
    输出16			C语言函数参数计算次序从右向左(←)
    			9
    fff(float x)
    {
    	printf("%d",x);
    }
    fff是int类型
    			10
    #include <stdio.h>
    int fun(int a, int b, int c)
    {
    	c = a * b;
    	return c;
    } 
    void main()
    {
    	int c;
    	fun(2,3,c);
    	printf("%d",c);
    }
    输出不定值,因为fun中的c传不出去,c不变(C语言 数据只能从实参传递给形参,而形参的数据不能传递给实参)
    可以改成
    #include <stdio.h>
    int fun(int a, int b, int c)
    {
    	c = a * b;
    	return c;
    } 
    void main()
    {
    	int c;
    	c = fun(2,3,c);
    	printf("%d",c);
    }
    输出6
    			11
    #include <stdio.h>
    void main()
    {
    	extern int c;
    	printf("%d",c);
    }
    int c = 0;
    输出0
    			12
    #include <stdio.h>
    func (int a, int b)
    {
    	static int m = 0,i = 2;	//表面上进了2次
    	i += m + 1;
    	m = i + a + b;
    	return m;
    }
    main()
    {
    	int k = 4,m = 1,p;
    	p = func(k, m);
    	printf("%d,",p);
    	p = func(k, m);
    	printf("%d",p);
    }
    输出8,17	       内部静态变量在第一次进入包含它的函数复制后,以后再进入该函数的赋初值语句不起作用
    			13
    #include <stdio.h>
    int fun(int x)
    {
    	printf("%d\n",x);
    	x *= 10;
    	return x;
    }
    void main()
    {
    	int a = 1;
    	
    	printf("%d",fun(a++));
    }
    输出1 10 a = 2;
    #include <stdio.h>
    int fun(int x)
    {
    	printf("%d\n",x);
    	x *= 10;
    	return x;
    }
    void main()
    {
    	int a = 1;
    	fun(a++);
    	printf("%d",a);
    }
    但这样就输出1,2,因为形参不能传给实参
    #include <stdio.h>
    int fun(int x)
    {
    	printf("%d\n",x);
    	x *= 10;
    	return x;
    }
    void main()
    {
    	int a = 1,c;
    	c = fun(a++);
    	printf("%d",c);
    }
    输出1 10,可用一个变量装着先
    			14
    全局变量有效范围是定义变量开始到本文件结束
    static 类型 函数名()	内部函数又称静态函数,只局限于所在的文件,在不同文件中有同名的内部函数,互不干扰
    类型 函数名()	函数定于省略extern,默认为外部函数
    函数实参:1实参可以是常量,变量,表达式
              2实参与形参的类型应相同或赋值相容
              3实参变量对形参变量的数据传递是“值传递”,即单向传递.只由实参传给形参,不能由形参传给实参.在内存中,实参单元与形参单元是不同的单元,在调用函数时,给形参分配存储单元,并将实参对应的值传给形参,调用结束后,形参单元被释放,实参单元保留.
    外部变量作用范围是本程序的全部范围,static声明的外部变量,作用范围只在本文本,不能被其他文件引用
    			15
    #include <stdio.h>
    int x;
    void main()
    {
    	void addone(),subone();
    	x = 1;
    	printf("%d\n",x);
    	addone();
    	subone();
    	subone();
    	addone();
    	addone();
    	printf("%d\n",x);
    }
    void addone()
    {
    	x++;
    	printf("%d\n",x);
    }
    void subone()
    {
    	x--;
    	printf("%d\n",x);
    }
    因为x是全局变量输出1210122
    			16
    对于多维数组
    eg: int c[3][4],*p;			//c是二维数组
    c 是数组的首地址,第0行的首地址( c[0] 表示 &c[0][0] )
    c+1 是数组的 c[1] 的地址,第1行的首地址( c[1]表示&c[1][0] )
    第0行第1列的地址用 c[0]+1 表示
    c[i] 和 *(c+i) 是等价的都表示地址 &c[i][0]
    c[i]+j 和 *(c+i)+j 都是 &c[i][j],表示第i行第j列的元素的地址
    所以c, c+i, c[i], *(c+i)+j, c[i]+j 都是地址, *(c[i]+j), *(*(c+i)+j) 是元素的值
    c				二维数组名,数组首地址,0行首地址
    c[0], *(c+0), *c			第0行第0列元素地址
    c+1, &c[1]			第1行首地址
    c[1], *(c+1)			第1行第0列元素地址
    c[1]+2, *(c+1)+2, &c[1][2]		第1行第2列元素地址
    *(c[2]+2),*(*(c+2)+2), c[2][2]		第2行第2列元素值
    			17
    #include <stdio.h>
    void main()
    {
    	char *str = {"china"};
    	str = str + 2;
    	printf("%s",str);
    }
    输出ina
    #include <stdio.h>
    void main()
    {
    	char  str[] = {"china"};
    	str = str + 2;
    	printf("%s",str);
    }编译错误
    			18
    char str[20];
    str = "China";   
    编译ok
    char *s;
    s = "China";
    编译ok
    char s[30];
    s[] = "China";  	//字符数组在定义时整体初始化,在赋值语句中不能完成整体赋值
    编译错误
    			19
    char s[10];
    scanf("%s",s);
    编译ok
    char *s;
    scanf("%s",s);
    编译错误		危险s单元不可预料,指向不明
    char *a,s[10];
    a = s;
    scanf("%s",a);
    编译ok
    			20
    int a = 3, b = 2, c = 1;
    d = (a>b>c);
    输出d为0   因为a>b布尔值为1,1>1布尔值为0,只要c>0→d=0
    			21
    int (*prt)[3]是一个指针,指向一个具有3个元素的一维数组
    int *prt[3]是一个指针数组
    #include <stdio.h>
    void main()
    {
    	int **k,*a,b=100;
    	a = &b;
    	k = &a;
    	printf("%d\n",b);
    	printf("%d\n",*a);
    	printf("%d\n",**k);
    }
    k    a     b
    □ → □ → □
    *a 引用变量b的储存单元 (&b), *a 就是 b			
    *k 引用变量a的储存单元 (&a), *k 就是 a
    **k 即 *(*k) 是 *a也是 b
    			22
    #include <stdio.h>
    void main()
    {
    	int *a = 0,*b = 0;
    	*a = 10;
    	*b = 20;
    }
    编译没错,运行出错.因为a,b是空指针
    char ch,*p = &ch;
    scanf(“%c”,p);
    *p = ‘A’;
    printf(“%c”,*p);	//注意是*p,取值
    			23
    返回指针的函数	类型名 *函数名(参数表)	eg: int *fun(int a,int b);
    指向函数的指针	类型名 (*指针变量名)();	eg: int (*fun)();
    		对指针函数的指针变量++s,--s,s+3是没有意义的
    			24
    #include <stdio.h>
    #define SQR(X)X*X
    void main()
    {
    	int a = 10,k = 2,m = 1;
    	a /= SQR(k+m)/SQR(k+m);
    	printf("%d",a);
    }
    输出为1	因为宏进行的是简单的替换a/=X*X/X*X;
    	再替换成a/=k+m*k+m/k+m*k+m;
    10/(2+1*2+1/2+1*2+1)=10/7=1;
    改正:
    #include <stdio.h>
    #define SQR(X)(X*X)
    void main()
    {
    	int a = 10,k = 2,m = 1;
    	a /= SQR(k+m)/SQR(k+m);
    	printf("%d",a);
    }
    输出10
    #include <stdio.h>
    #define SQR(X)X*X
    void main()
    {
    	int a,k = 3;
    	a = ++SQR(k+1);
    	printf("%d",a);
    }
    输出9	因为a = ++k+1*k+1 = 9
    #define MIN(x,y) (x)<(y)?(x):(y)
    void main()
    {
    	int i = 10,j = 15,k = 10*MIN(i,j);
    	printf("%d",k);
    }
    输出15 		因为k = 10*(x)(y)?(x):(y) = 15
    			25
    #include <stdio.h>
    void fun(float *p1,float *p2,float *s)
    {
    	s = (float *)calloc(1,sizeof(float));
    	*s = *p1 + *p2++;
    }
    void main()
    {
    	float a[2]={1.1,2.2},b[2] = {10.0,20.0},*s = a;
    	fun(a,b,s);
    	printf("%5.2f",*s);
    }
    输出1.10		因为fun()中已经给 s 重新赋了新地址,此后的*s赋值与主函数的数组a无关,形参数据传不回实参,s不变.
    			26
    #define N 6
    #include <stdio.h>
    main()
    {
    	char c[N];
    	int i = 0;
    	for(	;i<N;c[i]=getchar(),i++);
    	for(i=0;i<N;i++)
    		putchar(c[i]);
    }
    输入ab<回车>
         c<回车>
         def<回车>
    输出  ab
           c
           d
    0 1 2  3  4  5 
    a b  \n c \n  d   ('\n'ACSII = 10 = 回车 )
    			27
    int x = 1,y = 10;
    x^=y;
    y^=x;
    x^=y;
    printf("%d,%d",x,y);//输出10,1
    等价与x,y交换
    			28
    #define POWER(x) ((x)*(x))
    main()
    {
    	int i = 1;
    	while(i<=4)
    		printf("%d",POWER(i++));
    	printf("\n");
    }
    输出19  因为POWER	(i++) = ((i++)*(i++))    i→1*1→3*3
    #define PRINT(x) printf("%d\n",x)
    #include <stdio.h>
    main()
    {
    	int x=1,y=1,z=0;
    	x = x&&y||z;
    	PRINT(x);
    	PRINT(x||!y++&&++z);		//y和z的变换传不出去
    	PRINT(y);				//y不变
    	PRINT(z);				//z不变
    	x = y = 1;
    	z = x++ - 1;
    	PRINT(x);
    	PRINT(z);
    	z += -x++ + ++y;
    	PRINT(x);
    	PRINT(z);
    	PRINT(y);
    }
    输出1 1 1 0 2 0 3 0 2
    			29
    #include <stdio.h>
    union un
    {
    	int i;
    	char c[2];
    };
    void main()
    {
    	union un x;
    	x.c[0] = 10;
    	x.c[1] = 1;
    	printf("\n%d",x.i);
    }
    输出266,c[0]位于低字节,c[1]位于高字节, x.i = x.c[1] * 256 + x.c[0] = 266. 
    			30
    #include <stdio.h>
    void main()
    {
    	union 
    	{
    		int i[2];
    		long k;
    		char c[4];
    	}r,*s = &r;
    	s->i[0] = 0x39;
    	s->i[1] = 0x38;
    	printf("%x\n",s->c[0]);
    }
    输出39  因为共用体变量中,所有成员共用存储空间 c[0] 和 i[1] 的低八位共用一个字节
    			31
    #include <stdio.h>
    typedef union
    {
    	long i;
    	int  k[5];
    	char c;
    } DATE;
    struct data
    {
    	int cat;
    	DATE cow;
    	double dog;
    }too;
    DATE max;
    main()
    {
    	printf("%d\n",sizeof(struct data) + sizeof(max));
    }
    输出52      52 = (20+4+8)+(20)
    			32
    #include <stdio.h>
    union change
    {
    	char c[2];
    	int i;
    }un;
    main()
    {
    	un.i = 26984;
    	printf("%d,%c\n",un.c[0],un.c[0]);
    	printf("%d,%c\n",un.c[1],un.c[1]);
    }
    输出 104,h   105,i	因为26984 = 0110, 1001, 0110, 1000 B
    			 c[1]=104(' i ')  |  c[0]=105(' h ')
    			33
    #include <stdio.h>
    union pw
    {
    	int i;
    	char ch[2];
    }a;
    void main()
    {
    	a.ch[0] = 13;
    	a.ch[1] = 0;
    	printf("%d\n",a.i);
    }
    输出13
    			34
    #include <stdio.h>
    main()
    {
    	union
    	{
    		int i[2];
    		long k;
    		char c[4];
    	}t,*s = &t;
    	s->i[0] = 0x39;
    	s->i[1] = 0x38;
    	printf("%lx\n",s->k);
    	printf("%c\n",s->c[0]);
    }		
    输出39  9 
    printf("%lx\n",s->k);是16进制输出 39
    printf("%c\n",s->c[0]);0x39 = 十进制的59,ACSII57 = 9
    			35
    #include <stdio.h>
    main()
    {
    	static struct s1
    	{
    		char c[4],*s;
    	}s1 = {"abc","def"};
    	static struct s2
    	{
    		char *cp;
    		struct s1 ss1;
    	}s2 = {"ghi","jkl","mno"};
    	
    	printf("%s,%s\n",++s2.cp,++s2.ss1.s);
    }
    输出hi,no
    s2分别指向h,n开始,cp,s是指针
    			36
    #include <stdio.h>
    union ks
    {
    	int a;
    	int b;
    };
    union ks s[4];
    union ks *p;
    main()
    {
    	int n = 1,i;
    	for(i=0; i<4; i++)
    	{
    		s[i].a = n;
    		s[i].b = s[i].a + 10;
    		n +=2;
    	}
    	p = &s[0];
    	printf("%d,",p->a);
    	printf("%d",++p->a);
    }
    输出11,12因为ks是共用体,s[i].b覆盖原先s[i].a的值
    			37
    #include <stdio.h>
    main()
    {
    	int b[] = {1,2,3,4},y,*p = b;
    	y = *p++;
    	printf("%d",y);
    	printf("%d",*p);
    }
    输出1	2
     *p++ = (*p)++ 先赋值在++
    			38
    #include <stdio.h>
    main()
    {
    	char *p = "abcdefgh",*r;
    	long *q;
    	q = (long*)p;
    	q++;
    	r = (char*)q;
    	printf("%s",r);
    }
    输出efgh
    char 1字节 long 4字节 ,q++跳了4个char=1个long
    			39
    main()
    {
    	char a[2][5] = {"1234","5678"},b[2][5] = {"12345","5678"}, c[2][5] = {"123456","6"};
    	printf("%d\n",strlen(a));
    	printf("%d\n",strlen(b));
    	printf("%d\n",strlen(c));
    }
    输出4 9 6 
    			40
    #include <stdio.h>
    main()
    {
    	char ch[2][5] = {"6937","8254"},*p[2];
    	int i,j,s = 0;
    	for(i=0; i<2; i++)
    		p[i] = ch[i];
    	for(i=0; i<2; i++)
    		for(j=0; p[i][j]>'\0'; j+=2)
    			s = 10*s + p[i][j] - '0';
    		printf("%d",s);
    }
    输出6385
    隐含ch[2][5] = {"6937\0","8254\0"};
    	         ↑  ↑   ↑   ↑   ↑   ↑
    		    跳出        跳出
    			41
    #include <stdio.h>
    void main()
    {
    	int i;
    
    	for(i=0; i<7; i++)
    		putchar(*("ABCDEFG"+i));	printf("\n*****************\n");
    	for(i=0; i<7; i++)
    		putchar("ABCDEFG"[i]);		printf("\n*****************\n");
    
    	for(i=0; i<7; i++)
    		printf("ABCDEFG\n"+i);		printf("\n*****************\n");	
    	for(i=0; i<7; i++)
    		printf("%c\n","ABCDEFG"[i]);	printf("\n*****************\n");		
    	for(i=0; i<7; i++)
    		printf("%c\n",*("ABCDEFG"+i));	printf("\n*****************\n");
    	for(i=0; i<7; i++)
    		printf("%s\n","ABCDEFG"+i);	
    }
    
    
    
    
    


  • 相关阅读:
    利用Navicat向MySQL数据库中批量插入多条记录的方法
    《Spring MVC+MyBatis快速开发与项目实战》-黄文毅2019:一书的源码和配套视频下载地址
    MySQL数据库建库时SQL语句中数据库名、表名用引号的问题以及COLLATE utf8_general_ci的含义
    [转]层行列和经纬度坐标之间的相互转化方法(谷歌地图)
    [Web 前端] VML、SVG、Canvas简介
    [Android Pro] 完美解决 No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    [Android Pro] so 动态加载—解决sdk过大问题
    [Android Pro] https://blog.csdn.net/gaugamela/article/details/79143309
    [web前端] 去哪儿网前端架构师司徒正美:如何挑选适合的前端框架?
    [web前端] yarn和npm命令使用
  • 原文地址:https://www.cnblogs.com/coolulu/p/4084911.html
Copyright © 2011-2022 走看看