第三章
源码
/*3.1*/
#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);
value = 1700.0 * 14.5833*weight;
printf("You 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
#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);//遗漏了2个参数。
return 0;
}
//3.3 %d%o%x分别是十,八,十六进制的显示转换dox前带#显示进制前缀
#include<stdio.h>
#include <stdlib.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
#include<stdio.h>
int main(void)
{
unsigned int un = 3000000000;
short end = 200;
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 字符中不存在字母,ASC‖码转化而来
#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 定义可移植类型变量举例*/
#include<stdio.h>
#include<inttypes.h>
int main(void)
{
int32_t me32;
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 \"marco\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n",me32);
return 0;
}
/* 3.7 */
#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);
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 sizeof计算占用字节大小
#include<stdio.h>
int main(void)
{
printf("Type int has 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.9 参数问题2
#include<stdio.h>
int main(void)
{
int n = 4;
int m =5;
float f = 7.0f;
float g = 8.0f;
printf("%d\n",n,m);
printf("%d %d %d\n",n);
printf("%d %d\n",f,g);
return 0;
}
//3.10转义字符
#include<stdio.h>
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary:");
printf("$_______\b\b\b\b\b\b\b");//7ge
scanf("%f",&salary);
printf("\n\t$%.2f a month is $%.2f a year.",salary,salary*12.0);
printf("\rGee!\n");
return 0;
}
课后题
/*3.1*/
#include<stdio.h>
int main(void)
{
int int_max = 2147483647 ;
printf("%d %d\n",int_max,int_max+1);//上溢
unsigned int p = 0;
printf("%u %u",p,p-1);//下溢
return 0;
}
//3.2
#include<stdio.h>
int main(void)
{
int ASCII;
printf(" Enter a ASCII.\n");
scanf("%d",&ASCII);
printf("%d == %c",ASCII,ASCII);
return 0;
}
//3.3
#include<stdio.h>
int main(void)
{
printf("\aStartled by the sudden sound, Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"");// \".......内容\"
return 0;
}
//3.4
#include<stdio.h>
int main()
{
float x;
printf("Enter a floating-point values: ");
scanf("%f",&x);
printf("fixed-point notation: %f\n",x);
printf("exponential notation: %e\n",x);
printf("p notation: %#a",x);
return 0;
}
/*3.5 */
#include<stdio.h>
int main(void)
{
int age ;
printf("Enter your age.\n");
scanf("%d",&age );
printf("You have lived %f seconds.",age*3.156e7);
return 0;
}
//3.6
#include<stdio.h>
int main(void)
{
double weight;
int number;
printf("Please enter the quart of water.\n");
scanf("%d",&number);
printf("%d quater of water include %f",number,number/3.0e-23);
return 0;
}
//3.7
#include<stdio.h>
int main(void)
{
float height;
printf("Enter your height.\n");
scanf("%f",&height);
printf("Your height in inch is %finch,",height);
printf("Your height in centimeter is %fcm.",height*2.54);
return 0;
}
/* 3.8 */
#include<stdio.h>
int main(void)
{
float pintuo;
printf("输入品脱:\n");
scanf("%f",&pintuo);
printf("In pintuo is %f.\n",pintuo/2.0);
printf("In angsi is %f.\n",pintuo*8.0);
printf("In datangshao is %f.\n",pintuo*8*2);
printf("In chashao is %f.",pintuo*8*2*3);
return 0;
}