C程序设计语言_第2版(高清).pdf
《C程序设计语言_第2版(高清).pdf》由会员分享,可在线阅读,更多相关《C程序设计语言_第2版(高清).pdf(232页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、?1?C?C?C?C?C?1.1.?hello,world?C?“hello,world?#include main()printf(hello,worldn);?UNIX?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?“.c?hello.c?cc hello.c?a.out?a.out?a.out?hello,world?#include main()printf(hello,worldn);?main?main?main?printf?n?C?C?C?Fortran?Pascal?mai
2、n?main?main?main?main?#include?C?7?B?main?()?main?printf(hello,worldn);?hello,worldn?printf?printf?hello,worldn?printf?C?n?n?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?printf?n?n?printf(hello,world);C?printf?#include main()printf(hello,);printf(world);printf(n);?n?n?C
3、?t?b?2.3?1-1?“hello,world?1-2?printf?c?c?1.2.?=(5/9)(?-32)?1-1720-640460158026100371204814060160711808220093220104240115260126280137300148?main?“hello,world?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?#include/*?fahr=0?20?300?*/main()int fahr,celsius;int lower,upper,st
4、ep;lower=0;/*?*/upper=300;/*?*/step=20;/*?*/fahr=lower;while(fahr=upper)celsius=5*(fahr-32)/9;printf(%dt%dn,fahr,celsius);fahr=fahr+step;?/*?fahr=0?20?300?*/?/*?*/?C?int fahr,celsius;int lower,upper,step;?int?float?int?float?int?16?-32768?32767?32?int?float?32?6?10-38?1038?int?float?C?char?short?lon
5、g?double?4?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu- lower=0;upper=300;step=20;fahr=lower;?while?while(fahr=upper).while?(fahrupper)?while?while?while(i j)i=2*i;?while?C?celsius=5*(fahr-32)/9?celsius?5?9?5/9?C?5?9?5/9?0?0?printf?printf?7?%?%d?printf(%dt%dn,fahr,celsi
6、us);?fahr?celsius?t?printf?%?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?printf?C?C?printf?C?ANSI?printf?C?7?7?7.4?scanf?scanf?printf?printf?%d?printf(%3d%6dn,fahr,celsius);?fahr?celsius?fahr?3?celsius?6?0 -17 20 -6 40 4 60 15 80 26 100 37 .?0?-17.8?-17?#include /*prin
7、t Fahrenheit-Celsius table for fahr=0,20,.,300;floating-point version*/main()float fahr,celsius;float lower,upper,step;lower=0;/*lower limit of temperatuire scale*/upper=300;/*upper limit*/step=20;/*step size*/fahr=lower;while(fahr=upper)celsius=(5.0/9.0)*(fahr-32.0);printf(%3.0f%6.1fn,fahr,celsius)
8、;fahr=fahr+step;?fahr?celsius?float?5/9?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?0?5.0/9.0?fahr 32?32?2?fahr=lower;?while(fahr=upper)?int?float?printf?%3.0f?fahr?3?%6.1f?celsius?6?1?0 -17.8 20 -6.7 40 4.4 .?%6f?6?%.2f?%f?%d?%6d?6?%f?%6f?6?%.2f?%6.2f?6?printf?%o?%x?%
9、c?%s?%?%?1-3?1-4?1.3.for?#include Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu- /*?*/main()int fahr;for(fahr=0;fahr=300;fahr=fahr+20)printf(%3d%6.1fn,fahr,(5.0/9.0)*(fahr-32);?int?fahr?for?printf?C?printf?%6.1f?for?while?for?while?for?3?fahr=0?fahr=300?true?printf?fahr=f
10、ahr+20?fahr?faise?while?for?whi1e?for?for?while?1-5?300?0?1.4.?300?20?#define?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-#define?#define?#include#define LOWER 0 /*lower limit of table*/#define UPPER 300 /*upper limit*/#define STEP 20 /*step size*/*print Fahrenheit-Cels
11、ius table*/main()int fahr;for(fahr=LOWER;fahr=UPPER;fahr=fahr+STEP)printf(%3d%6.1fn,fahr,(5.0/9.0)*(fahr-32);?LOWER?UPPER?STEP?#define?1.5.?0?C?getchar?putchar?getchar?c=getchar()?c?7?putchar?putchar()?c?putchar?printf?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-1.5.1.?
12、getchar?putchar?while(?)?C?#include /*copy input to output;1st version */main()int c;c=getchar();while(c!=EOF)putchar(c);c=getchar();?!=?char?int?int?C?getchar?EOF?end of file?c?getchar?c?char?EOF?c?int?EOF?char?C?C?c=getchar()?c?while?#include Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!P
13、DF-XCHANGEwww.docu- /*copy input to output;2nd version */main()int c;while(c=getchar()!=EOF)putchar(c);?while?c?while?while?while?main?getchar?while?!=?=?!=?=?c=getchar()!=EOF?c=(getchar()!=EOF)?c?0?1?getchar?2?1-6?getchar()!=EOF?0?1?1-7?EOF?1.5.2.?#include /*count characters in input;1st version*/m
14、ain()long nc;nc=0;while(getchar()!=EOF)+nc;printf(%ldn,nc);?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-+nc;?+?1?nc=nc+1?+nc?-?+?-?+nc?nc+?2?+nc?nc+?nc?1?long?int?long?32?int?long?int?16?32767?int?%ld?printf?long?double?while?for?#include /*count characters in input;2nd
15、 version*/main()double nc;for(nc=0;gechar()!=EOF;+nc);printf(%.0fn,nc);?float?double?printf?%f?%.0f?0?for?C?for?for?getchar?while?for?0?whi1e?for?0?while?for?1.5.3.?#include Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu- /*count lines in input*/main()int c,nl;nl=0;while(c
16、=getchar()!=EOF)if(c=n)+nl;printf(%dn,nl);?while?if?+nl?if?=?C?Pascal?=?Fortran?.EQ.?C?=?=?=?C?=?2?A?ASCII?65?A?65?A?65?A?n?ASCII?10?n?n?2?1-8?1-9?1-10?t?b?1.5.4.?4?UNIX?wc?#include#define IN 1 /*inside a word*/#define OUT 0 /*outside a word*/Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF
17、-XCHANGEwww.docu- /*count lines,words,and characters in input*/main()int c,nl,nw,nc,state;state=OUT;nl=nw=nc=0;while(c=getchar()!=EOF)+nc;if(c=n)+nl;if(c=|c=n|c=t)state=OUT;else if(state=OUT)state=IN;+nw;printf(%d%d%dn,nl,nw,nc);?state?OUT?IN?OUT?1?0?nl=nw=nc=0;?3?nl?nw?nc?0?n1=(nw=(nc=0);?|?OR?if(c
18、=|c=n|c=t)?c?c?c?t?&?AND?|?&?|?c?else?if?if(?)?1else?2Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?if-else?1?2?else?if?if?1-11?1-12?1.6.?C?12?10?#include /*count digits,white space,others*/main()int c,i,nwhite,nother;int ndigit10;nwhite=nother=0;for(i=0;i=0&c=9)+ndigitc
19、-0;else if(c=|c=n|c=t)+nwhite;else +nother;printf(digits=);for(i=0;i=0&c=0&c=9)+ndigitc-0;else if(c=|c=n|c=t)+nwhite;else +nother;?if(?1)?1else if(?1)?2.else?n?else?else?if?else?0?else if(?)?if?else?3?switch?3.4?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-?switch?1-13?1
20、-14?1.7.?C?Fortran?Pascal?C?printf?getchar?putchar?C?Fortran?*?power(m,n)?power(m,n)?m?n?n?power(2,5)?32?xy?pow(x,y)?power(m,n)?#include int power(int m,int n);/*test power function*/main()int i;for(i=0;i=0*/int power(int base,int n)int i,p;p=1;for(i=1;i=0*/*(old-style version)*/power(base,n)int bas
21、e,n;int i,p;p=1;for(i=1;i=0;version 2*/int power(int base,int n)int p;for(p=1;n 0;-n)p=p*base;return p;?n?for?0?i?power?n?n?5?1.9.?C?while(?)if(?)?getline?getline?0?0?1?copy?main?getline?copy?#include#define MAXLINE 1000 /*maximum input line length*/Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy
22、NOW!PDF-XCHANGEwww.docu- int getline(char line,int maxline);void copy(char to,char from);/*print the longest input line*/main()int len;/*current line length*/int max;/*maximum length seen so far*/char lineMAXLINE;/*current input line*/char longestMAXLINE;/*longest line saved here*/max=0;while(len=ge
23、tline(line,MAXLINE)0)if(len max)max=len;copy(longest,line);if(max 0)/*there was a line*/printf(%s,longest);return 0;/*getline:read a line into s,return length */int getline(char s,int lim)int c,i;for(i=0;i 0)if(len max)max=len;copy();if(max 0)/*there was a line*/printf(%s,longest);return 0;/*getline
24、:specialized version*/int getline(void)int c,i;extern char line;for(i=0;i MAXLINE-1&(c=getchar)!=EOF&c!=n;+i)linei=c;if(c=n)linei=c;+i;Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu- linei=0;return i;/*copy:specialized version*/void copy(void)int i;extern char line,longest
25、;i=0;while(longesti=linei)!=0)+i;?main?getline?copy?extern?extern?extern?extern?main?getline?copy?extern?extern?file1?file2?file3?file2?file3?extern?extern?#include?.h?4?7?B?getline?copy?getline()?copy()?C?ANSI C?C?ANSI C?void?4?define?declaration?2?1?2?Click to buy NOW!PDF-XCHANGEwww.docu-Click to
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计语言
限制150内