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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言基础语法【1】

發(fā)布時(shí)間:2024/3/7 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言基础语法【1】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

1、在linux系統(tǒng)編寫C語言,基本命令;變量、常量、標(biāo)識(shí)符

2、數(shù)據(jù)類型、取值范圍、字符串

3、短路原則、類型轉(zhuǎn)換

4、switch語句、for、while循環(huán)、死循環(huán)


1、在linux系統(tǒng)編寫C語言,基本命令;變量、常量、標(biāo)識(shí)符

/*C語言 Linux系統(tǒng)中進(jìn)行操作 創(chuàng)建目錄后 切換進(jìn)去vi 編譯文件名 保存退出后 gcc 編譯文件名 -o 運(yùn)行文件名 就在目錄創(chuàng)建好了 ./運(yùn)行文件名 運(yùn)行完成*/ /*第一個(gè)程序*/ [wlp@192 桌面]$ mkdir C_xuexi [wlp@192 桌面]$ cd C_xuexi [wlp@192 C_xuexi]$ mkdir class_1 [wlp@192 C_xuexi]$ cd class_1 [wlp@192 class_1]$ vi test.c #include <stdio.h> int main(){printf("Hello Word!\n");return 0; } [wlp@192 class_1]$ gcc test.c -o test [wlp@192 class_1]$ ls test test.c [wlp@192 class_1]$ ./test Hello Word!

/*變量包含英文字母 數(shù)字 特殊符號(hào) _不能以數(shù)字開頭 不能使用關(guān)鍵字 數(shù)據(jù)類型int 整型char 字符型float 單精度浮點(diǎn)型double 雙精度浮點(diǎn)型 占位符*/ [wlp@192 class_3]$ vi test.c #include <stdio.h> int main(){int a;char b; float c;double d; a=520;b='f'; c=3.14;d=3.141592653;printf("輸出%d\n",a);printf("輸出%c\n",b);printf("輸出%.2f\n",c);//小數(shù)點(diǎn)后精確2位printf("輸出%11.9f\n",d); //一共11個(gè)站位,小數(shù)點(diǎn)后精確9位return 0; } [wlp@192 class_3]$ gcc test.c -o test [wlp@192 class_3]$ ./test 輸出520 輸出f 輸出3.14 輸出3.141592653 ========================================================= /*常量: 整型常量: 520 實(shí)型常量: 5.2 字符常量:普通字符:'L','o','v','e'轉(zhuǎn)義字符:'\n'【換行】,'\t','\b' 字符串常量:“Fi sh”'F' 'i' ' ' 's' 'h' '\0'【內(nèi)存存儲(chǔ)情況 所以會(huì)追加一個(gè)轉(zhuǎn)意字符代表字符串結(jié)束 需要6個(gè)站位 】符號(hào)常量:使用前必須定義格式:#define 標(biāo)識(shí)符 常量習(xí)慣上符號(hào)常量標(biāo)識(shí)符用大寫字母,變量標(biāo)識(shí)符用小寫字母。將標(biāo)識(shí)符換成后面的常量如:-#define URL "我"*/ [wlp@192 class_4]$ vi test.c #include <stdio.h>#define URL "wo" #define Name "miao" #define Year 2010 #define Month 5 #define Day 5int main(){printf("%s成立于%d年%d月%d日\(chéng)n",Name,Year,Month,Day);printf("%s是%s創(chuàng)立的。。。\n",Name,URL);printf("%s的域名是%s\n",Name,URL);return 0; } [wlp@192 class_4]$ gcc test.c -o test &&./test miao成立于2010年5月5日 miao是wo創(chuàng)立的。。。 miao的域名是wo=========================================================== /* 標(biāo)識(shí)符:包含英文字母 數(shù)字 特殊符號(hào) _不能以數(shù)字開頭 不能使用關(guān)鍵字*/

2、數(shù)據(jù)類型、取值范圍、字符串

