有符号数——原码反码与补码
1、原码
按照2进制数直接写出,注意首位为符号位 1为负,0为正
-1:10000000 00000000 00000000 00000001
2、反码
在原码的基础上,符号位不变,其他位置异号
-1:11111111 11111111 11111111 11111110
3、补码
在反码的基础上,对反码+1
-1:11111111 11111111 11111111 11111111
常见关键字
1、auto
#include<stdio.h>
int main()
{
(auto) int a=10;//局部变量(自动变量--默认省略auto-->意为自动出现,自动消失)
return 0;
}
2、register(寄存器)
#include<stdio.h>
int main()
{
register int a=10;//程序员认为该变量很重要,建议编译器将其置入寄存器中,便于调用
return 0;
}
3、typedef(类型重定义——给原本的关键字起个“小名”)
#include <stdio.h>
int main()
{
typedef unsigned int u_int ;
unsigned int num =30;
u_int num2=30;//等价于上一表达式
return 0;
}
4、static(局部变量生命周期变长)
#include <stdio.h>
void test()
{
int a=1;
a++;
printf("a=%d\n",a);
}
int main()
{
int i=0;
while (i<5)
{
test ();
i++;
}
return 0;
}
输出结果显然是
那么,对这个略加改动的
#include <stdio.h>
void test()
{
static int a = 1;//a是一个静态的局部变量,不再被销毁
a++;
printf("a=%d\n", a);
}
int main()
{
int i = 0;
while (i < 5)
{
test();
i++;
}
return 0;
}
输出结果会有所不同
变成了如下
发现:a不再被销毁了,可以累加了
同时:static也可以修饰全局变量
此时,运用另一个关键词extern——声明外部符号
提前来一步extern对变量进行声明
即,此时可以引用不同源文件中的内容
但是
此时,加入static 后,就将该全局变量限制于单个的源文件内部了
另:类似的,当在另一个源文件中定义函数时,如果不用static进行限定,也可以使用extern进行声明并引用
int add(int ,int)//区别在于此行int 前是否加上static
{
int sum=a+b;
return sum;
}
#include<stdio.h>
extern int add(int ,int );
int main()
{
int a,b;
int sum=add(a,b);
printf("sum=%d\n",sum);
return 0;
}