1、
%d-处理整型值(十进制)
%o-八进制
%x-十六进制
若要显示各进制数的前缀0、0x、0X,
必须分别使用%#o、%#x、%#X。
%f-处理浮点值
%.2f-表示显示小数点后两位
%e-指数记数法的浮点数
%s-处理字符
不提供值时,会打印输出内存中的任意值。
2、int
int-整数-%d
short int-%hd/%ho
long int-%ld/%lo/%lx
long long int-%lld
unsigned int-无符号整型(非负值)-%u
3、char
char something = ‘A’
char something = A //error 此时A为一个变量名
char something = “A” //error 此时A为一个字符串
注意:C语言没有字符型类型
4、转义序列
\n-换行
\b-退格
\f-换页
\a-警报
\r-回车
\t-水平制表符
\v-垂直制表符
\-反斜杠
'-单引号
"-双引号
?-问号
\0oo-八进制值
\xhh-十六进制值
5、
C99 新增了两个头文件stdint.h和inttypes.h,以确保C语言的类型在各系统中的功能相同。
C语言为现有类型创建了更多类型名。
这些新的类型名定义在stdint.h头文件中。
例如,int32_t表示32位的有符号整数类型。
在使用32位int的系统中,头文件会把int32_t作为int的别名。
不同的系统也可以定义相同的类型名。
例如,int为16位、long为32位的系统会把int32_t作为long的别名。
然后,使用int32_t类型编写程序,并包含stdint.h头文件时,编译器会把int或 long替换成与当前系统匹配的类型。
6、类型大小
sizeof(int/long/char)-用%zd表示
编程练习
waiting-
1、
通过试验(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。
2、
编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入的字符。
#include <stdio.h>
int main(void)
{
int ASCII;
printf("Please enter an ASCII code:");
scanf("%d", &ASCII);
printf("%d is the ASCII code for %c.\n", ASCII, ASCII);
return 0;
}
3、
编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
“By the Great Pumpkin, what was that!”
#include <stdio.h>
int main(void)
{
printf("\a");
printf("Startled by the sudden sound,sally shouted,\n");
printf("\"By the great pumkin,wahat was that!\"");
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 a;
printf("Enter a floating-point value: ");
scanf("%f", &a);
printf("fixed-point notation: %f\n", a);
printf("exponential notation: %e\n", a);
printf("p notation: %a\n", a);
return 0;
}
5、
一年大约有3.156×10^7秒。
编写一个程序,提示用户输入年龄,
然后显示该年龄对应的秒数。
#include <stdio.h>
#include<math.h>
int main(void)
{
int age;
printf("Please enter your age: ");
scanf("%d", &age);
float sec;
sec = age * 3.156 * pow(10, 7);
printf("The second is: %f\n", sec); //age不大时才可用%f
printf("The second is: %e\n", sec);
return 0;
}
6、
1个水分子的质量约为3.0×10−23克。
1夸脱水大约是950克。
编写一个程序,提示用户输入水的夸脱数,
并显示水分子的数量。
#include <stdio.h>
#include<math.h>
int main(void)
{
float quarts;
float molecules;
printf("Please enter the number of quarts of water: ");
scanf("%f", &quarts);
float mass_q,mass_m;
mass_m = 3.0e-23;
mass_q = 950;
molecules = quarts * mass_q / mass_m;
printf("%f quarts of water contain %e molecules.\n", quarts, molecules);
return 0;
}
//10^9
//pow(10,9)
//10e9
7、
1英寸相当于2.54厘米。
编写一个程序,提示用户输入身高(/英寸),
然后以厘米为单位显示身高。
#include <stdio.h>
#include<math.h>
int main(void)
{
float height_inches;
float height_cm;
printf("Please enter your height (in inches):");
scanf("%f", &height_inches);
height_cm = height_inches * 2.54;
printf("Your height is (in cm):%.2f",height_cm);
return 0;
}
8、
在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。
编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。
思考对于该程序,为何使用浮点类型比整数类型更合适?
#include <stdio.h>
int main(void)
{
float pints, cups, ounces, spoon, teaspoon;
//cups为单数时pints带有小数
printf("Please enter the number of cups:");
scanf("%f", &cups);
pints = cups / 2;
ounces = 8 * cups;
spoon = 2 * ounces;
teaspoon = 3 * spoon;
printf("%f pints = %f cups = %f ounces = %f spoon = %f teaspoon\n", pints, cups, ounces, spoon, teaspoon);
return 0;
}