#include<stdio.h> (预操作处理) int main() / int main(void) / int main(int arge,char argv[ ])(定义主函数) { 变量定义 部分变量赋值;(初始化赋值或赋值语气) 表达式运算;(算法和结构) 输出结果; return 0;(运行结束) }
十进制:由0123456789表示—字母D 二进制:由0和1表示—字母B 八进制:由01234567表示—字母O 十六进制:由0-9 A-F表示—字母H
char(字符型),double(双精度浮点),float(单精度浮点),int(整型)
break,case,continue,default,do,else,for,goto,if,return,sizeof,switch,while
typedef,void
auto,static
1、C程序由函数构成 2、程序里必须有且只能有一个主函数 3、主函数位置任意 4、从主函数开始执行、结束 5、大括号作为函数体来定义使用 6、分号为结束标志,最后一行末尾必须有
字母、数字、下划线 (第一个字符不能为数字) 源程序(.c) — 编译(.obj)— 可执行文件(.exe) —运行程序
#include<stdio.h> int main() { printf(“hello world!\n”) return 0; }
整型:int–4个字节(十进制0123、八进制(012)、十六进制(0x123)) 字符型:char–1字节(字符:‘a’、‘3’、‘!’) (字符串:“ ”、“apple”) 字符串个数=实际字符+1 字符‘a’为一个字节, 字符串“a”为两个字节, 因为字符串“a"实际是字符’a\0’ ,\0是字符串结束标志,系统自动增加 实型:float(单精度实型)—4个字节 double(双精度实型)—8个字节 double number 空类型(void) 构造类型:数组、结构体 指针类型:( * ) int*pa指针
‘ ’(空格)对应32 、‘0’对应48 、‘A’对应65 、‘a’对应97
\n (换行)、\t (水平制表)、 \0 (空字符)、 \ddd(1-3位八进制所代表的字符)
转换依据为十进制 #include<stdio.h> int main() { char c1,c2; c1=‘A’; c2=c1+32; printf(“c1=%c,c1=%d\n”,c1,c1); printf(“c2=%c,c2=%d\n”,c2,c2); return 0; }
运行结果 c1=A,c1=65 c2=a,c2=97
#define X 3.14159(程序开始定义) int r=5 a=Xrr即为(3.1415955)
int a; 如 int a=1;或int a=1,b; int max,min;如 int a=1,b=2;(错误表达int a=b=1) double num1,num2; 字符串赋值:如 char c1=‘A’;char c1=65;
1、/(取整:8.0/4等于=2.0000、3/2=1.000000、3.0/2=1.500000)、%(取余为8%4=0)、= = 、 >= 、<= 、 !=
2、逻辑运算符:&&(与)、 ||(或)、!(非)
3、++i为i=i+1、- -i为i=i-1 =(把等号右边的值赋值给左边的变量+=、-=,/=,%=)
(1)求int a=5,b=6,c=7;表达式a= =b>c的结果 (算数运算中>的优先级高于= =,真为1,假为0) 因为a== b>c等价于a==(b>c) b>c为假为0,a==0为假,答案为0
(2)int a=5,b=1,c=3;表达式a>b+c的结果 (算数运算高于关系运算)因为5>(1+3)结果为1
(3)表达式a+b>c&&x+y<b等价于:((a+b)>c)&&((x+y)<b) 算数运算>关系运算>逻辑运算
(4)设int a=0,b=1,c;c=(a++>0)&&(b–<0)的结果 a++>0等价于0>0,为假,&&中有假全为假,答案为0
(5)设int a=0,b=1,c;c=(++a>0)II(b–<0)的结果 ++a>0等价于1>0,答案为1
(6)假定int x=28,y=15;表达式x>y?x:y的结果为? ?为条件运算符如果x>y执行x,否则执行y,答案:2
(7)假定int a=3;表达式aa+5的结果为?答案:24 a=b等价于a=ab 、 a=a+5等价于a=a*(a+5)
(8)假定double x=5.6,y=3.2;int z; z=x+(int)y的结果为?答案:8 int为强制类转换,x+(int)y=5.6+3=8.6,z为整型,答案为8
(9)设x、y、t均为整型变量,则执行语句x=y=3;t=++x||++y后y的值为:4 因为++x=4为真值,等于t,根据逻辑运算规则,不再计算后面的答案
(10)设y=10,x=y++,执行该程序后x和y的值:x=10,y=11 y赋值给x后,y做自增运算
任意输入三位数,分离个十百位数
#include<stdio.h> int main() { int num,a,b,c; scanf("%d",&num); a=num/100 b=(num-a*100)/10 c=num%10 printf(“a=%d,b=%d,c=%d\n”,a,b,c); return 0; }
#include<stdio.h> 标准输入输出函数库
(1)输入scanf(); 输出函数:printf() scanf()的使用方法:scanf(“格式控制符”,变量) int a;char b; double c; float d scanf("%d" / “%C” / “%lf" / “%f”,&a,&b,&c,&d) 考试格式 int x,char y,double z; scanf(“%d,%c,%lf",&x,&y,&z); printf“%d,%c,%.2f",x,y,z);(z保留两位小数)
字符输入函数:getchar() ;字符输出函数:putchar() (3)输入函数gets(),输出函数puts() 考试格式 char c1,c2---------------输入:how are you gets(c1); ------------------------ how are you scanf("%s",&c2) -------输出: printf("%s\n",c1) -------------- how are you printf("%s\n",c2)----------------how
gets 遇回车符结束,scanf遇空格,回车符,TAB结束
格式声明%%输出结果为% 例:“printf(“a=%%d,b=%%d\n”,a,b)结果为a=%d,b=%d
#include<stdio.h> int main() { char c1,c2; c1=getchar(); c2=c1-32;大小写相差32 putchar(c2); return 0; }
#include<stdio.h> #include<math.h> int main() { int x,y,z; double s,area; scanf("%d,%d,%d",&x,&y,&z); s=(x+y+z)/2; area=sqrt(s*(s-x)(s-y)(s-z)); printf("%.2f",s); return 0; }
(1)if----else if----else
if后面有多条语句需要用大括号括住
两数交换 #include<stdio.h> int main() { double a,b,c; scanf("%lf,%lf",&a,&b); if(a>b) { c=a; a=b; b=c; } printf(“a=%.2f,b=%.2f\n”,a,b); return 0; } (2()if 嵌套问题 if 和else配对采用就近配对原则 if(x>1) if(x>10) y=x-10; else y=x+10
if(x>1) { if(x>10) y=x-10 } else y=x+10
(1)while while(表达式) { 语句; } 先判断再执行,反复执行,表达式值为假时结束 1-100累加求和
#include<stdio.h> int main() { int x=1,sum=0; while(x<=100) { sum=sum+x; x++; } printf("%d",sum); return 0; } (2)do – while 先执行再判断,的、判断句为假时结束 累加求和 #include<stdio.h> int main() { int x=1,sum=0; do { sum=sum+x; x++; } while(x<=100); printf("%d",sum); return 0; }
== for语句== for(表达式1;表达式2;表达式3) { 循环体语句 } 表达式1 – 表达式2 – 循环体语句–表达式3 – 表达式2(如果不成立循环体结束) 累加求和 #include<stdio.h> int main() { int x=1,sum=0; for(x=1;x<=100;x++) for函数里使用分号分隔 { sum=sum+x; } printf("%d",sum); return 0; } #include<stdio.h> 判断素数 #include<stdio.h> int main() { int x; int i=2; scanf("%d",&x); for(i=2;i<x;i++) { if(x%i==0)break; } if(i<x) printf(“不是素数”); else printf(“是素数”0; return 0; } 输出100-200之间的全部素数
break仅适用于switch和循环语句 #include<stdio.h> int main() { int n; for(n=1;n<=10;n++) { if(n%7==0)break; printf("%d\n",n); } return 0; } 输出结果为1 2 3 4 5 6
#include<stdio.h> int main() { int n; for(n=1;n<=10;n++) { if(n%3==0)continue; printf("%d\n",n); } return 0; } 输出结果为1 2 4 5 7 8 9 10
例题:若有下列语句:int k=10; while(k=0) k=k-1,则:一次循环都不执行 当k=0时,条件为假,初始条件不成立,循环一次都不执行。