C语言【数据类型】

 

=========================================================================

    -----类型------       --------存储大小------      ------值的范围--------

1.     char                       1 字节                     -128 到 127 或 0 到 255

2.     int                          2或4 字节                -32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647

3.     short                       2 字节                        -32,768 到 32,767

4.     long                       4  字节                           

======================================================================================================

 

1.怎样打印出数据类型的【存储大小】呢?

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5   int i;
 6     
 7   printf("i的字节大小:%lu\n",sizeof(double));
 8 
 9 
10 }
存储大小

 

2.定义常量

在C语言中有两种方法定义:

#define 叫预处理

 

1.    #define identifier value

     #define Height 20

2.   const type variable = value;

            const int WIDTH = 5;

======================================================================

C语言的存储类型:

 

auto -------------------------   auto 存储类是所有局部变量默认的存储类。

extern --------------------------

register -----------------------  register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。

static  ----------------------  static 存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。

 1 #include <stdio.h>
 2  
 3 /* 函数声明 */
 4 void func1(void);
 5  
 6 static int count=10;        /* 全局变量 - static 是默认的 */
 7  
 8 int main()
 9 {
10   while (count--) {
11       func1();
12   }
13   return 0;
14 }
15  
16 void func1(void)
17 {
18 /* 'thingy' 是 'func1' 的局部变量 - 只初始化一次
19  * 每次调用函数 'func1' 'thingy' 值不会被重置。
20  */                
21   static int thingy=5;
22   thingy++;
23   printf(" thingy 为 %d , count 为 %d\n", thingy, count);
24 }
static用法

extern 存储类

extern 存储类用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。当您使用 'extern' 时,对于无法初始化的变量,会把变量名指向一个之前定义过的存储位置。

当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。

extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候,如下所示:

第一个文件:

 1 #include <stdio.h>
 2  
 3 int count ;
 4 extern void write_extern();
 5  
 6 int main()
 7 {
 8    count = 5;
 9    write_extern();
10 }
main.c(extern用法)

第二个文件:

1 #include <stdio.h>
2  
3 extern int count;
4  
5 void write_extern(void)
6 {
7    printf("count is %d\n", count);
8 }
support.c(extern用法)

在这里,第二个文件中的 extern 关键字用于声明已经在第一个文件 main.c 中定义的 count。现在 ,编译这两个文件,如下所示:

 

posted @ 2018-07-27 16:37  Justice-V  阅读(169)  评论(0)    收藏  举报