3.1 platinum.c
/* platinum.c -- your weight in platinum */
#include <stdio.h>
int main(void)
{
float weight; /* 你的體重 */
float value; /* 相等重量的白金價值 */
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n");
printf("Please enter your weight in pounds: ");
/* 獲取用戶的輸入 */
scanf("%f", &weight);
/* 假設白金的價格是每盎司$1700 */
/* 14.5833 用於將英鎊常衡盎司轉換爲金衡盎司 ```*/
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");
return 0;
}
3.2 print1.c
/* print1.c-演示printf()的一些特性 */
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two );
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten ); // 錯誤,遺漏了兩個參數
return 0;
}
3.3 bases.c
/* bases.c--以十進制、八進制、十六進制打印十進制數100*/
#include <stdio.h>
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);
return 0;
}
3.4 print2.c程序
/* print2.c-更多printf() 特性 */
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* int爲32位的系統 */
short end = 200; /* short爲16位的系統 */
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig= %lld and not %ld\n", verybig, verybig);
return 0;
}
3.5 charcode.c
/* charcode.c-顯示字符的代碼編號 */
#include <stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.\n");
scanf("%c", &ch); /* 用戶輸入字符 */
printf("The code for %c is %d.\n", ch, ch);
return 0;
}
3.6 altnames.c
/* altnames.c -- 可移植整數類型名 */
#include <stdio.h>
#include <inttypes.h> // 支持可移植類型
int main(void)
{
int32_t me32; // me32 是一個32位有符號整型變量
me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n", me32);
return 0;
}
3.7showf_pt.c
/* showf_pt.c -- 以兩種方式顯示float類型的值 */
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
//下一行要求編譯器支持c99或其中的相關特性
printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);
return 0;
}
3.8 typesize.c
//* typesize.c -- 打印類型大小爲類型大小提供%zd的轉換說明 */
#include <stdio.h>
int main(void)
{
/* c99爲類型大小提供%zd的轉換說明 */
printf("Type int has a size of %zd bytes.\n", sizeof(int));
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type long has a size of %zd bytes.\n", sizeof(long));
printf("Type long long has a size of %zd bytes.\n",sizeof(long long));
printf("Type double has a size of %zd bytes.\n",sizeof(double));
printf("Type long double has a size of %zd bytes.\n",sizeof(long double));
return 0;
}
3.10 escape.c
/* escape.c -- 使用轉義序列 */
#include <stdio.h>
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary:");/* 1 */
printf(" $_______\b\b\b\b\b\b\b"); /* 2 */
scanf("%f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary,salary * 12.0); /* 3 */
printf("\rGee!\n"); /* 4 */
return 0;
}