1 前导程序
// talkback.c -- 演示与用户交互
#include <stdio.h>
#include <string.h> // 提供strlen()函数原型
#define DENSITY 62.4 // 人体密度(单位:磅/立方英尺)
int main(void)
{
float weight, volume;
int size, letters;
char name[40]; // name是一个可容纳40个字符的数组
printf("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;
}
程序的运行结果如下
ding@ding-ubuntu:~/primer$ ./a.out
What's your first name?
dingding
dingding, what's your weight in pounds?
125
Well, dingding, your volume is 2.00 cubic feet.
Also, your first name has 8 letters.
and we have 40 bytes to store it.
程序包含的特性
2 字符串简介
1 转义字符
注意,\0 是一个转义字符,ASCII码值为0,意为NULL 空字符,\0 用在字符串结尾处。
2 字符串简介
3 printf
/* praise1.c -- 使用不同类型的字符串 */
#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;
}
程序执行结果:
ding@ding-ubuntu:~/primer$ ./a.out
What's your name? Angela Plains
Hello, Angela. You are an extraordinary being.
可以看到,scanf() 只读取了第一个单词,后面的字符串被忽略。实际上,scanf() 在遇到第一个空格,制表符 \t,或换行符 \n时就不再读取输入。也就是说,scanf() 只会读取第一个单词,而不是一整句。
4 strlen() 函数
sizeof 或 strlen() 函数的返回值为 size_t 类型,使用 %zd 输出。
// size_t类型,一般使用%zd输出。如果不识别,使用%u或%lu
// size_t被定义为unsigned int或unsigned long int
size_t sizeof
size_t strlen (const char *__s)
如下是使用 strlen() 函数和 sizeof 的区别:
#include <stdio.h>
#include <string.h> // 提供strlen()函数原型
#define PRAISE "You are an extraordinory 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;
}
执行结果:
ding@ding-ubuntu:~/primer$ ./a.out
What's your name? ding
Hello, ding. You are an extraordinory being
Your name of 4 letters occupies 40 memory cells.
The phrase of praise has 30 letters and occupies 31 memory cells.
5 预处理 #define
#include <stdio.h>
#define STR_PUT "hello world"
int main(void)
{
printf("%s\n", STR_PUT);
return 0;
}
// 输出结果
ding@ding-ubuntu:~/primer$ ./a.out
hello world
3 printf
1 printf 函数转换说明
2 printf 函数 转换说明修饰符
3 关于 float 类型的参数转换
4 printf 函数中的标记
5 printf 函数返回值
printf 函数的返回值,它返回打印字符的个数;如果有输出错误,他返回一个负数值。用的较少。
#include <stdio.h>
int main(void)
{
int out_cnt;
out_cnt = printf("hello");
printf("out_cnt = %d\n", out_cnt);
out_cnt = printf("hello\n");
printf("out_cnt = %d\n", out_cnt);
return 0;
}
// 运行结果
ding@ding-ubuntu:~/primer$ ./a.out
helloout_cnt = 5
hello
out_cnt = 6
printf 函数的返回值,会将 \r \t \n 这些计算在内。
4 scanf
1 使用 scanf 函数
2 scanf 函数说明
scanf 函数,使用空白(换行符、制表符或空格)把输入分成多个字段。在依次把转换说明和字段匹配时,跳过空白。只要在每个输入间,至少输入一个换行符、制表符或空格。
唯一例外的是,%c转换说明。根据%c,scanf() 会读取每个字符,包括空白。
对于scanf() 函数,float 使用 f ,double 使用 lf,long double 使用 Lf,这点和 printf() 函数不同。
scanf() 函数,从第一个非空白字符开始读取,直到遇到空白字符结束。
3 scanf 函数返回值
scanf() 函数,返回成功读取的项数;如果没有读取任何项,则返回值为0。
读取scanf() 返回值的实例:
#include <stdio.h>
int main(void)
{
int age, count; // 变量
float assets; // 变量
char pets[30]; // 字符数组,用于存储字符串
printf("Enter your age, assets, and favorite pet.\n");
count = scanf("%d%f%s", &age, &assets, pets); // 获取输入返回值
printf("You enter %d items.\n", count);
printf("%d $%f %s\n", age, assets, pets);
return 0;
}
程序执行结果如下
ding@ding-ubuntu:~/primer$ ./a.out
Enter your age, assets, and favorite pet.
18
20.1231
hellokitty
You enter 3 items.
18 $20.123100 hellokitty
可以看到,程序成功的读取到了3次输入。