日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言笔记含源码(变量、输入输出、分支、循环、函数、数组、指针、字符串、结构体)小总结

發布時間:2024/3/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言笔记含源码(变量、输入输出、分支、循环、函数、数组、指针、字符串、结构体)小总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、變量與輸入輸出
  • 二、分支語句
  • 三、循環
  • 四、函數
  • 五、數組
  • 六、指針
  • 七、字符串
  • 八、結構體

一、變量與輸入輸出

定義變量需要:類型、變量名、變量值(可有可無)

#include <stdio.h> #include <stdlib.h> int main() //程序入口 {printf("hello\n");int num1;int num2;/* 01printf("請輸入一個加數:\n");scanf("%d",&num1); //1.雙引號中除了占位符,其余盡量不要寫,否則可能產生錯誤//輸入: //2.占位符代表從鍵盤輸入一個數,注意取地址//3.輸入輸出擦別:輸入比輸出多了一個“&” 即取址符//4.當連續輸入多個變量時,分開寫printf("請輸入一個加數:\n");scanf("%d",&num2);int num3=num1+num2;printf("我很帥\n");*/printf("請輸入兩個加數\n"); //scanf("%d %d",&num1,&num2);//多個變量輸入 嚴格按照“”里面的格式scanf("%d,%d",&num1,&num2);//必需輸入 4,4int num3=num1+num2;printf("結果:%d+%d=%d\n",num1,num2,num3); //占位符,%d 代表在輸出的地方,占了一個坑,至于輸出啥,根據逗號后面的變量名里面的變量值system("pause");return 0; }

心得一:在輸出(printf)時時刻注意換行(\n)的使用
心得二:非必要情況,選擇英文輸入法。如上例程序:
英文輸入法下正常:
中文輸入法下的結結果

二、分支語句

分支語句包括if-else及其嵌套,swtich
if-else

#include <stdio.h> #include <stdlib.h> int main() {//0.定義變量int num;//1.請輸入一個數printf("請輸入一個數\n");//2.獲取用戶輸入scanf("%d",&num);//3.是否滿足條件if(num==100){//滿足 輸出滿足printf("我嫁給你\n");}else{//不滿足printf("那就這樣吧\n");}system("pause");return 0; }

運行結果:


if-else嵌套

//判斷分數等級 等級條件略。。。#include <stdio.h> #include <stdlib.h> int main() {int score;//1.提示輸入printf("請輸入成績\n");//2.獲取用戶輸入scanf("%d",&score);//3.判斷等級if(score >= 90){printf("成績優秀\n");}else if(75 <= score && score < 90){printf("成績良好\n");}else if(60 <= score && score < 75){printf("成績及格\n");}else if(40<= score && score <60){printf("不及格\n");}else{printf("很差勁,退學!\n");}system("pause");return 0; }

運行結果:

請輸入成績
90
成績優秀
請按任意鍵繼續. . .

switch

#include <stdio.h> #include <stdlib.h> int main() {//0.定義int data;//1.提示用戶輸入printf("請輸入一個數\n");//2.獲取用戶輸入scanf("%d",&data);//3.判斷數據if(data == 100){printf("牛逼,可以找對象了\n");}else{switch(data/10){/* case 1:printf("你輸入了一個1\n");break;case 2:printf("你輸入了一個2\n");//break;*/case 1:case 2:case 3:printf("不及格,而且很擦\n");break;case 4:case 5:printf("不及格\n");break;case 6:case 7:printf("還行,及格了\n");break;case 8:printf("成績良好\n");break;case 9:printf("成績優秀\n");break;//case 10://printf("")default:printf("非法成績\n");break; }} system("pause");return 0; }

運行結果:

請輸入一個數
56
不及格
請按任意鍵繼續. . .

三、循環

for循環

#include <stdio.h> #include <stdlib.h> int main() {int score;int ciShu = 0;//循環的初始條件// while(1)//for(ciShu = 0; ciShu < 5; ciShu = ciShu + 1)for(;;){ //強調分號//cishu = cishu + 1; //循環條件發生變化//1.提示輸入printf("請輸入第%d成績\n",ciShu+1);//2.獲取用戶輸入scanf("%d",&score);//3.判斷等級if(score >= 90){printf("成績優秀\n");}else if(75 <= score && score < 90){printf("成績良好\n");}else if(60 <= score && score < 75){printf("成績及格\n");}else if(40<= score && score <60){printf("不及格\n");}else{printf("很差勁,退學!\n");}/*if (cishu == 3){ //循環終止條件break; //跳出循環} */}system("pause");return 0; }

運行結果:

請輸入第1成績
23
很差勁,退學!
請輸入第2成績
52
不及格
請輸入第3成績
65
成績及格
請輸入第4成績
99
成績優秀
請輸入第5成績

do-while
do-while循環與while循環:do-while至少執行一次,不關心條件,第一次循環執行后循環次數才與條件有關。while循環的每次循環都與循環的條件相關。

#include <stdio.h> #include <stdlib.h> int main() {/*while(1){printf("***帥\n");}*/int i=0;do{i++;printf("可以處對象了\n");}while(i<=10);//11次,i==10時多執行一次system("pause");return 0; }

運行結果:

可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
可以處對象了
請按任意鍵繼續. . .

四、函數

形參的作用域是函數的左耳朵(左括號)和右耳朵(右括號)之間

#include <stdio.h> #include <stdlib.h>//函數是一個功能模塊 /* 定義函數:1.返回值 2.形參 3.函數名 4.函數體 代碼塊 調用函數:函數(形參)*//* 為什么用函數:1.代碼很好看 2.容易定位問題 3.可復用性強 4.分工 */int prepare(){ //有返回無參數printf("做準備\n");printf("洗漱\n");printf("穿衣\n");printf("準備完畢\n\n");return 0; }int onTheBorad(){printf("在路上\n");printf("訂飯店\n");printf("找滴滴\n");printf("路上事情做完\n\n");return 0; }int datting(){printf("約會中\n");printf("寒暄\n");printf("吃飯\n");printf("看電影\n");printf("約會完畢\n\n");return 0; }int goBackHome(){printf("回家\n");printf("Ok\n\n");int mark = 0;return 0; }void delay(int i){//無返回有參數//int i;//局部變量 類似形參for(i = 0; i < 5000; i++); }//你爸爸叫你買米,不關心買多少斤(形式參數),也不關心買回來了沒有 void buyRice(){ //無返回無參數printf("去買米。。。\n\n"); }void buyRice2(int jinShu){ //jinShu是形式參數,類似變量定義 int i;printf("去買%d斤米。。。\n",jinShu); //如果提示\243,\254則說明你可能寫了中文符號}//形式參數,作用域僅限該函數int main() {prepare();//出門前的準備onTheBorad();//在路上datting();//約會中goBackHome();//回家//以上是自定義函數,需要去把函數做出來printf("\n");buyRice();buyRice2(20); //帶形參的自定義函數buyRice2(5);system("pause");//調用windows指令return 0; }

