C

Similar documents
C/C++程序设计 - 字符串与格式化输入/输出

C C

C/C++语言 - C/C++数据

C

C/C++ - 文件IO

C/C++ - 函数

CC213

C/C++ - 字符串与字符串函数

Microsoft Word - CPE考生使用手冊 docx

untitled

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

新版 明解C言語入門編

C/C++ 语言 - 循环

新・解きながら学ぶC言語

新・明解C言語入門編『索引』

C/C++ - 字符输入输出和字符确认

untitled

CC213

nooog

C

C++ 程式設計

C/C++语言 - 运算符、表达式和语句

CC213

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

プログラムの設計と実現II

untitled

untitled

C/C++ - 数组与指针

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

C/C++语言 - 分支结构

chap07.key

C 1

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

C/C++ - 结构体、共用体、枚举体

% % %

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

3.1 num = 3 ch = 'C' 2

c_cpp

untitled

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

C

Ps22Pdf

FY.DOC

untitled

Ps22Pdf

untitled

責任政府與責任社會公共行政改革的方向與重點 學術研討會紀要


Microsoft PowerPoint - 20-string-s.pptx

Microsoft Word - chap13.doc

碩命題橫式

untitled

_汪_文前新ok[3.1].doc

51 C 51 isp 10 C PCB C C C C KEIL

液晶之家论坛-

2015年计算机二级(C语言)模拟试题及答案(四)

Computer Architecture

公務員懲戒法實務及新制

大小通吃-糖尿病


98825 (Project Sunshine) Chi_TC_.indb

(Microsoft Word - outline for Genesis 9\243\2721\243\25529.doc)

穨Shuk-final.PDF

2

招行2002年半年度报告全文.PDF

Microsoft Word _4

郑州大学(下).doc

厨房小知识(六)

广 东 纺 织 职 业 技 术 学 院 发 展 党 员 公 示 制 实 施 办 法 关 于 推 荐 优 秀 团 员 作 为 党 的 发 展 对 象 工 作 的 意 见 后 勤 管 理 工 作 广 东 纺 织 职 业 技 术 学 院 新 引 进 教 职 工 周 转 房 管 理


游戏攻略大全(五十).doc

金融英语证书考试大纲


健康知识(二)

中南财经大学(二).doc

广西大学(一).doc

根据学校教学工作安排,2011年9月19日正式开课,也是我校迁址蓬莱的第一学期开学

山东大学(一).doc

2

主 编 : 杨 林 副 主 编 : 张 新 民 邹 兰 曹 纯 纯 周 秋 婷 李 雅 清 黄 囡 囡 评 审 顾 问 : 杨 林 张 新 民 评 审 : 张 新 民 邹 兰 曹 纯 纯 周 秋 婷 李 雅 清 黄 囡 囡 李 忆 萍 徐 如 雪 文 字 编 辑 : 曹 纯 纯 邹 兰 李 雅 清

最新文物管理执法全书(十四).doc

园林常识(二).doc

前 言 二 一 六 年 四 月 四 日, 兒 童 節, 誕 生 了 一 件 美 事 : 中 國 作 家 曹 文 軒 在 意 大 利 博 洛 尼 亞 國 際 童 書 展 榮 獲 國 際 安 徒 生 文 學 獎, 是 該 獎 創 設 六 十 年 來, 第 一 位 摘 桂 的 中 國 作 家, 意 義 重

湖 南 科 技 大 学

上海外国语大学(二).doc

2009 陳 敦 德

切 实 加 强 职 业 院 校 学 生 实 践 能 力 和 职 业 技 能 的 培 养 周 济 在 职 业 教 育 实 训 基 地 建 设 工 作 会 议 上 的 讲 话 深 化 教 育 教 学 改 革 推 进 体 制 机 制 创 新 全 面 提 高 高 等 职 业 教 育 质 量 在

鸽子(三)

兽药基础知识(四)

园林植物卷(十).doc

园林植物卷(十七).doc

临床手术应用(三)

家装知识(二十)

医疗知识小百科

家庭万事通(一)

家装知识(三)

