第三章 数据和C

目录

3.1 零碎基础知识点

3.2 复习题

3.3 编程题


3.1 零碎基础知识点

示例代码:

/*platinum.c --your weight 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_s("%f", &weight);
	/*假设白金的价格是每盎司$1700*/
	value = 1700.0 * weight * 14.5833;
	printf("Your weight in platinum is worth $%.2f.\n",value);
	printf("You are easily worth that! If platinum pricedrop,\n");
	printf("eat more to maintain yout value.\n");

	return 0;
}

运行结果:

e you worth your weight in platinum?  t' s check it out.  lease enter your weight in pounds: 155  our weight in platinum is worth $3842699. 50.  ou are easily worth that! If platinum prices drop,  eat more to maintain vnut value.

数据类型关键字

Long  uns Igaed  r :oat  double

其中long表示带小数点的数; _Bool类型表示布尔值(turefalse); _complex _Imaginary 分别表示复数和虚数。

按计算机的储存方式可分为两大基本类型:整数类型和浮点数类型。

位(bit)      字节(byte)     字(word)

int dogs, cats = 94; /* 有效,但是这种格式很糟糕 */

/*最好不要把初始化的变量和未初始化的变量放在同一条声明中*/

以十进制显示数字,使用%d;以八进制显示数字,使用%o;

以十六进制显示数字,使用%x。另外,要显示各进制数的前缀0、0x和0X,必须分别使

用%#o、%#x、%#X。

打印unsigned int类型的值,用%u转换说明;打印long类型的值,用%ld转换说明

C语言有多种printf()格式。对于short类型,可以使用h前缀

转义序列

(ANSIC)

3.2 复习题

1.指出下面各种数据使用的合适数据类型(有些可使用多种数据类型):

a.East Simpleton(澳洲的一个小镇)的人口

b.DVD影碟的价格

c.本章出现次数最多的字母

d.本章出现次数最多的字母次数

a int类型,也可以是 short类型 或 unsigned short; 人口数是一个整数。

b float类型, 价格通常并非是一个整数(也可以用 double 但是实际上并不需要那么高的精度)

C char 类型

d int 类型,可以是 unsigned类型

2. 需要 long类型变量代替 int类型变量的原因是什么?

原因一:在系统中要表示的变量超过了int可表示的范围,这时要使用long类型。

原因二:如果要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型。可提高程序的移植性。

3. 获得一个 32位的有符号整数,可以使用哪些可移植数据类型?每种选择的原因是什么?

如果要正好获得32位的整数,可以使用int32_t类型。要获得可储存至少32位整数的最小类型,可以使用int_least32_t类型。如果要为32位整数提供更快的计算速度,可以选择int_fast32_t类型(假设你的系统已定义了上述类型)。

4. 指出下列常量的类型和意义 (如果有的话)

a. '\b'

b. 1066

c. 99.44

d. 0xAA

e. 2.0e30

a.char类型常量(但是储存为int类型)

b.int类型常量

c.double类型常量

d.unsigned int类型常量,十六进制格式

e.double类型常量

5. Dottie Cawm 写的下面这个程序中有很多错误,找出这些错误。

intclude <stdio.h>

main

{

float g;h;

float tax,rate;

g = e21;

tax = rate*g;

}

更正后:

#include <stdio.h>

int main(void)
{
        float g, h;
        float tax, rate;

        rate = 0.08;
        g = 1.0e5;
        tax = rate * g;
        h = g + tax;

        printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, tax, h);

        /*运行结果:你欠$100000.000000加上$8000.000000的税,总共是 $108000.000000。*/

        return 0;
}
 

6. 指出下表中各常量的数据类型(在声明语句中使用的数据类型)及其在 printf()中的格式说明

uns igned int  f Ioat

7. 指出下表中各常量的数据类型(在声明语句中使用的数据类型)及其在 printf()中的格式说明符,假设 int 类型为16位长。

2.9e05L  100000  20.0±  ox44  Int  I orlg  char  Lnt)

8. 假设一个程序开始处有如下声明:

int imate = 2;

long shot = 53456;

char grade = 'A';

flooat log = 2.71828;

在下面 printf ()语句中添上合适的类型说明符

printf (" The odds ageinst the %d were %ld to 1,\n",imate shot);

printf (" A score of %f is not an %c gtade.\n",log,grade);

9. 假设 ch 为 char类型变量。使用转义序列,十进制值,八进制字符常量以及十六进制字符常量等方法将其赋值为回车符 (假设使用 ASCII 编码值)。

ch = '\r';

ch = 13;

ch = '\015'

cd = '\xd'

10.修正下面的程序(在C中,/表示除以)。

void main(int) / this program is perfect /