運行結果:

做準備 洗漱 穿衣 準備完畢

在路上 訂飯店 找滴滴 路上事情做完

約會中 寒暄 吃飯 看電影 約會完畢

回家 Ok

去買米。。。

去買20斤米。。。 去買5斤米。。。 請按任意鍵繼續. . .

自定義函數實現計算器
函數可以多級調用

#include <stdio.h> #include <stdlib.h>void tip(){printf("********************\n");printf("***作者:最帥------*\n");printf("********************\n"); }int add(int data1,int data2){int ret=data1+data2;return ret; }int minu(int data1,int data2){int ret=data1-data2;return ret; }int mult(int data1,int data2){int ret=data1*data2;return ret; }float chu(int data1,int data2){float ret=(float)data1/data2;return ret; } void tipSuanFa(char suanFa){switch(suanFa){case '+':printf("你選擇了加法\n");break;case '-':printf("你選擇了減法\n");break;case '*':printf("你選擇了乘法\n");break;case '/':printf("你選擇了除法\n");break;}}void realCalc(int data1,int data2,char suanFa) {switch(suanFa){case '+':printf("加法結果:%d+%d=%d\n",data1,data2,add(data1,data2));break;case '-':printf("減法結果:%d-%d=%d\n",data1,data2,minu(data1,data2));break;case '*':printf("乘法結果:%d*%d=%d\n",data1,data2,mult(data1,data2));break;case '/':printf("除法結果:%d/%d=%f\n",data1,data2,chu(data1,data2));break;} } void clac(char suanFa) {int data1;int data2;tipSuanFa(suanFa);printf("請輸入第一個數\n");scanf("%d",&data1); printf("請輸入第二個數\n");scanf("%d",&data2);realCalc(data1,data2,suanFa);}int main() {//int data1;//int data2;// int ret;char ch;int i=0;while(1){tip();if(i!=0)getchar();//吸收第一次(不吸收)之后的 輸入完“請輸入第二個數”之后的回車符i=1;printf("請輸入你要運算的類型\n");scanf("%c",&ch);getchar();//回車符也是一種字符,getchar用來獲取字符,此處用來吸收字符clac(ch);} system("pause");return 0; }

運行結果:

******************** ***作者:最帥------* ******************** 請輸入你要運算的類型 + 你選擇了加法 請輸入第一個數 2 請輸入第二個數 3 加法結果:2+3=5 ******************** ***作者:最帥------* ******************** 請輸入你要運算的類型 / 你選擇了除法 請輸入第一個數 9 請輸入第二個數 3 除法結果:9/3=3.000000 ******************** ***作者:最帥------* ******************** 請輸入你要運算的類型

五、數組

數組的定義、初始化、訪問

#include <stdio.h> #include <stdlib.h> int main() {printf("hello\n");int array[100];//沒有初始化數組,僅僅申請了100個內存空間 最好初始化array[100]={0}int array2[3]={1,2,3};//有初始化的數組,該數組申請了三個整型數內存空間,并賦值1 2 3int array3[100]={1,2,3};//有初始化的數組,不完整初始化,該數組申請了100個內存空間,但是只賦值了三個數1 2 3,放在了數組的前三個位置 其余默認是 0//int n;//scanf("%d",&n);//int array4[n];//實際應用中無報錯,可以使用,但教材中認為非法//學到指針時,就可以拋棄這種方法,用指針思維來做//***************數組通過下標引用元素**********************printf("array2的第二個元素是:%d\n",array2[1]);//數組中[]只有定義時候是表示數組大小,其余任何情況都表示用下標訪問數組int i;for(i = 0; i < 3; i++){printf("第%d個元素是:%d\n",i+1,array2[i]);}system("pause");return 0; } /* 1.數組是數據集 2.數組數據同類型 */

運行結果:

hello
array2的第二個元素是:2
第1個元素是:1
第2個元素是:2
第3個元素是:3
請按任意鍵繼續. . .

數組與函數(算數組大小,數組傳參、形參)

#include <stdio.h> #include <stdlib.h>//地址!! void arrayPrint(int datas[],int cnt)//形式參數中,雖然寫的是數組樣子 但是 中括號中數組大小是沒有用的 { //中括號里寫多少都不能代表形參數組有多大//這里中括號的作用僅僅是用來表示該參數是一個地址(這里是數組首地址) 在windows啊哈C中4個字節表示地址 linux64位中用8個字節表示int i;//printf("函數中array數組個數:%d\n",sizeof(datas)/sizeof(datas[0]));// 4/4=1for(i = 0;i<cnt;i++){printf("%d ",datas[i]);}putchar('\n');//putchar 輸出一個字符 getchar()獲取一個字符的輸入 當時過濾回車}int main() {int array[20]={11,22,33};printf("array數組個數:%d\n",sizeof(array)/sizeof(array[0]));//printf("array數組個數:%d\n",sizeof(&array[0])/sizeof(array[0])); //sizeof(&array[0]) 指針大小 4,數組名代表數組大小arrayPrint(array,sizeof(array)/sizeof(array[0]));arrayPrint(&array[0],sizeof(array)/sizeof(array[0]));//sizeof計算操作數存儲字節大小/*printf("array數組名大小:%d\n",sizeof(array));//數組名代表數組 20 x 4printf("指針大小:%d\n",sizeof(&array[0]));//是指針 4*/int a[3];char b[3];//類型大小 * 元素個數printf("a數組大小:%d\n",sizeof(a)); // 4 *3 printf("b數組大小:%d\n",sizeof(b)); // 1 *3printf("一個int數據類型的大小:%d\n",sizeof(int)); //4printf("一個char數據類型的大小:%d\n",sizeof(char)); //1//請問數組長度??printf("a數組長度為:%d\n",sizeof(a)/sizeof(a[0]));//避免了數據類型 巧用a[0]printf("b數組長度為:%d\n",sizeof(b)/sizeof(b[0]));/*//arrayPrint(array);//1.數組和函數結合,數組名做實際參數*/system("pause");return 0; }

