C-Program-study 发表于 2020-01-10 字数统计: 1k 字 | 阅读时长 ≈ 5 分钟 个人学习C语言笔记 以下代码学习自周立功程序设计与数据结构,学习笔记 仅作为学习记录使用 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282*//#include "swip.h"*int main(void){\#if 0 int iNum = 0x64; printf("This is Test\r\n"); *//**************************************//* printf("&iNum:%x,iNum:%x\n", &iNum, iNum); *//&iNum 是指向int 变量iNum的指针* *//**************************************//* int *ptr; *//定义一个指针变量 ptr是指向int的指针* *// int \* 类型名是指向int非指针类型* ptr = &iNum; printf("ptr:%x\n", ptr); printf("*ptr:%x\r\n", *ptr); *//**************************************//* typedef int *PTR_INT; PTR_INT *pPtr = &ptr; printf("pPtr:%x,&ptr:x\n", pPtr, &ptr); *//**************************************//* char data[3] = {0x01, 0x02, 0x03}; printf("%x \r\n", data); *//指向数组指针* printf("%x \r\n", data + 1); printf("%x \r\n", data + 2); printf("%x\r\n", *(data)); *//**************************************//* int data0, data1, data2; int *ptrarr[3] = {&data0, &data1, &data2}; char *keyWord[5] = {"eagle", "cat", "and", "dog", "ball"}; printf("%s\r\n", keyWord[0]); printf("%s\r\n", keyWord[1]); *//**************************************//* *//*****************浮点运算*************//* *//float 型变量仅能接收浮点型常量的7位有效数据,在有效数字后面输出的数字都是不准确的* *//因此有效数据外的数据,不能用==做比较,下面例子显示Fail* float f1 = 123.456001; float f2 = 123.456002; if (f1 == f2) { printf("OK\r\n"); } else { printf("Fail\r\n"); } *//**************************************//* *//数据类型转换* unsigned int a = 9; int b = -4, c; c = a / b; printf("c:%d\r\n", c);*//**************************************//**//**************************************//**//for 注意事项:i是uchar的范围是0--255,但是i++会导致一直累加,**//当I==255的时候,I++,导致i=0,又小于FF,无线循环*\#if 0 for(unsigned char i=0xFc;i <= 0Xff;i++) { printf(" i : %d\r\n",i); }\#endif*//**************************************//*\#if 0\#define MOD(*x*, *y*) x % yint m =13 ,n =94;printf("%d\n",MOD(n,m+4));*// 7 :94%13+4*\#else*//#define MOD(x,y) x%y**//int m =13 ,n =94;**//printf("%d\n",MOD(n,(m+4)));// 9 :94%(13+4)*\#define MOD(*x*, *y*) (x) % (y)int m =13 ,n =94;printf("%d\n",MOD(n,m+4));*// 7 :94%13+4*\#endif*//**************************************//*printf("%c%3c%5c\n", 'a', 'b', 'c'); *//%后面代表字段宽度,%5表示输出的字符少于5就用空格填空,目的是为了对齐**//**************************************//*int iNum_1 = 0x79;int *ptr_1 = &iNum_1;int *ptr_2 = &iNum_1;*//指针是无符号整型,因此此处char \*ptr_2赋值会报错*printf("%d, %d ",ptr_1,*ptr_1);*//6422208,121**ptr_1 = *ptr_1 +1;*//地址内数据加一**(ptr_1 + 1) = 0x7A; *//给下一个地址赋值*printf("%d, %d ",ptr_1,*ptr_1);*//6422208,122//地址未变,地址内的数据加一*printf("%d, %d ",ptr_1+1,*ptr_1+1);*//6422212,123//指针为Int型,因此增加4,这也是sizeof(int\*A) =4的原因*printf("%d, %d ",ptr_2,ptr_2+1);*//6422208,6422209*printf("%d",*(ptr_1+1));*//122**//assert()断言判断,如果参数为真,继续运行,如果不为真,return 0*int data_1[3] = {1, 2, 3};int *ReceivedData;printf("\r\n%x, %x, %x, %x, %x", &data_1[0], &data_1[1], &data_1[2], &data_1, &data_1+1);*//0x6100,6104,6108,6100,610c**//为什么&data_1 +1是610C呢?原因:系统会认为&data_1 +1中的1,是偏移一个数组大小,此数组大小为12(3\*4),即越界。*printf("\r\n%d,%d\r\n",*data_1,*(data_1 + 1));*//1,2*ReceivedData = data_1;printf("\r\n%x",ReceivedData);printf("\r\n%x",ReceivedData++);printf("\r\n%x",ReceivedData);int data[10],*pData,*qData,i,j;data[0]=0x12;data[1] = 0x22;pData = data;printf("data addr : 0x%x\r\n",data);*//0x0000*printf("pData addr : 0x%x\r\n",pData);*//0x0000*pData ++;printf("data addr : 0x%x\r\n",&data[1]);*//0x0004*printf("pData addr : 0x%x\r\n",pData);*//0x0004*printf("pData data : 0x%x\r\n",*pData);*//0x22**//如果pData指向data[i],那么pData+j 等于data[i+j]**//\*(pData++) 因为后缀++的优先级高于\*,因此pData++等于pData。计算完之后,才会++*\#else */*** *** *@brief* *几个数据累加的计算函数* *** *@details* *** *@author* *Tyler* *** **/*int data[10] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00};int sum,*pData;sum = 0;*//注意赋初值,默认编译器不会自动赋初值的*pData = data;while (pData < &data[10])*//此处未算法关键部分,虽然数组只有10个数据,但是data[10]的地址是合法的*{ sum += *pData++;}printf("Sum: 0x%x\r\n",sum);\#endif system("pause"); return 0;}