一、復習一下前面所學的內容
1.寫出下列字符或者數字的型別以及在printf()函式中使用什么符號轉換
| 常量 | 型別 | 轉換說明(%轉換符號) |
|---|---|---|
| 12 | int | %d |
| 0X3 | unsigned int | %#x |
| 'C' | char(實際上是int) | %c |
| 2.34E07 | double | %e |
| '\040' | char(實際上int) | %c |
| 7.0 | double | %f |
| 6L | long | %ld |
| 6.0f | float | %f |
| 0x5.b6p12 | float | %a |
| 012 | unsigned int(八進制) | %#o |
| 2.9e05L | float | %Le |
| 's' | char | %c |
| 100000 | long | %ld |
| '\n' | char(實際上是int | %c |
| 20.0f | float | %f |
| 0x44 | unsigned int(十六進制) | %x |
| -40 | int | %d |
2.假設char ch;分別使用轉義序列,十進制,八進制,十六進制來進行賦值\r
char ch = '\r'; char ch = 13; char ch = '\015'; char ch = '\xd';
二、字串和格式化輸入輸出
#include<stdio.h> #include<string.h> //提供strlen()函式的原型 #pragma warning(disable:4996) #define DENSITY 62.4 //人體密度(單位:磅/立方英尺) int D15_talkback() { float weight, volumn; int size, letters; char name[40]; //name是一個可以容納40個字符的陣列 printf("Hi!What's your first name?\n"); scanf("%s", name); printf("%s ,what's your weight in pounds?\n", name); scanf("%f", &weight); size = sizeof name; letters = strlen(name); volumn = weight / DENSITY; printf("Well ,%s ,your volumn is %2.2f cublic feet.\n", name, volumn); printf("Also,yout first name has %d letters,\n", letters); printf("and we have %d bytes to store it.\n", size); return 0; }
顯示結果: 
- 該程式包含以下特性
- (1)用陣列(array)存盤字串(character string),在該程式中,用戶輸入的名存盤到陣列中,該陣列占用記憶體40個連續的位元組,每個位元組存盤一個字符值,
- (2)使用%s轉換說明來處理字串的輸入和輸出,注意:在scanf()中,name沒有&前綴,而weight是有的,
- (3)C前處理器把字符常量DENSITY定義為62.4
- (4)用C函式strlen()獲取字串的長度,
三、原始碼:
- D15_talkback.c
- https://github.com/ruigege66/CPrimerPlus/blob/master/D15_talkback.c
- CSDN:https://blog.csdn.net/weixin_44630050
- 博客園:https://www.cnblogs.com/ruigege0000/
- 歡迎關注微信公眾號:傅里葉變換,個人賬號,僅用于技術交流

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/5007.html
標籤:C
上一篇:C 實戰練習題目76