運行結果

array數組個數:20
11 22 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 22 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
a數組大小:12
b數組大小:3
一個int數據類型的大小:4
一個char數據類型的大小:1
a數組長度為:3
b數組長度為:3
請按任意鍵繼續. . .

數組和函數綜合應用

#include <stdio.h> #include <stdlib.h>void initScores(int datas[],int size) {int i;for(i=0;i<size;i++){printf("請輸入第%d個學生的分數:",i+1);scanf("%d",&datas[i]);if(datas[i]<0 || datas[i]>100){printf("我信你個大頭鬼\n");printf("tui~\n");system("pause");exit(-1);}}}void printScores(int datas[],int size) {int i;printf("學生分數如下:\n");for(i=0;i<size;i++){printf("第%d個學生的分數是%d\n",i+1,datas[i]);}}int scoresMax(int datas[],int size) {int i;int max;max=datas[0];//初始賦值for(i=1;i<size;i++) //比較{if(max<datas[i]){max=datas[i];}}return max; }int scoresMin(int datas[],int size) {int i;int min;min=datas[0];//初始賦值for(i=1;i<size;i++) //比較{if(min>datas[i]){min=datas[i];}}return min; }float scoresAver(int datas[], int size) {int sum;int i;sum=0;for(i=0;i<size;i++){sum+=datas[i];}//printf("%d",sum);return (float)sum/size; }printRet(int data1,int data2,float data3) {printf("分數最大值:%d\n分數最小值:%d\n分數平均值%f\n",data1,data2,data3); }int main() {int scores[10]={0};int len;int max;int min;float average;len=sizeof(scores)/sizeof(scores[0]);//1.初始化學生分數initScores(scores,len);//2.輸出學生分數printScores(scores,len);//3.計算分數最大值max=scoresMax(scores,len);//4.計算分數最小值min=scoresMin(scores,len);// 5.計算分數平均值average=scoresAver(scores,len);// 6.輸出結果printRet(max,min,average);system("pause");return 0; }

運行結果:

正常分數輸入的

請輸入第1個學生的分數:60
請輸入第2個學生的分數:70
請輸入第3個學生的分數:80
請輸入第4個學生的分數:90
請輸入第5個學生的分數:100
請輸入第6個學生的分數:60
請輸入第7個學生的分數:32
請輸入第8個學生的分數:65
請輸入第9個學生的分數:56
請輸入第10個學生的分數:10
學生分數如下:
第1個學生的分數是60
第2個學生的分數是70
第3個學生的分數是80
第4個學生的分數是90
第5個學生的分數是100
第6個學生的分數是60
第7個學生的分數是32
第8個學生的分數是65
第9個學生的分數是56
第10個學生的分數是10
分數最大值:100
分數最小值:10
分數平均值62.299999
請按任意鍵繼續. . .

分數不正常輸入:

請輸入第1個學生的分數:20
請輸入第2個學生的分數:60
請輸入第3個學生的分數:90
請輸入第4個學生的分數:100
請輸入第5個學生的分數:101
我信你個大頭鬼
tui~
請按任意鍵繼續. . .

六、指針

C語言中的指針可以直接對特定的內存地址操作,這是很多編程語言所不具備的。巧妙運用指針能使程序變得靈活。

定義指針(對比非指針變量)

#include <stdio.h> #include <stdlib.h> int main() {int a=10;//整型變量a=100;char c;//字符變量c='a';int array[3]={1,2,3};//數組 變量int *p;//指針變量存放的地址p = &a;//給指針變量賦值int *p2=&a;//定義并且初始化指針變量/*共同點:都是變量不同:變量類型*//*問:什么變量能存放地址,存放的是地址?答:指針變量問:怎么定義指針變量?int* p; int *p;char */system("pause");return 0; }

通過指針訪問變量

#include <stdio.h> #include <stdlib.h>/* 一、變量名的訪問方式有兩種 變量名 地址四、指針=地址 */ int main() {int juHuaTai=10; //定義變量int meiGuiTai=88;int *p; //定義指針p=&juHuaTai;int *p2=&meiGuiTai;printf("juHuaTai=%d\n",juHuaTai); //一、通過變量名輸出printf("meiGuiTai=%d\n",meiGuiTai);printf("juHuaTai的地址是:%p\n",&juHuaTai); //二、取變量名所代表的變量的內存地址(取地址)printf("meiGuiTai的地址是:%p\n",&meiGuiTai);printf("通過地址訪問juHuaTai的值=%d\n",*(&juHuaTai)); //三、此時,*是一個運算符 與+-*/類似,該運算符的功能是取出內存地址中的數據的值(取內存)printf("通過地址訪問meiGuiTai的值=%d\n",*(&meiGuiTai)); printf("通過地址訪問juHuaTai的值=%d\n",*p); //取內存printf("通過地址訪問juHuaTai的值=%d\n",*p2);system("pause");return 0; }

運行結果:

juHuaTai=10
meiGuiTai=88
juHuaTai的地址是:0060FEF4
meiGuiTai的地址是:0060FEF0
通過地址訪問juHuaTai的值=10
通過地址訪問meiGuiTai的值=88
通過地址訪問juHuaTai的值=10
通過地址訪問juHuaTai的值=88
請按任意鍵繼續. . .

指針分類型&指針與數組