{

cows, legs integer;

170

printf("How many cow legs did you count?\n);

scanf("%c", legs);

cows = legs / 4;

printf("That implies there are %f cows.\n", cows)

}

更正后:

# include <stdio.h>

int main(void)
{

        unsigned int cows,legs;

        printf("How many cow legs did you count?\n");
        scanf_s("%d", &legs);

        cows = legs / 4;
        printf("That implies there are %d cows.\n", cows);

        return 0;
}

11.指出下列转义序列的含义:

a.\n   换行字符

b.\\   反斜杠字符

c.\"   双引号字符

d.\t   制表字符

3.3 编程练习

1.通过试验的方法(即编写带有此类问题的程序)观察系统如何处理整数上溢,浮点数上溢和浮点为数下溢的情况。

#include <stdio.h>

int main(void)
{
    int i;
    int j = 1;
    float toobig = 1.2345e38;
    float toosmall = 1.234567e-38;

    for(i = 1; i <= sizeof(int) * 8 - 1; i++)
    {
        j *= 2;    
    } 

    /*
    **分别输出整数值的上限、上溢1、上溢2
    */
    printf("整数值的上限: %d.\n", j - 1);         
    printf("整数值上溢1: %d.\n", j);      
    printf("整数值上溢2: %d.\n\n", j + 1);

    /*
    **分别输出整数值的下限、下溢1、下溢2
    */
    printf("整数值的下限: %d.\n", j);             
    printf("整数值下溢1: %d.\n", j - 1);      
    printf("整数值下溢2: %d.\n\n", j - 2);

    /*
    **试验浮点数的上溢和下溢
    */
    printf("浮点数的上溢: %e\n", toobig * 100.0f);
    printf("浮点数的下溢: %e\n", toosmall / 100);

    return 0;
}

运行结果:

整 数 值 的 上 限 :  2147483647 .  格 数 值 上 溢 I :  一 214748364 &  整 数 值 上 溢 2 :  一 2147483647 .  整 数 值 的 下 限 :  一 214748364 &  整 数 值 下 溢 1 : 2147483647 ,  整 数 值 下 溢 2 : 2147483646 、  , 孚 点 数 的 上 溢 ;  inf  浮 占 敖 下 溢  1. 234572e 一 40

浮点数上溢当计算导致数字过大,超过当前类型能表达的范围时,就会发生上溢。C语言规定,在这种情况下会给toobig赋一个表示无穷大的特定值,而且printf()显示该值为inf或infinity(或者具有无穷含义的其他内容)。

浮点数下溢如上程序所示,把一个6位有效数字的toosmall = 1.234567e-38除以100,得到的结果是1.234572e-40虽然得到了结果,但是在计算过程中却损失了原本尾有效位上的数字。这种情况叫做下溢(underflow)。

 

2. 编写一个程序,要求输入一个 ASCII 码值 (如66),然后输出相应的字符

#include <stdio.h>

int main(void)
{
	int a;

	printf("请输入一个ASCII码:");
	scanf_s("%d", &a);
	printf("输入的ASCII码转化成字符为:%c", a);

	return 0;
}

运行结果:

Run-Time Check Failure #2 - Stack around the variable ” was corrupte

程序中,在某个变量附近的内存被破坏了,如果出现此类问题,一般表示我们的程序存在内存越界。

3. 编写一个程序,发出警报声,并打印下列文字:

Startled by the sudden sound,Sally shouted,

"By the Great pumpkin,what was that!"

#include <stdio.h>

int main(void)
{
	printf("\aStartled by the sudden sound, Sally shouted,\n");
	printf("\"By the Great Pumpkin, what was that!\"\n");

	return 0;
}

4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。

按以下格式输出(实际显示的指数位数因系统而异):

Enter a floating-point value: 64.25

fixed-point notation: 64.250000

exponential notation: 6.425000e+01

p notation: 0x1.01p+6

#include <stdio.h>

int main(void)
{
	float floating_point;

	printf("Enter a flaoting_point value: "); 
	scanf_s("%f", &floating_point);
	printf("fixed_point notation: %f\n", floating_point);
	printf("exponential notation: %e\n", floating_point);
	printf("p notation: %a\n", floating_point);

	return 0;
}

运行结果:

5.一年大约有3.156×107秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。

#include <stdio.h>

int main(void)
{
	int age;

	printf("Enter your age: "); 
	scanf_s("%d", &age);
	printf("Seconds for your age: %.3e\n", age*3.156e7);

	return 0;
}

运行结果:

6.1个水分子的质量约为3.0×10−23克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。

 

#include <stdio.h>

int main(void)
{
	int quart;

	printf("Enter quart number: "); 
	scanf_s("%d", &quart);
	printf("%d quart = %e water molecule.\n", quart, quart/3.0e-23);

	return 0;
}

运行结果:

7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。

 

#include <stdio.h>

int main(void)
{
	float inch;

	printf("Enter your height(inches): "); 
	scanf_s("%f", &inch);
	printf("Your height is %f cm.\n", inch*2.54);

	return 0;
}

运行结果:

8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用浮点类型比整数类型更合适?

因为 1杯等于 1/2品脱,使用整型转换无法精确

#include <stdio.h>

int main(void)
{
    float cup;

    printf("Enter the cup number:");
    scanf_s("%f", &cup);

    printf("%.1f杯等于%.1f品脱\n", cup, cup / 2.0);
    printf("%.1f杯等于%.1f盎司\n", cup, cup * 8.0);
    printf("%.1f杯等于%.1f大汤勺\n", cup, cup * 8.0 * 2.0);
    printf("%.1f杯等于%.1f茶勺\n", cup, cup * 8.0 * 2.0 * 3.0);

    return 0;
}

运行结果:

警告 C26451算术溢出: 使用 4 字节值上的运算符 * ,然后将结果转换到 8 字节值。在调用运算符 * 之前将值强制转换为宽类型可避免溢出(io.2)

运算符两边数字改为浮点数即可        

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值