程式設計基礎C第03講例程
阿新 • • 發佈:2019-10-06
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 用於將英鎊常衡盎司轉換為金衡盎司 # 3.2 print1.c ``` ```*/ 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
# 3.4 print2.c程式
#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 */
r