#include <stdio.h> #include <stdlib.h> int main() {int a=10;char c='s';int *p;char *p2;p=&a;//p=&c判斷計算機的大小端p2=&c;printf("a的值是%d\n",a);printf("c的值是%c\n",c);printf("a的地址是%p\n",p);printf("a的++地址是%p\n",++p);printf("c的地址是%p\n",p2);printf("c的++地址是%p\n",++p2);int array[3]={1,2,3};//數組 關心首地址 1.數組名array2.第一個元素的地址&array[0]printf("第一個數組元素的地址%p\n",&array[0]);printf("第二個數組元素的地址%p\n",&array[1]);printf("第三個數組元素的地址%p\n",&array[2]);//遍歷數組int i;for(i=0;i<3;i++){printf(" %d ",array[i]);}int *parray;parray=array;/*printf("\n第一個數組元素的地址%p\n",parray++);printf("第二個數組元素的地址%p\n",parray++);printf("第三個數組元素的地址%p\n",parray++);parray=array;*/for(i=0;i<3;i++){printf(" %d ",*parray++); // * 優先級 > ++ 優先級}parray=array;for(i=0;i<3;i++){printf(" %d ",parray[i]);}system("pause");return 0; }

運行結果:

a的值是10
c的值是s
a的地址是0060FEDC
a的++地址是0060FEE0
c的地址是0060FEDB
c的++地址是0060FEDC
第一個數組元素的地址0060FECC
第二個數組元素的地址0060FED0
第三個數組元素的地址0060FED4
1 2 3 1 2 3 1 2 3 請按任意鍵繼續. . .

指針數組函數的綜合
此例代碼主要將上述某代碼函數的數組形參改為指針形參。

#include <stdio.h> #include <stdlib.h>void initScores(int *datas,int size) {int i;for(i=0;i<size;i++){printf("請輸入第%d個學生的分數:",i+1);scanf("%d",datas); //用指針就不用取地址(&)if(*datas<0 || *datas>100){printf("我信你個大頭鬼\n");printf("tui~\n");system("pause");exit(-1);}datas++;}}void printScores(int *datas,int size) {int i;printf("學生分數如下:\n");for(i=0;i<size;i++){printf("第%d個學生的分數是%d\n",i+1,*datas++);}}int scoresMax(int *datas,int size) {int i;int max;max=*datas++;//初始賦值for(i=1;i<size;i++) //比較{if(max < *datas){max=datas[0];//指針后移 不能交叉使用datas[i]}datas++;}return max; }int scoresMin(int *datas,int size) {int i;int min;min=*datas++;//初始賦值for(i=1;i<size;i++) //比較{if(min>*datas){min=datas[i];}}return min; }float scoresAver(int datas[], int size) {int sum;int i;sum=0;for(i=0;i<size;i++){sum+=*datas++;}//printf("%d",sum);return (float)sum/size; }printRet(int data1,int data2,float data3) {printf("分數最大值:%d\n分數最小值:%d\n分數平均值%f\n",data1,data2,data3); }int main() {int scores[10]={0};int len;int max;int min;float average;len=sizeof(scores)/sizeof(scores[0]);//1.初始化學生分數initScores(scores,len);//2.輸出學生分數printScores(scores,len);//3.計算分數最大值max=scoresMax(scores,len);//4.計算分數最小值min=scoresMin(scores,len);// 5.計算分數平均值average=scoresAver(scores,len);// 6.輸出結果printRet(max,min,average);system("pause");return 0; }

運行結果:

請輸入第1個學生的分數:12
請輸入第2個學生的分數:56
請輸入第3個學生的分數:60
請輸入第4個學生的分數:100
請輸入第5個學生的分數:90
請輸入第6個學生的分數:88
請輸入第7個學生的分數:80
請輸入第8個學生的分數:70
請輸入第9個學生的分數:30
請輸入第10個學生的分數:10
學生分數如下:
第1個學生的分數是12
第2個學生的分數是56
第3個學生的分數是60
第4個學生的分數是100
第5個學生的分數是90
第6個學生的分數是88
第7個學生的分數是80
第8個學生的分數是70
第9個學生的分數是30
第10個學生的分數是10
分數最大值:100
分數最小值:12
分數平均值59.599998
請按任意鍵繼續. . .

為什么用指針
1.在指定地址存儲數據

#include <stdio.h> #include <stdlib.h>/* 為什么要用指針 *///能不能 a(10) 強制保存我要的地址 0060FEF8 javaint main() {int a; //a的地址肯定是隨機分配a=10;int *p;p=&a;printf("a的地址是%p\n",p);int *p2=(int *)0x0060FE00;//int a=10 ARM架構 裸機編程 ARM驅動*p2=10;printf("在內存%p的位置存放的的是數值%d\n",p2,*p2);//volatile int *p2 = (volatile int *)0x0060FEF8 //類型修飾符/*volatile 多線程編程每次從內存中取數據,而不是寄存器() (快)*/system("pause");return 0; }

運行結果:

a的地址是0060FEF4 在內存0060FE00的位置存放的的是數值10 請按任意鍵繼續. . .

2.在自定義函數中通過指針形參對主函數數據本身操作。

#include <stdio.h> #include <stdlib.h>void jiaJiaA(int a) {a++;printf("jiaJiaA里面的值是%d\n",a); }void jiaJiaA2(int *p) {*p=*p+1;printf("jiaJiaA2里面的值是%d\n",*p); }void swap(int *a,int *b) {int temp;temp=*a;*a=*b;*b=temp; } int main() {/*int a=10;jiaJiaA(a); //main中a不變 10//jiaJiaA2(&a); //main中a變化 11printf("main函數里面a的值是%d\n",a);*/int a=1;int b=5;printf("a and b %d %d\n",a,b);swap(&a,&b);printf("a and b %d %d\n",a,b);system("pause");return 0; }

運行結果:

a and b 1 5
a and b 5 1
請按任意鍵繼續. . .

指針數組
能將無關數據組合到一起。

#include <stdio.h> #include <stdlib.h> int main() {int a = 1;int b = 2;int c = 3; //三個毫無關系的整型變量int array[3]; //多個整數叫整數數組int* p[3];//這樣來定義指針數組int* parray[3]; //多個指針叫指針數組 數組中每一個元素都是指針變量//指針變量存放的是地址的變量parray[0] = &a;parray[1] = &b;parray[2] = &c; //三個普通沒有任何關系的 整型變量的地址 存入指針數組int i;for(i=0;i<3;i++){printf(" %d ",*(parray[i]));}system("pause");return 0; }

