数据类型与strlen()函数sizeof()运算符

1.基本数据类型

基本数据类型 字节数
整数 整型 int    *
短整型  short  2
长整型 long   4
浮点型(实数) 单精度 float  4
双精度 double    8
字符 字符 char    1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 2.int与long的使用

  C语言的标准规定long的长度不能低于int,所以一般long型为4字节,而int可能是2个字节,也有可能是4个字节,而为了保证程序的可移植性,当您所使用的变量超过两个字节时,使用long。

 

3.strlen()函数与sizeof()运算符                                                                                                

  C中没有专门的变量类型给字符串,而是把它存储在数组中,每个字符占一个单位,而且在末尾加'\0'。

  strlen()函数以字符为单位给出字符串的长度,给出字符串字符(包括空格和标点)的准确数目,不包括末尾的'\0'。

  sizeof()会把用来标志字符串结束的不可见的空字符也计算在内。

  sizeof()操作符以字节形式给出其操作数的存储大小。

 1 #include <stdio.h>
 2 #include <string.h>     /*提供strlen()函数*/
 3 #define     PRAISE    "What a super marvelous name!"
 4 
 5 int main(void)
 6 {
 7     char name[20];
 8 
 9     printf("Enter your name: ");
10     scanf("%s", name);
11     printf("Hello, %s. %s\n", name, PRAISE);
12     printf("strlen(name) = %d, sizeof(name) = %d\n", 
13         strlen(name), sizeof(name));
14     printf("strlen(PRAISE) = %d, sizeof(PRAISE) = %d\n", 
15         strlen(PRAISE), sizeof(PRAISE));
16 
17     return 0;
18 }

  在Vc++6.0中的输出结果为:   

  对于sizeof运算符,是否使用圆括号取决于是想获取一个类型的大小还是获取某个具体量的大小。圆括号对于类型是必须的,而对于具体量则是可选的。

  例如:应该使用 sizeof (char) 或者 sizeof (float),但是可以使用 sizeof name 或者sizeof 6.28。不过,在所有情况下都使用圆括号会更好。

posted @ 2012-09-25 20:15  wjtang  阅读(448)  评论(0编辑  收藏  举报