数据类型-类型匹配符
#include<stdio.h>
int main(void)
{
unsigned int un = 3000000000;
short end = 200;
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig= %lld and not %ld\n", verybig, verybig);
return 0;
}
un = 3000000000 and not -1294967296
end = 200 and 200
big = 65537 and not 1
verybig= 12345678908642 and not 1942899938
字符类型
#include<stdio.h>
int main(void)
{
char ch;
printf("please enter a character.\n");
scanf_s("%c", &ch);
printf("the code for %c is %d.\n", ch, ch);
return 0;
}
please enter a character.
C
the code for C is 67.
可移植类型:stdint.h和inttypes.h
/* altnames.c -- 可移植整数类型名*/
#include <stdio.h>
#include <inttypes.h> //支持可移植类型
int main(void)
{
int32_t me32; // me32是一个32位有符号整型变量
me32 = 45933945;
printf("First, assume int32_t is int:");
printf("me32 = %d\n", me32);
printf("Next, let's not make money any assumptions.\n");
printf("Instead, use a \"macro\" form inttypes.h:");
//在inttypes.h头文件中定义了PRId32字符串宏
//代表打印32位有符号值的合适转换声明
printf("me32 = %" PRId32 "\n", me32);
return 0;
}
浮点数float
/*以两种方式显示float类型的值*/
#include<stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
//下列代码要求编译器支持C99或其中相关特性
printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be writeen %Le\n", dip, dip);
return 0;
}
32000.000000 can be written 3.200000e+004
And it's 0x1.f40000p+14 in hexadecimal, powers of 2 notation
2140000000.000000 can be written 2.140000e+009
0.000053 can be writeen 5.320000e-005
数据类型的大小
#include<stdio.h>
int main(void)
{
/*c99为类型大小提供%zd转换说明*/
printf("Type int has a size of %d bytes.\n", sizeof(int));
printf("Type char has a size of %d bytes.\n", sizeof(char));
printf("Type long has a size of %d bytes.\n", sizeof(long));
printf("Type long long has a size of %d bytes.\n", sizeof(long long));
printf("Type double has a size of %d bytes.\n", sizeof(double));
printf("Type long double has a size of %d bytes.\n", sizeof(long double));
return 0;
}
Type int has a size of 4 bytes.
Type char has a size of 1 bytes.
Type long has a size of 4 bytes.
Type long long has a size of 8 bytes.
Type double has a size of 8 bytes.
Type long double has a size of 8 bytes.
转义序列的使用

字符
#include<stdio.h>
int main(void)
{
float salary;
// \a 发出警报 没有换行符,光标停留在:后面
printf("\aEnter your desired monthly salary:");
// 7个退格符\b将光标左移7个位置
printf(" $_______\b\b\b\b\b\b\b");
scanf_s("%f", &salary);
// \n换行使光标移动到下一行起始处; \t 水平制表符使光标移至该行的下一个制表点
printf("\n\t$%.2f a month is $%.2f a year.", salary, salary*12.0);
// \r使光标回到当前行的起始处,然后打印
printf("\rGee!\n");
return 0;
}
Enter your desired monthly salary: $500.2__
Gee! $500.20 a month is $6002.40 a year.
网友评论