园林绿化(一)

园林植物卷(十五).doc

Transcription:

C 2017 3 14

1. 2. 3. 4. 2/95 C

1. 3/95 C

I 1 // talkback.c: 2 #include <stdio.h> 3 #include <string.h> 4 #define DENSITY 62.4 5 int main(void) 6 { 7 float weight, volume; 8 int size; 9 unsigned long letters; 10 char name[40]; 11 printf("hi! What s your name?\n"); 4/95 C

II 12 scanf("%s", name); 13 printf("%s, what s your weight in pounds?\n", name); 14 scanf("%f", &weight); 15 size = sizeof name; 16 letters = strlen(name); 17 volume = weight/density; 18 printf("well, %s, your volume is %2.2f cubic feet.\n", name, volume); 19 printf("also, your first name has %lu letters, \n", letters); 5/95 C

III 20 printf("and we have %d bytes to store it in.\n ", size); 21 return 0; 22 } 6/95 C

Hi! What s your name? Xiaoping Xiaoping, what s your weight in pounds? 139 Well, Xiaoping, your volume is 2.23 cubic feet. Also, your first name has 8 letters, and we have 40 bytes to store it in. 7/95 C

character string "Once more you open the door!" 8/95 C

C char 9/95 C

O n c e m o r e y o u o p e n t h e d o o r! \0 1 10/95 C

\0 (null character) C 0 ASCII 0 1 11/95 C

(array) 12/95 C

char name[40]; 40 char 13/95 C

char name[40]; 40 char name 40 char 13/95 C

\0 14/95 C

1 // praise1.c 2 #include <stdio.h> 3 #define PRAISE "What a super marvelous name!" 4 int main(void) 5 { 6 char name[40]; 7 8 printf("what s your name?\n"); 9 scanf("%s", name); 10 printf("hello, %s. %s\n", name, PRAISE); 11 return 0; 12 } 15/95 C

$ gcc praise1.c $./a.out What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 16/95 C

scanf() name scanf name & name %s scanf Xiaoping 17/95 C

"x" x x "x" "x" x \0 18/95 C

strlen sizeof strlen 19/95 C

strlen I 1 // praise2.c: 2 #include <stdio.h> 3 #include <string.h> 4 #define PRAISE "What a super marvelous name!" 5 int main(void) 6 { 7 char name[40]; 8 printf("what s your name?\n"); 9 scanf("%s", name); 10 printf("hello, %s. %s\n", name, PRAISE); 20/95 C

strlen II 11 printf("your name of %lu letters occupied %lu memory cells.\n", strlen(name), sizeof name); 12 printf("the phrase of PRAISE has %lu letters", strlen(praise)); 13 printf(" and occpied %lu memory cells.\n", sizeof PRAISE); 14 return 0; 15 } 21/95 C

strlen $ gcc praise2.c $./a.out What s your name? Xiaoping Hello, Xiaoping. What a super marvelous name! Your name of 8 letters occupied 40 memory cells. The phrase of PRAISE has 28 letters and occpied 29 memory cells. 22/95 C

strlen string.h strlen C (1) printf scanfs stdio.h (2) strlen string.h 23/95 C

printf printf printf 24/95 C

sizeof strlen name = "Morgan" sizeof name : 40 strlen(name): 6 M o r g a n \0 25/95 C

sizeof strlen sizeof PRAISE : 29 strlen(praise): 28 sizeof 26/95 C

sizeof sizeof(float) sizeof(char) sizeof name sizeof 2.15 sizeof(name) sizeof(2.15) 27/95 C

2. 28/95 C

1 // circle1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 float radius, circum, area; 6 radius = 1; 7 area = 3.1415926 * radius * radius; 8 circum = 2 * 3.1415926 * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 29/95 C

radius = 1.000000, circum = 6.283185, area = 3.141593 30/95 C

1 // circle2.c: 2 #include <stdio.h> 3 int main(void) { 4 float radius, circum, area; 5 float pi = 3.1415926; 6 radius = 1; 7 area = pi * radius * radius; 8 circum = 2 * pi * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 31/95 C