運行結果:

1 2 3 請按任意鍵繼續. . .

數組指針

#include <stdio.h> #include <stdlib.h>//上例講的是指針數組,多個指針 //本例說的可是“數組指針” 一個指針int main() {int a[3]={0,1,2};int (*p)[3];p=a;int *p2;p2 = a;printf("數組的地址是%p\n",a);printf("數組的地址是%p\n",&a[0]);printf("p數組的地址是%p\n",p);printf("p2數組的地址是%p\n",p2);//0060FEDCprintf("==================區別=================\n");printf("++p的結果是%p\n",++p); //0060FEE0 ++p 數組指針自加//0060FED4 p 首地址// =12/* 二者自加偏移量不同:數組指針偏移整個數組大小的長度普通(指向數組)指針自加偏移一個數組元素大小長度 */printf("++p2的結果是%p\n",++p2);//0060FED8 ++p2 普通(指向數組)指針自加//0060FED4 p2 首地址//=4//printf() int *p3;p3=a;int i;for(i=0;i<3;i++){printf(" %d ",*p3++);}system("pause");return 0; }

運行結果:

數組的地址是0060FED4
數組的地址是0060FED4
p數組的地址是0060FED4
p2數組的地址是0060FED4
================== 區別 =================
++p的結果是0060FEE0
++p2的結果是0060FED8
0 1 2 請按任意鍵繼續. . .

函數指針

#include <stdio.h> #include <stdlib.h>void printWelcome() {printf("歡迎昌老濕\n"); }int add(int a,int b) {return a+b; }int main() {//1.如何定義一個函數指針void (*p)();//1.如何表示指針:* //2.如何知道是函數 //3.函數指針是專用的,格式要求很強(參數類型,個數,返回值)就像數組指針一樣//2.如何給函數指針賦值p=printWelcome; //函數名就是地址,就像地址一樣,數組名就是地址//3.如何通過函數指針調用函數p();//直接通過名字+()(*p)();//取內容 (*指針名字)()int (*padd)(int a,int b); //嚴格按照函數聲明原型定義函數指針padd = add; //為函數指針賦值int ret = (*padd)(1,5); //通過函數指針調用函數printf(" %d \n",ret);system("pause");return 0; }

運行結果:

歡迎昌老濕
歡迎昌老濕
6
請按任意鍵繼續. . .

無類型指針 &malloc& 避免內存泄漏

#include <stdio.h> #include <stdlib.h> //無類型指針int main() {int n;printf("請輸入int型數組元素個數\n");scanf("%d",&n);int *parray = (int*)malloc(n * sizeof(int));//我尼瑪是可變長度數組int i;for(i=0;i<n;i++){printf("請輸入第%d個學生的sb分數:\n",i+1);scanf("%d",&parray[i]);}for(i=0;i<n;i++){printf("第%d個學生的sb分數是:%d\n",(i+1),parray[i]);}/*避免內存泄漏1.程序剛跑起來 很好,跑幾個小時,或者幾天,或幾周 程序崩潰while(1){sleep(1);int *p = malloc(1024);//malloc 申請的空間,程序不會自動釋放。linux中的話,程序結束后,系統會自動回收這個空間。避免:1.注意,循環中有沒有一直申請2.及時合理釋放 free(p); p=NULL;}int *p;野指針定義時最好初始化*/ free(parray);system("pause");return 0; }

運行結果:

請輸入int型數組元素個數
5
請輸入第1個學生的sb分數:
10
請輸入第2個學生的sb分數:
20
請輸入第3個學生的sb分數:
30
請輸入第4個學生的sb分數:
40
請輸入第5個學生的sb分數:
50
第1個學生的sb分數是:10
第2個學生的sb分數是:20
第3個學生的sb分數是:30
第4個學生的sb分數是:40
第5個學生的sb分數是:50
請按任意鍵繼續. . .

指針收官

#include <stdio.h> #include <stdlib.h> int main() {int c; int *p;int a[5];int *pa[4];//指針數組int (*pb)[4];//數組指針int f();int *fun();//返回指針的函數int (*pf)();//指向函數的指針int **pc;void *pd;system("pause");return 0; }

七、字符串

字符串定義初始化
定義字符串四種方式
第一種:char str[5]={‘a’,‘b’,‘c’,‘d’,‘e’};//類比于整數數組
第二種:char str2[5]=“abcde”;
第三種:char str3[]=“aaaaaasdkjhgfdytrv”;//數組原屬個數不寫,會根據真是大小默認分配
第四種:char *str4=" hello world !!";//指針方式

#include <stdio.h> #include <stdlib.h> int main() {//C語言字符串 定義以及初始化int i;int a[3]={0,2,3};char c;//定義字符串的第一種方法,和整型數組類似! 傻逼法!!!!char str[5]={'a','b','c','d','e'};//類比于整數數組for(i=0;i<sizeof(str)/sizeof(str[0]);i++){printf(" %c ",str[i]);}putchar('\n');//定義字符串的第二種方式:和整型數組類似,沒那末傻逼!!!!char str2[5]="abcde";for(i=0;i<sizeof(str2)/sizeof(str2[0]);i++){printf(" %c ",str[i]);}putchar('\n');//定義字符串的第三種方式:和整數數組類似沒那末傻逼char str3[]="aaaaaasdkjhgfdytrv";//數組原屬個數不寫,會根據真是大小默認分配for(i=0;i<sizeof(str3)/sizeof(str3[0]);i++){printf(" %c ",str3[i]);}putchar('\n');//定義字符串數組的第四種方式://數組名就是地址(大多數情況下)char *str4=" hello world !!";//指針方式 如果操作不當 內存非法 易造成段錯誤sigment errorprintf("%s\n",str4);//字符串用格式占位符%s,不需要用i遍歷puts("");puts(str);puts(str2);puts(str3);puts(str4);system("pause");return 0; }

運行結果:

a b c d ea b c d ea a a a a a s d k j h g f d y t r vhello world !!abcde abcdeabcde aaaaaasdkjhgfdytrvhello world !! 請按任意鍵繼續. . .

字符串數組大小

