這是一個經典的 C 語言入門範例,輸入姓名與體重(磅),利用水的密度(62.4 磅/立方呎)換算體積,並顯示姓名長度與佔用記憶體位元組數。
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define DENSITY 62.4
int main(void) {
float weight, volume;
int size, letters;
char 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);
volume = weight / DENSITY;
printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
printf("Also, your first name has %d letters,\n", letters);
printf("and we have %d bytes to store it in.\n", size);
return 0;
}
sizeof(name) 回傳陣列總位元組數(40),而 strlen(name) 回傳實際字串長度。DENSITY 為 62.4 磅/立方呎,體積 = 體重 / 密度。文中另包含關於 soundback、kickback、talkback 的中文翻譯(回聲、回扣、對講機),以及 Android TalkBack 語音助手的啟用步驟(需安裝訊飛語音庫並設為預設 TTS 引擎)。