C primer plus(第六版)第三章源代码

  • Post author:
  • Post category:其他




第三章

/*  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 * weight * 14.5833;
	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);
	
	return 0;
 }    
/*   3.3 十进制,八进制,十六进制 的转换(不管怎么转,在计算机中都是以二进制存储。)  */ 
#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   */
#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  字符在计算机中以整型存储   */
#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    <inttypes.h>头文件中定义了PRId32字符串宏,代表打印32位有符号整型变量  */
#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  以字节为单位给出指定类型的大小,可用%zd(%u or %lu)  */
#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    */
#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");
	scanf("%f",&salary);
	printf("\n\t$%.2f a month is $%.2f a year.",salary,salary*12.0);
	printf("\rGee!\n");
	
	return 0;
}      



版权声明:本文为weixin_45638291原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。