一、複習一下前面所學的內容 1.寫出下列字元或者數字的類型以及在printf()函數中使用什麼符號轉換 常量類型轉換說明(%轉換符號) 12 int %d 0X3 unsigned int %#x 'C' char(實際上是int) %c 2.34E07 double %e '\040' char( ...
一、複習一下前面所學的內容
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/
- 歡迎關註微信公眾號:傅里葉變換,個人賬號,僅用於技術交流