不能使用sizeof計算字符串中有效字符的個數!! 應該要用strlen,她在計算字符串大小時,遇到’\0’就結束計數。sizeof是計算在內存中的字節數

#include <stdio.h> #include <stdlib.h> int main() {char a[]="changxiatrui";printf("a數組大小%d\n",sizeof(a));printf("a數組大小%d\n",strlen(a));//strlen 計算有效字符大小char *a2="changxianruib";printf("a2字符串大小%d\n",sizeof(a2));//計算指針 :(printf("a2字符串大小%d\n",strlen(a2));//不能使用sizeof計算字符串中有效字符的個數!! 應該要用strlen,她在家算字符串大小時,遇到'\0'就結束計數char a3[20]="hello\0jkl";//后面一堆'\0'printf("a3數組大小%d\n",sizeof(a3)/sizeof(a3[0]));printf("希望得到a3大小%d\n",strlen(a3));printf("打印字符串%s\n",a3);system("pause");return 0; }

運行結果:

a數組大小13
a數組大小12
a2字符串大小4
a2字符串大小13
a3數組大小20
希望得到a3大小5
打印字符串hello
請按任意鍵繼續. . .

字符串常用函數一

#include <stdio.h> #include <stdlib.h> int main() {char *str = "而非很快就去";/*puts(str);printf("%d\n",strlen(str)); // 12 一個漢字占兩個字節*///char pstr[128]={'\0'};//1.申請空間 2.初始化 把每個元素都初始化為'\0'char *pstr;//野指針 造成非法內存訪問,出現段錯誤 cmd窗口閃退pstr=(char *)malloc(128);//1.申請了空間 2.一旦用了malloc,一定要注意內存泄漏的問題, 3.malloc可能會失敗,要對返回值做判斷if(pstr == NULL){printf("申請內存失敗\n");exit(-1);}memset(pstr,'\0',128);////1.pstr 初始化的對象 2.初始化成什么字符 3.多大printf("請輸入字符串\n");//scanf("%s",pstr);gets(pstr);puts(pstr);system("pause");return 0; }

運行結果:

請輸入字符串
c is the best laug
c is the best laug
請按任意鍵繼續. . .

字符串常用函數二

strcpy是一種C語言的標準庫函數,strcpy把含有’\0’結束符的字符串復制到另一個地址空間,返回值的類型為char*。

strncpy函數用于將指定長度的字符串復制到字符數組中,是 C語言的庫函數之一,來自 C語言標準庫,定義于 string.h。語法形式為:char *strncpy(char *dest, const char *src, int n),表示把src所指向的字符串中以src地址開始的前n個字節復制到dest所指的數組中,并返回被復制后的dest。

strcat函數將兩個char類型連接。例如:
char d[20]=“Golden”;
char s[20]=“View”;
strcat(d,s);
//打印d
printf(“%s”,d);
輸出 d 為 GoldenView (中間無空格)d和s所指內存區域不可以重疊且d必須有足夠的空間來容納s的字符串。
返回指向d的指針。

strcmp函數是string compare(字符串比較)的縮寫,用于比較兩個字符串并根據比較結果返回整數。基本形式為strcmp(str1,str2),若str1=str2,則返回零;若str1<str2,則返回負數;若str1>str2,則返回正數。

#include <stdio.h> #include <stdlib.h> int main() {//char strDest[128] ={'\0'};//第一個 \0 之后全是 \0char *strDest; //01.定義strDest=(char *)malloc(128); //02.申請內存空間memset(strDest,'\0',128); //03.初始化char *strSrc = " cxr hs";strcpy(strDest,strSrc);printf("復制后的字符串:%s\n",strDest);/* //字符串拷貝的兩種方法memset(strDest,'\0',sizeof(strDest)/sizeof(strDest[0]));strcpy(strDest,"hello");puts(strDest); //hellomemset(strDest,'\0',sizeof(strDest)/sizeof(strDest[0]));char *str = (char *)malloc(10 * sizeof(char));memset(str, '\0', 10);str=strncpy(strDest,"he llo",5);puts(str);puts(strDest); //he ll*///字符串連接char test[128]=" 沒毛病 ";strcat(strDest,test);puts(strDest);//字符串比較char *str1="123";char *str2="jkl";int ret = strcmp(str1,str2);if(ret == 0){printf("一樣大小\n");}else{printf("不一樣大小\n");}system("pause");return 0; }

運行結果:

復制后的字符串: cxr hs
cxr hs 沒毛病
不一樣大小
請按任意鍵繼續. . .

字符串常用函數三
strchr函數功能為在一個串中查找給定字符的第一個匹配之處。函數原型為:char *strchr(const char *str, int c),即在參數 str 所指向的字符串中搜索第一次出現字符 c(一個無符號字符)的位置。strchr函數包含在C 標準庫 <string.h>中。

strstr(str1,str2) 函數用于判斷字符串str2是否是str1的子串。如果是,則該函數返回 str1字符串從 str2第一次出現的位置開始到 str1結尾的字符串;否則,返回NULL。

strlwr函數的功能是將字符串中的S參數轉換為小寫形式。

strupr,函數的一種,將字符串s轉換為大寫形式。

strotk函數,分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符(如果傳入字符串,則傳入的字符串中每個字符均為分割符)。首次調用時,s指向要分解的字符串,之后再次調用要把s設成NULL。

