C語言精簡代碼

const的用法

#include <stdio.h>

//遊戲、不想被其他人修改自己寫的代碼,要用const

int main()

{

const int a=1000;

//a=200;//a屬於常量,不能修改

int b=10000,c=20;

const int* p=&b;//常量指針

//*p=1;//不能通過其目標去修改

p=&c;

printf("%d\n",*p);

int* const p1=&b;//指針常量

//p1=&c;//不能通過其地址去修改

*p1=1000000;

printf("%d\n",*p1);

const int* const p2=&b;//常量指針常量

//*p2=300;//不能通過其目標去修改

//p2=&c;//不能通過其地址去修改

return 0;

}

模擬計算機尋找某個文件
#include <stdio.h>

int main()

{

char st[20];//相當於新建一個文件夾

char* ps;

int i,j=0;//i用來循環遍歷我們的文件夾,j用來統計文件出現了多少次

printf(“請輸入一個字符串:\n”);

ps=st;//指向數組的首地址

scanf("%s",ps);

for(i=0;ps[i]!=’\0’;i++)//文件夾的遍歷

if(ps[i]==‘7’)

printf(“文件夾中出現‘7’字符%d次\n”,++j);

if(j==0)

printf(“文件夾中沒有‘7’字符\n”);

printf(“文件夾中共出現‘7’字符%d次\n”,j);

system(“pause”);//暫停 getch() 等待輸入

return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章