首页 > 编程语言 > 详细

C语言中sizeof、strlen函数的使用

时间:2019-04-12 16:31:25      阅读:120      评论:0      收藏:0      [点我收藏+]

一、测试环境

  Win10 + Visual Studio 2017

二、测试代码

#include "pch.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
    char a[10] = "hello";
    char b[10] = { h,e,l,l,o};
    char c[] = "hello";
    const char *d = "hello";
    cout << "sizeof(a) = " << sizeof a <<  << "strlen(a) = " << strlen(a) <<  \n <<endl;
    cout << "sizeof(b) = " << sizeof b <<   << "strlen(b) = " << strlen(b) << \n << endl;
    cout << "sizeof(c) = " << sizeof c <<   << "strlen(c) = " << strlen(c) << \n << endl;
    cout << "sizeof(d) = " << sizeof d <<   << "strlen(d) = " << strlen(d) << \n << endl;
    return 0;
}

三、测试结果 

  结果1(x86):

sizeof(a) = 10 strlen(a) = 5

sizeof(b) = 10 strlen(b) = 5

sizeof(c) = 6 strlen(c) = 5

sizeof(d) = 4 strlen(d) = 5

  结果2(x64):

sizeof(a) = 10 strlen(a) = 5

sizeof(b) = 10 strlen(b) = 5

sizeof(c) = 6 strlen(c) = 5

sizeof(d) = 8 strlen(d) = 5

四、测试结果分析

  技术分享图片

 图 1 变量a的内容

技术分享图片

 图 2 变量b的内容

技术分享图片

 图 3 变量c的内容

技术分享图片

图4 变量d内容

  1、sizeof函数的使用

  sizeof 运算符可用于获取类、结构、共用体和其他用户自定义数据类型的大小:

  sizeof (data type)

  data type:要计算大小的数据类型,包括类、结构、共用体和其他用户自定义数据类型

  2、strlen函数的使用

 

C语言中sizeof、strlen函数的使用

原文:https://www.cnblogs.com/wyt123/p/10696713.html

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