实验内容:1.判断当前系统的指定类型的大小是多少
2.掌握部分转义序列的使用
1.判断当前系统的指定类型的大小是多少
实验代码:
# include<stdio.h>
int main()
{
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type short has a size of %zd bytes.\n", sizeof(short));
printf("Type int has a size of %zd bytes.\n", sizeof(int));
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 float has a size of %zd bytes.\n", sizeof(float));
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;
}
运行结果:
Type char has a size of 1 bytes.
Type short has a size of 2 bytes.
Type int has a size of 4 bytes.
Type long has a size of 4 bytes.
Type long long has a size of 8 bytes.
Type float has a size of 4 bytes.
Type double has a size of 8 bytes.
Type long double has a size of 8 bytes.
2.掌握部分转义序列的使用
实验代码:
# include<stdio.h>
int main()
{
float salary;
printf("\aEnter your desired monthly salary:");
printf("$_______\b\b\b\b\b\b\b");
printf(”%d”,scanf("%f", &salary));
printf("\n\t$%.2f a month is $%.2f a year.", salary,salary*12.0);
printf("\rWow!\n");
return 0;
}
运行结果:
Enter your desired monthly salary:$4000.00
Wow! $4000.00 a month is $48000.00 a year.
总结:
1. 程序1列出了八种类型的大小
2. sizeof 是c语言的内置运算符,以自节为单位给出指定类 型的大小
3. sizeof的返回类型是%zd
4. 程序2展示了\a \b \t \n \r这几种转义序列的用法
5. \a的作用为警报;\b的作用为退格;\t为水平制表符; \n的作用为换行;\r的作用为回车
6. 只要scanf()读取一个整数,就会返回1