资源描述:
《c程序设计(第三版)潭浩强著课本例题程序源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.1输出一行信息#includevoidmain(){printf("ThisisaCprogram.");}1.2求两数之和#includevoidmain(){inta,b,sum;a=123;b=456;sum=a+b;printf("sumis%d",sum);}1.3求2个数中较大者#includevoidmain(){intmax(intx,inty);inta,b,c;scanf("%d,%d",&a,&b);c=max(a,b);printf("max=%d
2、",c);}intmax(intx,inty){intz;if(x>y)z=x;elsez=y;return(z);}3.1符号常量的使用#definePRICE30#includevoidmain(){intnum,total;num=10;total=num*PRICE;printf("total=%d",total);}3.5转义字符的使用#includevoidmain(){printf("abctderftg");printf("htibbjk");}显示结果:f_-
3、_-_-_gdeh_-_-_-j_k3.7大小写字母的转换#includevoidmain(){charc1,c2;c1='a';c2='b';c1=c1-32;c2=c2-32;printf("%c%c",c1,c2);}4.11#includevoidmain(){charc1,c2;c1=getchar();printf("%c,%d",c1,c1);c2=c1+32;printf("%c,%d",c2,c2);}5.4输入一个字符,判断它是否大写字母,如果是,将它转换成小写字母;如果不
4、是,不转换,然后输出最后得到的字符#includevoidmain(){charch;scanf("%c",&ch);ch=(ch>='A'&&ch<='Z')?(ch+32):ch;printf("%c",ch);}3.8强制类型转换#includevoidmain(){floatx;inti;x=3.6;i=(int)x;printf("x=%f,i=%d",x,i);}运行结果:x=3.600000,i=34.1输出单个字符#includevoidmain(){char
5、a,b,c;a='B';b='O';c='Y';putchar(a);putchar(b);putchar(c);putchar('');}结果:BOY4.3无符号数据的输出#includevoidmain(){unsignedinta=65535;intb=-2;printf("a=%d,%o,%x,%u",a,a,a,a);printf("b=%d,%o,%x,%u",b,b,b,b);}结果:a=-1,177777,ffff,65535b=-2,177776,fffe,655344.5字符串的输出#i
6、ncludevoidmain(){printf("%3s,%7.2s,%.4s,%-5.3s","CHINA","CHINA","CHINA","CHINA");}输出入下:CHINA,_-_-_CH,CHIN,CHI_-4.6输出实数时的有效位数#includevoidmain(){floatx,y;x=111111.111;y=222222.222;printf("%f",x+y);}结果:333333.3281254.7输出双精度数时的有效位数#includevoidmai
7、n(){doublex,y;x=1111111111111.111111111;y=2222222222222.222222222;printf("%f",x+y);}结果:3333333333333.3330004.8输出实数时指定小数位数#includevoidmain(){floatf=123.456;printf("%f%10f%10.2f%.2f%-10.2f",f,f,f,f,f);}结果:123.456001_-123.456001_-_-_-123.46_-123.46_-123.46_-_-5.1
8、由小到大输出输入的两个实数#includevoidmain(){floata,b,t;scanf("%f,%f",&a,&b);if(a>b){t=a;a=b;b=t;}printf("%