系统:windows10家庭和学生版 环境:Miccrosoft Visual Studio 2013
#include<stdio.h> #include<string.h> #include<windows.h> int main(){ //按所占空间大小依次为: printf("char: %d 字节\n\n",sizeof(char)); //字符型 //sizeof关键字用来测量变量/常量所占空间大小,单位为字节 printf("short: %d 字节\n", sizeof(short)); //短整型 printf("int: %d 字节\n", sizeof(int)); //整型 printf("long: %d 字节\n", sizeof(long)); //长整型 printf("long long: %d 字节\n\n", sizeof(long long));//双长整型 printf("float: %d 字节\n\n", sizeof(float)); //浮点型 //(表示小数)(小数点后6位) printf("double: %d 字节\n\n", sizeof(double));//双精度浮点型 //指针类型算不算一种数据类型呢? //C语言没有字符串类型,也没有布尔类型 //字符串常常用以下几种方法表示 char str[] = { "This is a C pragma !" };//使用字符数组来表示 printf("%s\n", str); char* ch = "This is a C pragma !"; printf("%s\n\n", ch); //使用一个字符指针指向字符串 /*字符串以‘\0’结束并以双引号(“”)引起来,当读取到‘\0’时程序认为字符串结束, 前面所定义的字符串程序在编译时会自动的在其后面加上‘\0’, 字符串有其长度和所占空间,可以使用sizeof和strlen测出*/ //strlen用来测量字符串长度,包含在<string.h>头文件里 printf("length of str : %d\n", strlen(str)); //字符串的长度 printf("size of str : %d\n\n", sizeof(str)); //字符串所占空间 //需要特别注意的是: char str_1[] = { 'H', 'e', 'l', 'l', 'o' }; //这并不表示字符串 char str_2[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; //必须显式的给出‘\0’ printf("length of str_1: %d\n", strlen(str_1)); //这个值可能是随机的 printf("size of str_1: %d\n\n", sizeof(str_1)); printf("length of str_2 : %d\n", strlen(str_2)); //和str_1相等吗? printf("size of str_2 : %d\n\n", sizeof(str_2)); //和str_1相等吗? printf("\n\nlength of ch : %d\n", strlen(ch)); //可以测指针ch所指向的字符串长度 printf("%d\n\n", sizeof(ch));//这个表示什么?是ch所指向的字符串的空间大小吗? //ch所指向的字符串的空间大小怎样测? system("pause"); //请求系统暂停,在<windows.h>头文件里 return 0; }