/* 數(shù)據(jù)類型:基本類型: 整數(shù)類型:short int,int,long int,long long int浮點(diǎn)數(shù)類型:float,double,long double字符類型:char布爾類型:_Bool枚舉類型:enum指針類型:構(gòu)造類型:空類型:sizeof運(yùn)算符:用于獲取數(shù)據(jù)類型或表達(dá)式的長(zhǎng)度【站位】-sizeof(object); //sizeof(對(duì)象);-sizeof(type_name); //sizeof(類型);--sizeof object; //sizeof 對(duì)象;*/=====================================================================#include <stdio.h>int main(){int i;char j;float k;i=123;j='C';k=3.14;printf("size of int is %d\n",sizeof(int));printf("size of i is %d\n",sizeof(i));printf("size of char is %d\n",sizeof(char));printf("size of j is %d\n",sizeof j);printf("float的size是%d\n",sizeof(float));printf("k的站位是%d\n",sizeof(k));return 0; } [wlp@192 class_5]$ gcc test.c -o test && ./test size of int is 4 size of i is 4 size of char is 1 size of j is 1 float的size是4 k的站位是4============================================================================ /*取值范圍: signed和unsigned:帶符號(hào)位可以存有符號(hào),如:負(fù)數(shù);不帶符號(hào)位不可以存符號(hào) 比特位:CPU能讀懂的最小單位——比特位,bit,b ;1bit存放1位二進(jìn)制數(shù); 字節(jié):1 Byte==8 bitsigned的數(shù)據(jù)存儲(chǔ)8位比特,有符號(hào)時(shí)第一位為1,所以存儲(chǔ)范圍就比第一位為0的小許多 負(fù)數(shù):一個(gè)32位的整型變量,除去左邊第一位符號(hào)位,剩下的表示值只有31個(gè)比特位;2的31次方-1 因此,一般用正數(shù)的補(bǔ)碼表示負(fù)數(shù) 一般整數(shù)類型,默認(rèn)【不寫】都是帶符號(hào)的。所以寫上unsigned可以極大提高取值范圍。*/#include <stdio.h>int main(){short i;unsigned short j;i= -1;j= -1;printf("%d\n",i);printf("%u\n",j);return 0;} [wlp@192 C_xuexi]$ gcc test1.c -o test && ./test -1 65535 //出錯(cuò) ============================================================================= /*字符串聲明*///1、顯示ASCII碼 #include <stdio.h>int main(){char a='C';printf("%c=%d\n",a,a);return 0; }[wlp@192 class_1]$ gcc test1.c && ./a.out C=67 //2 #include <stdio.h>int main(){char a=70,b=105,c=115,d=104,e=67;printf("%c%c%c%c%c\n",a,b,c,d,e);return 0; } [wlp@192 class_1]$ gcc test1.c && ./a.out FishC //3、聲明字符串 #include <stdio.h>int main(){char name[]={'F','i','s','h','C','\0'};//5個(gè)字符組成的字符串【[]里數(shù)字可以忽略】char name[]="FishC";//聲明字符串【{}可以省略】printf("%s\n",name);return 0; }[wlp@192 class_1]$ gcc test1.c && ./a.out FishC FishC

3、短路原則、類型轉(zhuǎn)換

/* 算術(shù)運(yùn)算符: 單目運(yùn)算符 雙目運(yùn)算符 關(guān)系運(yùn)算符: 短路原則:*///1 #include <stdio.h> #include <math.h>int main(){int i,j,k;i=1+2;j=1+2*3;k=i+j+-1+pow(2,3);//3+7+(-1)+2的3次方printf("i=%d\n",i);printf("j=%d\n",j);printf("k=%d\n",k);return 0; }[wlp@192 class_1]$ gcc test1.c && ./a.out i=3 j=7 k=17//2 #include <stdio.h>int main(){int a=3,b=3;(a=0)&&(b=5);//左邊是假了,&&短路,就不往右邊運(yùn)行了printf("a=%d,b=%d\n",a,b);(a=1)||(b=5);//左邊是真了,||短路,就不往右邊運(yùn)行了printf("a=%d,b=%d\n",a,b);return 0; } ~ [wlp@192 class_1]$ gcc test1.c && ./a.out a=0,b=3 a=1,b=3===================================================================/*類型裝換:自動(dòng)裝換;強(qiáng)制轉(zhuǎn)換;1+2.0==1.0+2.0*///3#include <stdio.h>int main(){printf("整型輸出:%d\n",1+(int)2.1);printf("浮點(diǎn)型輸出:%f\n",1+2.0);return 0; }[wlp@192 class_1]$ gcc test1.c && ./a.out 整型輸出:3 浮點(diǎn)型輸出:3.000000 ===============================================================

