c 语言学习第二天

261 阅读4分钟

常量

字符串常量

  • 字符

例如:'f','i','z','a'编译器为每个字符分配空间。

'f''i''z''a'
  • 字符串 例如:"hello"编译器为字符串里的每个字符分配空间以'\0'结束。
'h''e''l''l''o''\0'

基本类型

  • 整数型: short int,int,long int,long long int(c99 新增).占字节大小按照从小到大书写。
  • 浮点数类型:float,double,long double
  • 字符类型: char
  • 布尔类型:_Bool
  • 枚举类型:enum

sizeof 运算符

用户获取数据类型或表达式的长度。

  • sizeof(object); // sizeof(对象);
  • sizeof(type_name); // sizeof(类型);
  • sizeof object; // sizeof 对象;

因为根据编译器不同,以下的数据类型占字节大小仅供参考

#include <stdio.h>

int main(){
  int i;
  char j;
  float k;

  i=123;
  j='c';
  k=3.123;

  printf("size of int is %d \n",sizeof(int));
  printf("size of i is %d \n",sizeof i );
  printf("size of char is %d \n",sizeof(char));
  printf("size of j is %d \n",sizeof j );
  printf("size of flaot is %d \n",sizeof(float));
  printf("size of k is %d \n",sizeof k );

  return 0;
}
size of int is 4
size of i is 4
size of char is 1
size of j is 1
size of flaot is 4
size of k is 4
#include<stdio.h>

int main(){

printf("int sizeof is %d\n",sizeof(int));
printf("short int size is %d\n",sizeof(short));
printf("long int size is %d\n",sizeof(long));
printf("long long int size is %d\n",sizeof(long long));
printf("char size is %d\n",sizeof(char));
printf("_Bool size is %d\n",sizeof(_Bool));
printf("float size is %d\n",sizeof(float));
printf("double size is %d\n",sizeof(double));
printf("long double size is %d\n",sizeof(long double));

return 0;
}

int sizeof is 4
short int size is 2
long int size is 8
long long int size is 8
char size is 1
_Bool size is 1
float size is 4
double size is 8
long double size is 16

signed 和 unsigned

signed:代表带符号位

unsigned:代表不带符号位 ≥0

#include<stdio.h>
int main(){
short i;
unsigned short j;
i = -1;
j = -1;
printf("i is %d\n",i);
printf("j is %u\n",j);
return 0;
}
i is -1
j is 65535

计算机数据单位

cup 读懂的最小单位:bit(比特位).

bit(比特位) => b

1 / 0

Byte(字节) => B

11111111

关系:1B = 8b

1 字节可以表示多大的数? 十进制:255,十六进制:FF

最大值计算方式:2的n次方-1

二进制十进制十六进制
000
111
1022
1133
10044
10155
11066
11177
100088
100199
101010A
101111B
110012C
110113D
111014E
111115F
100001610
100011711
.........
11111111255FF
  • 计算

已知int类型为 4 个字节。

#include<stdio.h>
#include<math.h>
int main(){
  int result = pow(2,32)-1;
  printf("result is %d\n",result);
  return 0;
}
test6.c: In function ‘main’:
test6.c:4:2: warning: overflow in implicit constant conversion [-Woverflow]
  int result = pow(2,32)-1;
  ^
result is 2147483647

运行报出警告,超出定义范围。为什么会这样?

因为在定义的int类型的时候默认会在前面加上signed类型,所以左边的第一位用来表示符号位。如果为 0 表示正数,如果为 1 表示负数。所以int result其实只有 7 位用来表示数值,其最大值为2^(4*8-1) -1:2,147,483,647.

修改如下:

#include<stdio.h>
#include<math.h>
int main(){
  unsigned int result = pow(2,32) - 1; // 在int前加上unsigned
  printf("result is %u\n",result); // 这里的%d 需要改成%u
  return 0;
}
[root@localhost day1]$ gcc  test6.c  && ./a.out
result is 4294967295

result 正常显示为 2^8-1 : 4294967295.

计算机如何存储数值?

采用补码的形式存储。

  • 正数的补码:该数的二进制形式。
  • 负数的补码:
  1. 先取得该数的绝对值的二进制,也就是正数的二进制。
  2. 将第一步的值按位取反。
  3. 将第二步的值 +1.

例如:

7:

00000111

-7:

  1. 获得 7 的二进制
00000111
  1. 按位取反
11111000
  1. +1
11111001

总结

当左边第一位为0的时候,后面的1越多,值越大。

当左边第一位为1的时候,后面的0越多,值越大。

  • 基本数据类型取值范围

有符号 1 字节 => -2^(1x8-1) ~ 2^(1x8-1)-1

无符号 1 字节 => 0 ~ 2^(1x8)-1

有符号 4 字节 => -2^(4x8-1) ~ 2^(4x8-1)-1

无符号 4 字节 => 0 ~ 2^(4x8)-1

以此类推

为什么采用补码?

参考一下链接:

百度百科:补码

关于2的补码