1 // circle3.c: 2 #include <stdio.h> 3 #define PI 3.1415926 4 int main(void) { 5 float radius, circum, area; 6 radius = 1; 7 area = PI * radius * radius; 8 circum = 2 * PI * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 32/95 C

1 // circle4.c: 2 #include <stdio.h> 3 int main(void) { 4 float radius, circum, area; 5 const float PI = 3.1415926; 6 radius = 1.; 7 area = PI * radius * radius; 8 circum = 2 * PI * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 33/95 C

#define NAME value C 34/95 C

#define #define BEEP \a #define TEE T #define ESC \033 #define OOPS "Now you have done it!" 35/95 C

#define 36/95 C

#define #define B = 20 B = 20 20 c = a + B; c = a + = 20; 36/95 C

const C90 const const int MONTHS = 12; MONTHS 37/95 C

3. 38/95 C

: %a p- %A P- %c %d 39/95 C

: %e e- %E E- %f %g %f %e %e -4 %G %f %E %E -4 40/95 C

: %i %d %o %p %s 41/95 C

: %x 0-f %X 0-F 42/95 C

printf printf(control-string, item1, item2,...); item1, item2 (Control-string) 43/95 C

printf( "You look great in %s\n", color ); 44/95 C

45/95 C

% %% printf("once more you open the door!\n"); printf("%s%d\n", "area = ", area); printf("%d%% = %f\n", 30, 0.3); 46/95 C

%d %d: %md: m <m >=m %-md: m <m >=m %0md: m <m 0 >=m 47/95 C

%d 1 // width.c: 2 #include <stdio.h> 3 #define N 1000 4 int main(void) 5 { 6 printf("*%d*\n", N); 7 printf("*%2d*\n", N); 8 printf("*%10d*\n", N); 9 printf("*%-10d*\n", N); 10 printf("*%010d*\n", N); 11 return 0; 12 } 48/95 C

%d *1000* *1000* * 1000* *1000 * *0000001000* 49/95 C

%f %e %E I 1 // floats.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const double RENT = 3852.99; 6 printf("*%f*\n", RENT); 7 printf("*%e*\n", RENT); 8 printf("*%4.2f*\n", RENT); 9 printf("*%3.1f*\n", RENT); 10 printf("*%10.3f*\n", RENT); 11 printf("*%10.3e*\n", RENT); 50/95 C

%f %e %E II 12 printf("*%10.3e*\n", RENT); 13 printf("*%+4.2f*\n", RENT); 14 printf("*%-10.2f*\n", RENT); 15 printf("*%010.2f*\n", RENT); 16 return 0; 17 } 51/95 C

%f %e %E *3852.990000* *3.852990e+03* *3852.99* *3853.0* * 3852.990* * 3.853e+03* * 3.853E+03* *+3852.99* *3852.99 * *0003852.99* 52/95 C

%f %e %E %m.nf %m.ne %m.ne m n 53/95 C

%f %e %E %.nf n 54/95 C

%f %e %E %m.f m 55/95 C

I 1 // flags.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 printf("%x %X %#x %#X\n", 31, 31, 31, 31); 6 printf("*%d*\n", 42); 7 printf("*% d*\n", 42); 8 printf("*% d*\n", -42); 9 printf("*%5d*\n", 6); 10 printf("*%5.3d*\n", 6); 11 printf("*%05d*\n", 6); 56/95 C

II 12 printf("*%05.3d*\n", 6); 13 return 0; 14 } 57/95 C

1f 1F 0x1f 0X1F *42* * 42* *-42* * 6* * 006* *00006* * 006* 58/95 C

%x 1f %X 1F %#x 0x1f %#X 0X1F 59/95 C

% d 60/95 C

%5.3d %05d %05.3d 0 0 61/95 C

1 // strings.c: 2 #include <stdio.h> 3 #define WORD "Hello World!" 4 int main(void) 5 { 6 printf("*%2s*\n", WORD); 7 printf("*%15s*\n", WORD); 8 printf("*%15.5s*\n", WORD); 9 printf("*%-15.5s*\n", WORD); 10 return 0; 11 } 62/95 C