4、switch語句、for、while循環(huán)、死循環(huán)

/* switch語句 分支嵌套*/ //1#include <stdio.h>int main(){ int i;printf("你多大了:");scanf("%d",&i);if(i>=18){printf("成年了!\n"); } return 0; } [wlp@192 class_1]$ gcc test1.c && ./a.out 你多大了: 18 成年了! == //2 #include <stdio.h>int main(){ int i;printf("你多大了");scanf("%d",&i);if(i>=18){printf("成年了\n"); } else{printf("慢走"); } return 0; } == //3 #include <stdio.h>int main(){int i;printf("輸入分?jǐn)?shù):");scanf("%d",&i);if(i>=90){printf("A\n"); } else if(i>=80 && i<90){printf("B"); } else if(i>=70 && i<80){printf("C\n"); } else if(i>=60 && i<70){printf("D\n"); } else{printf("E\n"); }return 0; } //4 #include <stdio.h> int main(){char ch;printf("請(qǐng)輸入成績(jī)等級(jí):");scanf("%c",&ch);switch(ch){case'A':printf("成績(jī)?cè)?0以上\n");break;case'B':printf("成績(jī)?cè)?0~89\n");break;case'C':printf("成績(jī)?cè)?0~79\n");break;case'D':printf("成績(jī)?cè)?0~69\n");break;case'E':printf("成績(jī)?cè)?9以下\n");break;default:printf("請(qǐng)輸入有效成績(jī)等級(jí)!");break; }return 0; } //5 #include <stdio.h> int main(){int a,b;printf("請(qǐng)輸入兩個(gè)數(shù):");scanf("%d %d",&a,&b);【空格可有可無,空格接收回車或空格,就可以輸入下一個(gè)參數(shù)了】if(a!=b){if(a>b){printf("%d>%d\n",a,b);}else{printf("%d<%d\n",a,b);}}else{printf("%d=%d\n",a,b);}return 0; } //6 #include <stdio.h>int main(){char isRain,isFree;printf("是否有空?Y/N");scanf("%c",&isFree);getchar(); //接收回車,就可以輸入下一個(gè)參數(shù)了printf("是否下雨?Y/N");scanf("%c",&isRain);if(isFree=='Y'){if(isRain=='Y'){printf("記得帶傘^_^\n");}else{printf("女神沒空T_T\n");}}else{printf("嗚嗚");}return 0; } /* 循環(huán)結(jié)構(gòu) while語句 do while語句 for語句 C99才允許,初始化語句定義類型*/ //7 #include <stdio.h>int main(){int i=1,sum=0;while(i<=10){sum+=i;i++;}printf("結(jié)果是:%d\n",sum);return 0;} //8 #include <stdio.h>int main(){//統(tǒng)計(jì)鍵盤輸入的一行英文句子的字符個(gè)數(shù)。int count=0;printf("請(qǐng)輸入字符");while(getchar()!='\n'){count++;}printf("總共輸入的字符數(shù):%d\n",count);return 0; } //9 #include <stdio.h>int main(){int count;for(count=0;count<10;count++){printf("愛\n");}return 0; }#include <stdio.h> int main(){int i,j;for(i=0,j=10;i<j;i++,j--){printf("%d\n",i);}return 0; } //九九乘法表 #include <stdio.h>int main(){int i,j;for(i=1;i<=9;i++){for(j=1;j<=i;j++){printf("%d*%d=%-2d ",i,j,i*j);//-2d保證至少兩個(gè)在占位符;顯得整齊些}putchar('\n');//輸出換行}return 0; }//死循環(huán) while(1){}#include <stdio.h> int main(){int count;for(;count<10;){printf("愛\n");count++;}return 0; }

C語言基礎(chǔ)語法【2】_z輸關(guān)的博客-CSDN博客

C語言基礎(chǔ)語法【3】_z輸關(guān)的博客-CSDN博客

總結(jié)

以上是生活随笔為你收集整理的C语言基础语法【1】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。