#include <stdio.h> #include <stdlib.h> int main() {char *str = "changxianrui";char c = 'a';char *p = NULL;/*//查找字符p = strchr(str,c);if(p==NULL){printf("沒有找到\n"); //puts(p); }else{printf("找到了 為:");puts(p); //輸出后面的字符串 angxianrui}*///字符串中查找子串/*char *zi="xian";p=strstr(str,zi);if(p==NULL){printf("沒有字串\n");}else{printf("字串存在\n");puts(p); //xianrui}*///字符串大小寫轉換/*char v[]="Hello World";p=strlwr(v);puts(p); // hello worldputs(strupr(v));// HELLO WORLD*///zi字符串分割int i = 0 ;char *pubs[3];char s[]="chang,xian,rui";p=strtok(s,",");//''既然不行if(p!=NULL){i++;//printf("第%d個子串是%s\n",i,p) ;pubs[i-1] = p;}while(1){p=strtok(NULL,",");if(p!=NULL){i++;//printf("第%d個子串是%s\n",i,p) ;pubs[i-1] = p;}else{printf("沒有子串了\n");break;}}int j;for(j = 0; j <= i; j++){puts(pubs[j]);}/*if(p!=NULL){printf("第一個子串是%s\n",p) ;}//獲取第二個字串的方式很奇葩 跟第一次不一樣的是,目標字符串表示為NULL;p=strtok(NULL,","); printf("第二個字串是%s\n",p);//獲取第三個字串的方式很奇葩 跟第一次不一樣的是,目標字符串表示為NULL;p=strtok(NULL,",");printf("第三個字串是%s\n",p); //獲取第四個字串的方式很奇葩 跟第一次不一樣的是,目標字符串表示為NULL;p=strtok(NULL,",");if(p == NULL){printf("沒有子串了\n");}else{printf("第四個字串是%s\n",p); }*/system("pause");return 0; }

運行結果:

沒有子串了
chang
xian
rui
請按任意鍵繼續. . .

八、結構體

結構體

#include <stdio.h> #include <stdlib.h>//struct關鍵字 //1.結構體的定義和使用 @!!!!提醒:新手容易忘記結構體后的; //2.如何訪問結構體: 目標 內部的變量 struct Student//字定義類型 {int score; //特性:分數char name[128]; //特性:名字//void (*pintroduce)(char *pnme); //行為:函數指針(自我介紹);};int main() {//類型 變量 初始值int a = 10;struct Student stu1= {59,"昌顯瑞"};printf("a=%d\n",a);printf("結構體中的score=%d\n",stu1.score);printf("結構體中的name=%s\n",stu1.name);putchar('\n');int b;b=10;struct Student test;test.score=12;//test.name="小紅";//字符串只能 通過復制strcpy(test.name,"小紅");printf("結構體中的score=%d\n",test.score);printf("結構體中的name=%s\n",test.name);system("pause");return 0; }

結構體的定義和使用:

#include <stdio.h> #include <stdlib.h>//struct關鍵字 //1.結構體的定義和使用 @!!!!提醒:新手容易忘記結構體后的; //2.如何訪問結構體: 目標 內部的變量 struct Student//字定義類型 {int score; //特性:分數char name[128]; //特性:名字//void (*pintroduce)(char *pnme); //行為:函數指針(自我介紹);};int main() {//類型 變量 初始值int a = 10;struct Student stu1= {59,"昌老濕"};printf("a=%d\n",a);printf("結構體中的score=%d\n",stu1.score);printf("結構體中的name=%s\n",stu1.name);putchar('\n');int b;b=10;struct Student test;test.score=12;//test.name="小紅";//字符串只能 通過復制strcpy(test.name,"小紅");printf("結構體中的score=%d\n",test.score);printf("結構體中的name=%s\n",test.name); system("pause");return 0; }

運行結果:

a=10 結構體中的score=59 結構體中的name=昌老濕結構體中的score=12 結構體中的name=小紅 請按任意鍵繼續. . .

結構體:舊知識新用法

#include <stdio.h> #include <stdlib.h> //jie結構體里面的東西都是熟悉的,外面只是一個框架 舊知識新用法而已void func(int data) {printf("數據:%d\n",data); } struct Student {char *name;char sex;int age;float score;char *adress;void (*peat)(); //函數指針void (*itroduce)(char *name,char *adress,int age); };int main() {struct Student stu1;system("pause");return 0; }

野指針是致命問題

#include <stdio.h> #include <stdlib.h>struct Datas //結構體:不建議這種方式存字符串 {char *p; //必須 malloc };struct Test //結構體:建議這種方式存字符串 {char p[128]; };int main() {/*struct Datas d; //指針strcpy(d.p,"就立刻ncv"); //Datas linux下段錯誤puts(d.p); //程序崩掉 :因為是 野指針 沒有內存*//*struct Test d; //數組strcpy(d.p,"就立刻ncv"); //puts(d.p); //程序不崩*/struct Datas d;d.p=(char *)malloc(128);memset(d.p,'\0',128);strcpy(d.p, "你好,世界。");puts(d.p);printf("%s\n",d.p);system("pause");return 0; }

運行結果:

你好,世界。
你好,世界。
請按任意鍵繼續. . .

結構體數組玩成績

#include <stdio.h> #include <stdlib.h>struct Student {int score;char *name;};int main() {int i;struct Student stus[5];struct Student maxStu,minStu;for(i=0;i<sizeof(stus)/sizeof(stus[0]);i++){printf("請輸入第%d個學生的名字\n",i+1);stus[i].name = (char *)malloc(128);scanf("%s",stus[i].name);printf("請輸入第%d個學生的分數\n",i+1);//stus[i].name = (char *)malloc(128);scanf("%d",&stus[i].score);}for(i=0;i<sizeof(stus)/sizeof(stus[0]);i++){printf("輸出第%d個學生的名字和分數",i+1);printf("%s:%d\n",stus[i].name,stus[i].score);}maxStu = minStu = stus[0];for(i=1;i<sizeof(stus)/sizeof(stus[0]);i++){if(minStu.score > stus[i].score){minStu = stus[i];}if(maxStu.score < stus[i].score){maxStu = stus[i];}//printf("%s:%d\n",stus[i].name,stus[i].score);}printf("分數最高的人%s:%d\n",maxStu.name,maxStu.score);printf("分數低的人%s:%d\n",minStu.name,minStu.score);system("pause");return 0; }

運行結果:

請輸入第1個學生的名字
one
請輸入第1個學生的分數
50
請輸入第2個學生的名字
two
請輸入第2個學生的分數
60
請輸入第3個學生的名字
three
請輸入第3個學生的分數
70
請輸入第4個學生的名字
four
請輸入第4個學生的分數
80
請輸入第5個學生的名字
five
請輸入第5個學生的分數
90
輸出第1個學生的名字和分數one:50
輸出第2個學生的名字和分數two:60
輸出第3個學生的名字和分數three:70
輸出第4個學生的名字和分數four:80
輸出第5個學生的名字和分數five:90
分數最高的人five:90
分數低的人one:50
請按任意鍵繼續. . .