*Hello World!* * Hello World!* * Hello* *Hello * 63/95 C

%15.5s printf 5-64/95 C

printf() 1 #include <stdio.h> 2 int main(void) 3 { 4 int bph2o = 100; 5 int rv; 6 rv = printf("%d C is water s boiling point.\n", bph2o); 7 printf("the printf function printed %d character.\n", rv); 8 return 0; 9 } 65/95 C

printf() 100 C is water s boiling point. the printf function printed 32 character. 66/95 C

printf() printf() 67/95 C

printf() %n printf() %n %n 68/95 C

printf() %n 1 // printf_n.c: 2 #include<stdio.h> 3 int main(void) 4 { 5 int c1, c2; 6 printf("hello Wuhan %nuniversity!%n\n", &c1, & c2); 7 printf("c1 = %d, c2 = %d\n", c1, c2); 8 return 0; 9 } 69/95 C

printf() %n $ gcc printf_n.c $./a.out Hello Wuhan University! c1 = 12, c2 = 23 70/95 C

4. 71/95 C

printf() scanf() printf() scanf() 72/95 C

scanf() scanf() & scanf() & 73/95 C

scanf() 1 // input.c: 2 #include <stdio.h> 3 int main(void) { 4 int age; 5 double weight; 6 char name[20]; 7 printf("enter your name, age and weight:\n"); 8 scanf("%s", name); 9 scanf("%d and %lf", &age, &weight); 10 printf("%s: %d %f\n", name, age, weight); 11 return 0; 12 } 74/95 C

scanf() $ gcc input.c $./a.out Enter your name, age and weight: Xiaoming 23 100 Xiaoming: 23 100.000000 75/95 C

scanf() scanf() %c 76/95 C

printf scanf printf() %f %e %E %g %G float double scanf() float double l 77/95 C

scanf %c %d %e,%f,%g,%a %E,%F,%G,%A %i %o %p 78/95 C

scanf %s %u %x,%x 79/95 C

scanf % 80/95 C

scanf * "%*d" digit "%10s" hh signed char unsigned char "%hhd" "%hhu" ll long long unsigned long long "%lld" "%llu" 81/95 C

scanf "%hd", "%hi" "%ho", "%hx", "%hu" "%ld", "%li" short unsigned short long "%lo", "%lx", unsigned long "%lu" "%le", "%lf", double "%lg" "%Le", "%Lf", long double "%Lg" 82/95 C

scanf %d, %i, %o %x int %e, %f %g float 83/95 C

scanf() 84/95 C

scanf("%d, %d", &n, &m); 12, 23 12, 23 12, 23 12, 23 85/95 C

scanf("%d and %d", &n, &m); 12 and 23 12 and 23 12and23 86/95 C

%c scanf("%d%d",&n,&m); scanf("%d %d",&n,&m); 87/95 C

%c scanf("%c",&ch) scanf(" %c",&ch) 88/95 C

scanf scanf() 0 (end of file) EOF EOF stdio.h -1 89/95 C

printf() * I 1 // varwidth.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 unsigned width, precision; 6 int number = 256; 7 double weight = 123.5; 8 printf("what field width?\n"); 9 scanf("%d", &width); 10 printf("the number is:%*d\n", width, number); 90/95 C

printf() * II 11 printf("now enter a width and a precision:\n") ; 12 scanf("%d %d", &width, &precision); 13 printf("weight=%*.*f\n", width, precision, weight); 14 return 0; 15 } 91/95 C

printf * $ gcc varwidth.c $./a.out What field width? 6 The number is: 256 Now enter a width and a precision: 8 3 Weight= 123.500 92/95 C

scanf() * scanf() * % 93/95 C

scanf() * 1 // skip2.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n; 6 printf("please enter three integers:\n"); 7 scanf("%*d %*d %d", &n); 8 printf("the last integer was %d\n", n); 9 return 0; 10 } 94/95 C

scanf() * $ gcc skip2.c $./a.out Please enter three integers: 10 20 30 The last integer was 30 95/95 C