C Primer Plus随手笔记(随意记录,持续更新。)

shijianzhongdeMacBook-Pro:c_study shijianzhong$ gcc print1.c 
print1.c:10:19: warning: more '%' conversions than data arguments [-Wformat]
printf("%d minus %d is %d
",ten);
                 ~^
1 warning generated.
shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 0 is 1582628912
shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 0 is -1165623103
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat print1.c 
#include<stdio.h>
int main(void)
{
int ten=10;
int two=2;

printf("Doing it right: ");
printf("%d minus %d is %d
",ten,2,ten-two);
printf("Doing it wrong: ");
printf("%d minus %d is %d
",ten);

return 0;
}
shijianzhongdeMacBook-Pro:c_study shijianzhong$ 

 上面写了一个printf函数里面的参数不定,如果%d格式化的数量与输入的参数不一样,我编译的时候会报警,但还是能够编译通过,不能改打印出的值是内存中的任意值。

所以使用printf()函数时,要确保转换说明的数量与待打印值的数量相等。

C中的8进制显示为前面带一个0,16进制为0x跟Python一样

如果想格式化输出的时候,通过0x或0的形式显示八进制与16进制,需要格式化的时候已%#o,%#x形式.

shijianzhongdeMacBook-Pro:c_study shijianzhong$ gcc bases.c 
shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
dec=100;octal=144;hex=64
dec=100;octal=0144;hex=0x64
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat bases.c 
/* bases.c--以十进制、八进制、十六进制打印十进制数100 */
# include<stdio.h>
int main(void)
{
int x = 100;

printf("dec=%d;octal=%o;hex=%x
",x,x,x);
printf("dec=%d;octal=%#o;hex=%#x
",x,x,x);

return 0;
}

shijianzhongdeMacBook-Pro:c_study shijianzhong$ 

其他整数类型

C语言提供了三个附属管子见修饰基本整数类型:short、long和unsigned

超过范围的显示

shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
2147483647 -2147483648 -2147483647 
4294967295 0 1 
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat toobig.c 
/* toobig.c-- 超出系统允许的最大int值*/
#include<stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;

printf("%d %d %d 
",i,i+1,i+2);
printf("%u %u %u 
", j, j+1, j+2);

return 0;
}
shijianzhongdeMacBook-Pro:c_stu5ldy shijianzhong$ 

 溢出范围感觉跟循环一样,又重新开始了,跟汽车公里表有点像。

打印short、long、long long 和unsigned类型

另外都是首字母开头格式化,比如%ld  %lld %llu %u等

对于short类型,需要使用h前缀, %hd,十进制显示short类型数字,%ho八进制显示short类型的整数。

shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
un= 3000000000 and not -1294967296
end= 200 and 200
big= 65537 and not 1
verybig= 12345678908642 and not 12345678908642
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat print2.c 
/* print2.c-- 更多printf()的特性*/
#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
", un, un);
printf("end= %hd and %d
", end, end);
printf("big= %ld and not %hd
", big, big);
printf("verybig= %lld and not %ld
", verybig, verybig);

return 0;

}
shijianzhongdeMacBook-Pro:c_study shijianzhong$ 

 不同的格式化输出,针对的位数不同 %hd,针对的是16位,%ld针对的是32位。

编译器在编译的时候,如果型号不对,还是会报提醒的。

char类型。

char用于存储字符,但从技术层面看,char是整数类型。

初始化可以

chat t;

t='a'

t=45

用单引号括起来的单个字符被称为字符常量(chatacter constant)。

char存储在8位的存储单元中。

非打印字符,3种方法表示。

第一种:直接ASCII码赋值

char beep = 7;

 第二种: 使用特殊的符号序列表示一些特殊的字符。也就时用转义字符

char neft = ' ';

其中 xx xhh 第一个后面为8进制,第二个后面为16进制

打印字符

The code for a is 97.
shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
Pleasr enter a character.
b
The code for b is 98.
shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
Pleasr enter a character.
c
The code for c is 99.
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat charcode.c 
/* charcode.c-显示字符的代码编号*/
#include<stdio.h>
int main(void)
{
char ch;

printf("Pleasr enter a character.
");
scanf("%c", &ch);
printf("The code for %c is %d.
",ch,ch);

return 0;

}
shijianzhongdeMacBook-Pro:c_study shijianzhong$ 

有符号还是无符号

C90标准,允许在char前面天健signed或unsigned来标识是否有符号。

_Bool类型

_Bool类型实际上也是一种整数类型,但原则上它仅占用了1位存储空间,因为0与1就够用了。

 可移植类型:stdint.h和inttypes.h

C99新增了两个头文件stdint.h和inttypes.h。

传入了stuint.h int32_t表示32位的有符号整数类型。

最小宽度类型(mininum width type)int_least8_t。会使用系统中的最小整数类型。

最快最小宽度类型(fastst minimum width type)  int_fast8_t

最大有符号整数类型intmax_t,无符号整数类型uintmax_t,这种类型有可能比long long 和unsigned long类型更大

当定义了int32_t需要输出时,C标准针,提供了一些字符串宏 inttypes.h中定了PRId32(注意大小写)字符串宏,代表打印32位有符号值的合适转换说明。

shijianzhongdeMacBook-Pro:c_study shijianzhong$ ./a.out 
First, assume int32_t is int: me32 = 45933945
Next, let't not make any assumptons.
me32 = 45933945
shijianzhongdeMacBook-Pro:c_study shijianzhong$ cat altnames.c 
/* altnames.c -- 可移植整数类型名*/
#include<stdio.h>
#include<inttypes.h>
int main(void)
{
int32_t me32;

me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d
", me32);
printf("Next, let't not make any assumptons.
");
printf("me32 = %" PRId32 "
",me32);  //通过inttypes起始PRId32就相当于"d"

return 0;
}
shijianzhongdeMacBook-Pro:c_study shijianzhong$ 

 在C语言中,可把多个连续的字符串组合成一个字符串。

float double long double

三种表示法 一般计算法, 科学记数法1.0 X 107 指数计数法1.0e7

C标准规定float至少能标识6位有效数组,且取值范围至少是10的正负37次方,是6个数字,不是精确到小数后的6个数字。后一项就是取值范围了。

通常系统存储一个浮点数要占用32位。其中8位用于表示指数的值和符号,剩下24位用于表示非指数部分极其符号。

C语言提供的另一种浮点类型是double,double与float类型的最小取值范围相同。但double占用64位,且至少必须能表示10位有效数字,实际double的值至少有13位有效数字,超过标准的最低位数规定。

long double 以满足double更高的精度要求,C只保证long double类型至少与double类型的精度相同。

原文地址:https://www.cnblogs.com/sidianok/p/12825028.html