結構體指針

#include <stdio.h> #include <stdlib.h>struct Student {int score;char name[128]; };int main() {struct Student stu1;stu1.score=10;strcpy(stu1.name,"zhangsan");printf("%s : %d\n",stu1.name,stu1.score);struct Student *p;//野指針p=(struct Student *)malloc(sizeof(struct Student));//p.score錯 //如果用結構體指針就不能用點運算符來訪問結構體中的變量 要用->p->score=20;strcpy(p->name,"liwu");printf("%s : %d\n",p->name,p->score);free(p);/*free()只是將malloc()函數申請的空間釋放掉,并不能將指針置空,指針的指向還是之前的,并不會改變,所以用指針與NULL比較作為循環的結束條件在雙鏈表之中是不適合的,會導致死循環的產生。*/p=&stu1;printf("%s : %d\n",p->name,p->score);printf("地址:%p\n",p++); //0060FE68 + 132 =printf("之后地址:%p\n",p); //0060FEECsystem("pause");return 0; }

運行結果:

zhangsan : 10
liwu : 20
zhangsan : 10
地址:0060FE68
之后地址:0060FEEC
請按任意鍵繼續. . .

結構體指針玩成績

#include <stdio.h> #include <stdlib.h>struct Student {int score;char *name;};int main() {int len;int i;printf("請輸入個數:n=");scanf("%d",&len);//struct Student stus[3];//struct Student *p;struct Student *p=(struct Student *)malloc(sizeof(struct Student)*len);//p=stus;for(i=0; i<len; i++){p->name = (char *)malloc(128);printf("請輸入第%d個學生的名字\n",i+1);scanf("%s",p->name);printf("請輸入第%d個學生的分數\n",i+1);scanf("%d",&(p->score));p++;}p = p-len;for(i=0; i<len; i++){printf("第%d個學生 %s : %d\n",i+1, p->name, p->score); p++;}system("pause");return 0; }

運行結果:

請輸入個數:n=3
請輸入第1個學生的名字
one
請輸入第1個學生的分數
77
請輸入第2個學生的名字
two
請輸入第2個學生的分數
88
請輸入第3個學生的名字
three
請輸入第3個學生的分數
99
第1個學生 one : 77
第2個學生 two : 88
第3個學生 three : 99
請按任意鍵繼續. . .

結構體、指針、函數綜合處理學生成績

#include <stdio.h> #include <stdlib.h>struct Student{char *name;int score; };//初始化函數 struct Student *initStudent(int len) {int i;printf("開始錄入:\n");struct Student *p = (struct Student *)malloc(len*sizeof(struct Student));//在隊上面開辟空間,函數結束調用不會釋放for(i=0;i<len;i++){//memset(p->name,'\0',128);printf("請輸入第%d個學生姓名:\n",i+1);p->name = (char *)malloc(64);scanf("%s",p->name);printf("請輸入第%d個學生分數:\n",i+1);scanf("%d",&(p->score));p++;}return p-len; }void printStu(struct Student *p,int len) {int i;for(i=0;i<len;i++){printf("%s : %d\n",p->name,p->score);p++;}}struct Student *minStu(struct Student *p,int len) {int i=0;struct Student *min = NULL;min=p;for(i=0;i<len;i++){if(min->score > p->score){min=p;}p++;}return min; }struct Student *maxStu(struct Student *p,int len) {int i=0;struct Student *max = NULL;max=p;for(i=0;i<len;i++){if(max->score < p->score){max=p;}p++;}return max; } //平均值 float averStu(struct Student *p,int len) {int i=0;int sum=0;for(i=0;i<len;i++){sum += p->score;p++;}return (float)sum/len; }int findStudnet(struct Student *p,int len,char *name) {int i=0;for(i=0;i<len;i++){if(strcmp(p->name,name)==1){return 1;}p++;}return -1; }int main() {int len=0;printf("請輸入錄入學生個數");scanf("%d",&len);//長度輸入struct Student *pstus=NULL;struct Student *min=NULL;struct Student *max=NULL;float aver=0;//初始化pstus = initStudent(len);//輸出printStu(pstus,len);//找最大最小值min=minStu(pstus,len);max=maxStu(pstus,len);//平均值aver=averStu(pstus,len);//輸出printf("min %s : %d\nmax %s : %d\naver : %f\n",min->name,min->score,max->name,max->score,aver);if(findStudnet(pstus,len,"zhangsan") == 1){printf("找到\n");}else{printf("沒找到\n");}system("pause");return 0; }

運行結果:

請輸入錄入學生個數3
開始錄入:
請輸入第1個學生姓名:
one
請輸入第1個學生分數:
100
請輸入第2個學生姓名:
two
請輸入第2個學生分數:
50
請輸入第3個學生姓名:
there
請輸入第3個學生分數:
30
one : 100
two : 50
there : 30
min there : 30
max one : 100
aver : 60.000000
沒找到
請按任意鍵繼續. . .

typedef關鍵字

#include <stdio.h> #include <stdlib.h> /*typedef 重命名作用:為一種數據類型定義一個新的名字這里的數據類型包括內部數據類型(int,char等)和自定義數據類型(struct等)和struct來匹配偽類代碼編寫簡潔和普通類型匹配,通過名字來獲取一些信息 *///在單片機開發中,寄存器有8位 16位 32位typedef unsigned char u_int8; //(0,255) typedef unsigned short int u_int16; typedef unsigned int u_int32;//typedef struct Student STU,*PSTU;不建議typedef struct Student{char *name;int score;void (*p)(struct Student *); //結構體里面最好不用STU }STU,*PSTU;int main() {PSTU p=NULL;//STU p;p=(PSTU)malloc(sizeof(STU));p->score=99;printf("分數:%d\n",p->score); u_int8 data=10;unsigned short int data2=20;u_int32 data3=30;printf("%d %d %d \n",data,data2,data3);system("pause");return 0; }

運行結果:

分數:99
10 20 30
請按任意鍵繼續. . .

總結

以上是生活随笔為你收集整理的C语言笔记含源码(变量、输入输出、分支、循环、函数、数组、指针、字符串、结构体)小总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。