sizeof()和strlen()区别

1.sizeof是单目运算符,用来计算数据类型所占空间的大小,单位为字节,返回值与参数的类型有关,在编译时就已经计算好了;strlen函数返回的是在字符串中‘\0’前面出现的字符个数(不包含‘\0’)

        注意:字符串以‘\0’作为结束标志,strlen函数的返回值为size_t,是无符号的。

2.sizeof()

#include<stdio.h>
int main()
{
	const char* s = "hello,world";
	int a = sizeof(s);
	printf("%d\n", a);
	char str[] = "hello,world";
	int b = sizeof(str);
	printf("%d\n", b);//结果为12,字符串以/0结束
	return 0;
}

3.strlen()

#include<iostream>
#include "string.h"
using namespace std;
int main()
{
	const char* src = "hello,world";
	int a = strlen(src);//11
	cout << a << endl;
	const char* str = "hello\0world";
	int b = strlen(str);//5
	cout << b << endl;
	return 0;
}

        注意:strlen函数遇到\0即返回\0之前的字符串长度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值