初识常量变量

初识数据类型

位,字节和字

  • 最小的储存单位是 位(bit) 0 or 1
  • 常用的储存单位是 字节(byte) 1 byte = 8 bit
  • 设计计算机时给定的自然存储单位 字(word) 个人计算机增长到32位 64位计算机的字长越大,其数据转移的就越快,允许的内存访问就越多

数据类型

char		//字符型
short		//短整型
int		//整型
long		//长整型
long long	//长长整型
float		//单精度浮点型
double		//双精度浮点型
_Bool           //表示布尔值(true or false)
_Complex        //复数
_Imaginary      //虚数
  • 要把一个较小的常量作为long类型的对待,可在值的末尾加一个 L
  • 例如 7 会作为16(int)位储存 7L 会作为32(long)位储存 类似的 7LL 作为long long 类型储存

数据长度

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	
	printf( "char  		= %d \n",sizeof(char) ); 
	printf( "short 		= %d \n",sizeof(short) ); 
	printf( "int 		= %d \n",sizeof(int) ); 	
	printf( "long 		= %d \n",sizeof(long) ); 
	printf( "long long 	= %d \n",sizeof(long long) ); 
	printf( "float	 	= %d \n",sizeof(float) ); 
	printf( "double		= %d \n",sizeof(double) ); 

	return 0;
}
运行结果
char            = 1
short           = 2
int             = 4
long            = 4
long long       = 8
float           = 4
double          = 8

--------------------------------
Process exited after 0.197 seconds with return value 0
请按任意键继续. . .

初识常量变量

类型使用

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	
	char 	a = 'a';
	char*   d = "hello";
	
	int  	b1 = 1000000;
	int     b_list[5] = {1,2,3,4,5};
	
	float	c = 10.001;
	
	int  	e = 9;
	int  	f = 17;
	//short			使用%hd  //printf 会查看16位
	//unsigned int		使用%u
	//long			%ld      //查看32位
	//long long		%lld 
	printf("****************\n");
	printf("a 	= %c \n",a);	//%c 单个字符
	printf("d 	= %s \n",d);	//%s 字符串 
	
	//有符号表示有正负 无符号表示全表示正数 
	printf("****************\n");	
	printf("b1	= %d \n",b1);	//%d 十进制有符号数 			
	printf("b1	= %u \n",b1);	//%u 十进制无符号数  	 
	
	printf("****************\n");
	//float  有效数字6-7位  double 有效数字15-16位 
	printf("c	= %f \n",c);	//%f 浮点数 	%.2f是float后的小数只输出两位。
	printf("c	= %e \n",c);	//%e 浮点输出 科学计数法 
	
	printf("****************\n");
	printf("e 	= %o \n",e); 	//%o 无符号以八进制表示的整数
	printf("f 	= %x \n",f); 	//%X 无符号以十六进制表示的整数
        //  printf("e 	= %#o \n",e); 	//%#o  0???
	//  printf("f 	= %#x \n",f); 	//%#x  0x??

	printf("****************\n");
	printf("%p\n", &b_list); 
	printf("%p\n", &b_list[0]);      	
	printf("%p\n", &b_list[1]);     
	
    printf("****************\n");
    int i=17;
    int *p;
    p=&i;
    printf("i的地址为%p\n",&i);
    printf("i的内容为%p\n",i);
    printf("p的内容为%p\n",p);
    printf("p指向内存的内容为%p\n",*p);
    printf("p的地址为:%p\n",&p);
    printf("这个函数的地址为%p\n",main);

	return 0;
}
运行结果
****************
a       = a
d       = hello
****************
b1      = 1000000
b1      = 1000000
****************
c       = 10.001000
c       = 1.000100e+001
****************
e       = 11
f       = 11
****************
000000000062FDE0
000000000062FDE0
000000000062FDE4
****************
i的地址为000000000062FDDC
i的内容为0000000000000011
p的内容为000000000062FDDC
p指向内存的内容为0000000000000011
p的地址为:000000000062FDD0
这个函数的地址为00000000004014F0

--------------------------------
Process exited after 0.03226 seconds with return value 0
请按任意键继续. . .

局部全局

#include <stdio.h>
#include <stdlib.h>
//全局变量 
int a = 100; 

int main(int argc, char *argv[]) {
	//局部变量 
	//当局部变量和全局变量名字相同,局部优先 
	int a = 10;
	
	printf("%d \n",a); 
	
	return 0;
}

数据表达含义

//有符号的数据表达
//2的32次方0-4294967296			表达的数据-2147483648~2147483647
0~2147483647  				0~2147483647 
2147483648~4294967296			-2147483648~0
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

	int a = 2147483647;
	int b = 2147483648;
	int c = 2147483649;
	int d = 4294967295;
	int e = 4294967296;
	
	
	printf("%d %d %d %d %d\n",a,b,c,d,e); 
	
	return 0;
}
运行结果
2147483647 -2147483648 -2147483647 -1 0

--------------------------------
Process exited after 0.02872 seconds with return value 0
请按任意键继续. . .

初识常量

const int num = 10; //常属性 本质还是个变量 修饰后无法修改

#define MAX 100 //定义的标识符常量

枚举常量

#include <stdio.h>
#include <stdlib.h>

enum sex{
	male,
	female,
	secret 
}; 

typedef enum phone{
	xiaomi = 2,
	apple,
	oppo,
	vivo, 
}Phone; 

int  main(int argc, char *argv[]) {
	enum sex a = male;
	enum sex b = female;
	enum sex c = secret;
	printf("a = %d\n",a);
	printf("b = %d\n",b);
	printf("c = %d\n",c);
	printf("*************\n");
	Phone q,w;
	q = xiaomi;
	w = apple;	
	printf("q = %d\n",q);
	printf("w = %d\n",w);
	
	return 0;
}
运行结果
a = 0
b = 1
c = 2
*************
q = 2
w = 3

--------------------------------
Process exited after 0.03001 seconds with return value 0
请按任意键继续. . .

字符串

  • 字符串以'\0'结尾

转义字符

  • \r \n
posted @ 2022-05-24 11:47  StuTian  阅读(76)  评论(0)    收藏  举报