首页 > 其他 > 详细

sizeof和strlen

时间:2021-05-15 00:07:28      阅读:27      评论:0      收藏:0      [点我收藏+]

C语言(sizeof&&strlen)

sizeof(单目运算符):计算变量或变量类型占的字节数

  • sizeof求数组长度:sizeof(arr)/sizeof(arr[0])
变量类型 字节数
短整型 2
整形 4
长整型 4
单精度浮点型 4
双精度浮点型 8
字符类型 1
示例:
#include<stdio.h>
int main()
{
	printf("%d\n", sizeof(char));
	printf("%d\n", sizeof(short));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));

}

示例结果:1 2 4 4 4 8

strlen(函数):计算指定字符串 str 的长度,但不包括结束字符(即 null 字符(’\0‘))

strlen函数在头文件#include<string.h>中

示例:
#include<stdio.h>
#include<string.h>
int main()
{
	char arr[] = "asfdgng";
	printf("%d\n",strlen(arr));

示例结果:7

sizeof和strlen

原文:https://www.cnblogs.com/jiabaolu/p/14769963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!