内存存储

内存存储:

Char 有符号 范围(-128 --- 127)

Unsigned char 无符号 范围 (0 --- 255)

#include <stdio.h>
#include <stdlib.h>
int main()
{
   char a = -1; 
	signed char b = -1; 
	unsigned char c = -1; 
	printf("a=%d,b=%d,c=%d", a, b, c);
	system("pause");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a = 258;
	printf("%d\n", a);
    char b = -128;
	printf("%u\n", b);
	system("pause");
	return 0;
}

Sizeof : 计算占内存空间的长度 (编译期间做,不在运行时做 , 不运行sizeof ()括号中的结果 )

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a = 6;  // sizeof;
	int n = sizeof (a++);
	printf("n = %d,a = %d", n, a);

	system("pause");
	return 0;
}

Strlen :  计算字符串的长度 (\0 不算)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int i;
	char str[1000];
	for (i = 0; i < 1000; i++)
	{
		str[i] = -1 - i;
	}
	printf("%d\n", strlen(str));  //255 '\0' -> 0;

	system("pause");
	return 0;
}

使用函数,交换两个数的内容,需要先找到存储数的地址,在通过地址对数原本的内容进行交换操作,而不是对拷贝的书进行操作。

#include <stdio.h>
#include <stdlib.h>
void swap1(int a, int b)
{
	int t;
	t = a;
	a = b;
	b = t;
}
void swap2(int* x, int* y)
{
	int *t = x;
	x = y;
	y = *t;
}
void swap3(int* x, int* y)
{
	int t;
	t = *x;
	*x = *y;
	*y = t;
}

int main()
{
int a = 5,b = 7;
	swap1(a, b);
	printf("%d %d\n", a, b);
	swap2(&a, &b);
	printf("%d %d\n", a, b);
	swap3(&a, &b);
	printf("%d %d\n", a, b);

	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值