源碼
//4.1
#include<stdio.h>
#include<string.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.\n",size);
return 0;
}
//4.2
#include<stdio.h>
#define PRAISE "You are an extraordinary being."
int main(void)
{
char name[40];
printf("What's your name?");
scanf("%s",name);
printf("Hello, %s. %s\n",name , PRAISE);
return 0;
}
//4.3
#include<stdio.h>
#include<string.h>
#define PRAISE "You are an extraordinary being."
int main(void)
{
char name[40];
printf("What's your name?");
scanf("%s",name);
printf("Hello, %s. %s\n",name , PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n",strlen(name),sizeof(name));
printf("The phrase of praise has %zd letters",strlen(PRAISE));
printf("and occupies %zd memory cells.\n",sizeof(PRAISE));
return 0;
}
//4.4
#include<stdio.h>
#define PI 3.14159
int main(void)
{
float area, circum, radius;
printf("What is the radius of your pizza?\n");
scanf("%f",&radius);
area = PI * radius * radius;
circum = 2.0 * PI *radius;
printf("Your basic pizza parameters are as follows:\n");
printf("circumference = %1.2f,area = %1.2f\n",circum,area);
return 0;
}
//4.5
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main(void)
{
printf("Some nember limits for this system:\n");
printf("Biggest int: %d\n",INT_MAX);
printf("Smallest long long: %lld\n",LLONG_MIN);
printf("One byte = %d bits on this system.\n",CHAR_BIT);
printf("Largest double: %e\n",DBL_MAX);
printf("Smallest normal float: %e\n",FLT_MIN);
printf("float precision = %d digits\n",FLT_DIG);
printf("float epsilon = %e\n",FLT_EPSILON);
return 0;
}
/* 4.6 */
#include<stdio.h>
#define PI 3.141593
int main(void)
{
int number = 7;
float pies = 12.75;
int cost = 7800;
printf("The %d contestants ate %f berry pies.\n",number,pies);
printf("The value of pi is %f.\n",PI);
printf("Farewell! thou art too dear for my possessing,\n");
printf("%c%d\n",'$',2 * cost);
return 0;
}
//4.7
#include<stdio.h>
#define PAGES 959
int main(void)
{
printf("*%d*\n",PAGES);
printf("*%2d*\n",PAGES);
printf("*%10d*\n",PAGES);
printf("*%-10d*\n",PAGES);
return 0;
}
//4.8
#include<stdio.h>
int main(void)
{
const double RENT = 3852.99;
printf("*%f*\n",RENT);
printf("*%e*\n",RENT);
printf("%4.2f*\n",RENT);
printf("*%3.1f*\n",RENT);
printf("*%10.3f*\n",RENT);
printf("*%10.3e*\n",RENT);
printf("*%+4.2f*\n",RENT);
printf("*%010.2f*\n",RENT);
return 0;
}
//4.9
#include<stdio.h>
int main(void)
{
printf("%x %X %#x\n",31,31,31);
printf("**%d**% d**% d**\n",42,42,-42);
printf("**%5d**%5.3d**%05d**%05.3d**\n",6,6,6,6);
return 0;
}
//4.10
#include<stdio.h>
#define BLURB "Authentic imitation!"
int main(void)
{
printf("[%2s]\n",BLURB);
printf("[%24s]\n",BLURB);
printf("[%24.5s]\n",BLURB); //.5告訴printf()只打印5個字符
printf("[%-24.5s]\n",BLURB);
return 0;
}
//4.11
#include<stdio.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{
short num = PAGES;
short munm = -PAGES;
printf("num as short and unsigned short: %hd %hu\n",num,num);
printf("-num as short and unsigned short: %hd %hu\n",munm,munm);
printf("num as int and char: %d %c\n",num,num);
printf("WORDS as int ,short, and char: %d %hd %c\n",WORDS,WORDS,WORDS);
return 0;
}
/* 4.12 不匹配的浮點數轉換*/
#include<stdio.h>
int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;
printf("%.1e %.1e %.1e %.1e\n",n1,n2,n3,n4);
printf("%ld %ld\n",n3,n4);
printf("%ld %ld %ld %ld",n1,n2,n3,n4);
return 0;
}
/* 4.13 printf的參數傳遞 */
#include<stdio.h>
int main(void)
{
int bph2o = 212;
int rv;
rv = printf("%d F is water's boiling point.\n",bph2o); //此語句將printf()的返回值賦給rv
printf("The printf() function printed %d characters.\n",rv);
return 0;
}
//4.14
#include<stdio.h>
int main(void)
{
printf("Here's one way to print a ");
printf("long string.\n");
printf("Here's another way to print a \
long string.\n");
printf("Here's another way to print a "
"long string.\n");
return 0;
}
//4.15
#include<stdio.h>
int main(void)
{
int age;
float assets;
char pet[30];
printf("Enter your age, assets ,and favourite pet.\n");
scanf("%d %f",&age,&assets);
scanf("%s",pet);
printf("%d $%.2f %s\n",age,assets,pet);
return 0;
}
/* 4.16 */
#include<stdio.h>
int main(void)
{
unsigned width, precision;
int number = 256;
double weight = 242.5;
printf("Enter a field width:\n");
scanf("%d",&width);
printf("The number is :%*d:\n",width,number);
printf("Now enter a width and a precision:\n");
scanf("%d %d",&width,&precision);
printf("Weight = %*.*f\n",width,precision,weight);
printf("Done!\n");
return 0;
}
/* 4.17 */
#include<stdio.h>
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %d",&n);
printf("The last integer was %d\n",n);
return 0;
}