文档详情

C语言程序设计3第一章C语言概述.ppt

发布:2017-05-03约3.23千字共34页下载文档
文本预览下载声明
;第1章 C语言概述;1.1 程序设计及编程语言的“代” 1.2 C语言的发展历史和特点 1.3 C程序示例 1.4 C程序的编辑、编译和运行;1.1 程序设计及编程语言的“代”;1.1.2 编程语言的“代” 多数专家认为,计算机语言大致可以分为以下五代。 1.第一代语言——机器语言 2.第二代语言——汇编语言 3.第三代语言——高级语言 4.第四代语言 5.第五代语言 ;1.2 C语言的发展历史和特点;经典C ANSI C 标准C;1.2.2 C语言的特性 1.语言表达能力强 2.语言简洁,使用方便、灵活 3.运算符丰富 4.生成的代码质量高 5.具有良好的可移植性 6.具有结构化语言特征 ;1.3 C程序示例; 第1行——#include stdio.h,是文件包含行。 第2行——int main(),是函数首部,它告诉系统这个函数的名称是main。 第3行只有一个开花括号“{”,它等同于Pascal语言中的BEGIN,而第10行的闭花括号“}”等同于END。这一对花括号往往被称为语句括号。 第4行至第9行构成这个函数的函数体。 第5行至第7行是三个赋值语句。 注意:在每一个语句的后面都带一个分号(;)。 第8行printf(…); 是一个函数调用语句。 第9行是return语句,它将其后的值返回。;例1-2:计算半径为r的圆的面积。 1 /* Calculating the area of a circle. */ 2 #include stdio.h 3 #define PI 3.14 4 ;5 int main() 6 { 7 float r; /* radius of a circle */ 8 float area;/* area of the circle */ 9 printf(Input:r=?\n); 10 scanf(%f,r); 11 area=PI*r*r; 12 printf(The area is %f\n,area); 13 return (0); 14 }; 在C语言中,注释行是以“/*”开头、以“*/”结尾的任意字符串。 注释的目的是为了增加程序的可读性。 在使用注释时,要注意以下几点: ① /*和*/要成对出现,并且在字符“/”和字符“*”之间不能插入空格; ② 注释不能嵌套,就是说,不能在注释中间又有注释; ③ 注释不要插到一个字符常量(如A、\n)或一个字符串常量(如abc、Hello! )的中间。; 例1-3:给定等差级数的首项、公差和项数,计算该级数的第n项值以及前n项和。 设:首项为a1,公差为d,项数为n,则第n项an:an=a1+(n-1)d 前n项和sn: ;程序如下: 1 /*Calculating nth item of the arithmetical series and the sum of its n items*/ 2 #include stdio.h 3 int n_item(int,int,int);/*Function prototype*/ 4 int sum(int,int,int);/*Function prototype*/ 5 ;6 int main() 7 { 8 int a1,n,d,an,sn;  9 printf(Input data:a1,n,d\n); 10 scanf(%d%d%d,a1,n,d); 11 an=n_item(a1,n,d); 12 sn=sum(a1,n,d); 13 printf(an=%d\n,an); 14 printf(sn=%d\n,sn); 15 return (0); 16 } ;17 18 /* Calculating n_th item of the arithmetical series */ 19 int n_item(int a,int x,int k) 20 { 21 int b; 22 b=a+(x-1)*k; 23 return(b); 24 } 25 ;26 /*Calculating the sum of its n items */ 27 int sum(int a,int x,int k) 28 { 29 int sum_n; 30 sum_n=x*a+x*(x-1)
显示全部
相似文档