第1章_程序设计ABC精选.ppt

#include stdio.h main() { int a, b, c; //变量声明整型(integer)variable declaration a = 100; //变量赋值, assignment b = 50; c = a + b; //计算,compute and assign. printf(“\n c=%d”,c); //output “c” to screen } 例2:求两数之和。 例3:求两个数的和(函数) 前面使用了系统提供的函数printf。不用知道这个函数内部是如何运作的,只按照参数形式调用它即可; C程序由函数构成; 我们也可以定义自己的函数; “一个程序应该是轻灵自由的,它的函数就象串在一根线上的珍珠。” (《编程之道》) #include stdio.h /*函数功能:计算两个整数相加之和 入口参数:整型数据a和b 返回值: 整型数a和b之和 */ int Add(int a, int b) { return (a + b); } /*主函数*/ main() { int x, y, sum = 0; printf(Input two integers:); scanf(%d%d, x, y); /*输入两个整型数x和y*/ sum = Add(x, y); /*调用函数Add计算x和y相加之和*/ printf(sum = %d\n, sum); /*输出x和y相加之和*/ } 例3:求两个数的和(函数) (1) 函数由两部分组成: 函数说明部分:函数名、函数类型、形参名、形参类型。 函数体:实现函数的具体操作;由语句构成。 (2) 程序总是从main函数开始执行。 sum.c 例4:打印华氏温度与摄氏温度对照表 计算公式: C=(5/9)(F-32) 打印华氏温度与摄氏温度对照表 #include stdio.h /* 对 fahr = 0, 20, ..., 300 打印华氏温度与摄氏温度对照表 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* 温度表的下限 */ upper = 300; /* 温度表的上限 */ step = 20; /* 步长 */ fahr = lower; while (fahr = upper) { celsius = 5 * (fahr-32) / 9; printf(%d\t%d\n, fahr, celsius); fahr = fahr + step; } } fc1.c While ( ) { } 代码风格 #include stdio.h /* 对 fahr = 0, 20, ..., 300 打印华氏温度与摄氏温度对照表 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* 温度表的下限 */ upper = 300; /* 温度表的上限 */ step = 20; /* 步长 */ fahr = lower; while (fahr = upper) { celsius = 5 * (fahr-32) / 9; printf(%d\t%d\n, fahr, celsius); fahr = fahr + step; } } fc1.c 没有代码风格 #include stdio.h main(){int fahr,celsius; int lower,upper,step; lower=0;upper=300;step=20; fahr=lower; while (fahr=upper){ celsius=5*(fahr-32)/9; printf(%d\t%d\n,fahr,celsius); fahr=fahr+step;}} The International Obfuscated C Code Contest 两个获奖的例子:dance.c、sqrt.c fc2.c 学习了 #include stdio.h main() printf(), scanf() +,=,=, =, ==, != in

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档