字符串和格式化输入/输出

前导程序

程序清单


//talkback.c--一个能为您提供一些信息的对话程序

#include<stdio.h>

#include<string.h> //提供strlen()函数的原型

#define DENSITY 62.4 //人的密度(单位是:英镑/每立方英尺)

int main()

{

float weight, volume;

int size, letters;

char name[40]; //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 cubic feet. \n", name, volume);

printf ("Also, your first name has %d letters, \n", letters);

printf ("and we have %d bytes to store it in, \n", size);

return 0;

}

talkback.c的运行结果如下:

Hi! what's your first name?

Sharla

Sharla,what's your weight in pounds?

139

Well, Sharla, your volume is 2.23 cubic feet.

Also, your first name has 6 letters,

and we have 40 bytes to store it in,

/*程序清单4.2 praise 1.c程序*/

/*praisel.c --使用不同类别的字符串*/

#include<

#define PRAISE "What a super marvelous name"

int main (void)

{

char name[40];

printf("What 's your name?\n");

scanf("%s",name);

printf("Hello. %s. %s\n", name, PRAISE);

return 0;

}

运行结果

What 's your name?

Hilary Bubbles

Hello. Hilary. What a super marvelous name

strlen()函数

程序清单4.3 praise2.c程序

/*praise2.c*/

#include<stdio.h>

#include<string.h>

#define PRAISE "What a super marvelous name! "

int main (void)

{

char name [40];

printf("What's your name?\n");

scanf("%s", name);

printf("Hello, %s. %s\n", name, PRAISE);

printf("Your name of %d letters occupies %d memory cells. \n", strlen(name), sizeof name);

printf("The phrase of praise has %d letters", strlen(PRAISE));

printf("and occupies %d memory cells. \n", sizeof PRAISE);

return 0;

}

运行结果

What's your name?

Morgan Buuttercup

Hello, Morgan. What a super marvelous name!

Your name of 6 letters occupies 40 memory cells.

The phrase of praise has 29 lettersand occupies 30 memory cells.

程序清单4.4 pizza.c程序

/*pizza.c -- 在这个比萨饼子中使用定义常量*/

#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;

}

运行结果:

What is the radius of your pizza?

6.0

Your basic pizza parameters are as follows:

circumference = 37.70, area = 113.10

程序清单4.5 defines.c程序

//defines.c --使用limit.h和float.h中定义常量

#include<stdio.h>

#include //整数限制

#include//浮点数限制

int main(void)

{

printf ("Some number limits for this system: \n");

printf ("Biggest int: %d\n", INT_MAX);

printf ("Smallest unsigned 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;

}

运行结果:

Some number limits for this system:

Biggest int: 2147483647

Smallest unsigned long: -9223372036854775808

One byte = 8 bits on this system.

Largest double: 1.797693e+308

Smallest normal float: 1.175494e-38

float precision = 6 digits

float epsilon = 1.192093e-07

访问 生活导航

在本例中,我们改变超链接的文本和 URL。我们也改变 target 属性。target 属性的默认设置是 "_self",这意味着会在相同的窗口中打开链接。通过把 target 属性设置为 "_blank",链接将在新窗口中打开。