当前位置:网站首页>Strlen and sizeof, array length and string length, don't be silly
Strlen and sizeof, array length and string length, don't be silly
2022-06-30 05:55:00 【Ape Bridge】
This article mainly talks about many new acquaintances C Language students will encounter problems , It is also a must for many colleges and universities to take the postgraduate entrance examination .sizeof and strlen How to use it , I briefly summarized their usefulness :
sizeof: Calculate variable size , Array size ,( Include \0) The unit is byte , Is an operator ; strlen: Calculate string length , With \0 For the end sign , It's a library function .
Let's start with a piece of code :
int main()
{
char a[] = "xyz";
char b[] = {
'x','y','z' };
printf("%d\n",sizeof(a)); //4
printf("%d\n",sizeof(b)); //3
printf("%d\n",strlen(a)); //3
printf("%d\n",strlen(b)); //>=3
return 0;
}
By now everyone should be able to understand ,strlen Is the number of number characters , As long as we don't meet \0, Then count back ; and sizeof Is the number size , How big is this array or variable , Unit is byte , It's the number of bytes , Be careful \0 It's also a character .
Let's take a look at a real topic for the postgraduate entrance examination :
This topic , The answer is C, Note that the array length calculation formula is size=sizeof(arr)/sizeof(arr[0])
therefore a The length of is 4,( because a There is..., by default \0)b The length of is 3.
I hope it can help you in your study C Language students .
边栏推荐
- 1380. lucky numbers in matrices
- 从零开发 stylelint规则(插件)
- 拼多多店铺搜索相关问题,为什么新品上架搜索不到
- [GPU] basic operation
- 聲網,站在物聯網的“土壤”裏
- At the beginning of 2022, people who are ready to change jobs should pay attention to
- 【数据库】事务
- hashlips_ art_ Engine-1.0.6 usage
- Use of OpenCL thread algebra library viennacl
- [chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
猜你喜欢

Idea of capturing mobile terminal variant combination

I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?

Configure the user to log in to the device through telnet -- AAA local authentication

Database SQL language 04 subquery and grouping function

OpenCL线程代数库ViennaCL的使用

Gestion des utilisateurs de la base de données MySQL

从零开发 stylelint规则(插件)

抓取手机端变体组合思路设想
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor

C语言基础小操作
随机推荐
[Blue Bridge Road -- bug free code] DS1302 time module code analysis
Promise knowledge points
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
What kind of answer has Inspur given in the big AI model landing test?
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
Finally someone can make the server so straightforward
[OSPF] comparison between rip and OSPF
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
[untitled] user defined function
Database SQL language 03 sorting and paging
MySQL数据库用户管理
[road of system analyst] collection of wrong topics in Project Management Chapter
Video summary of my station B
[exercise] basic practice letter graph of Blue Bridge Cup
After getting these performance test decomposition operations, your test path will be more smooth
inno setup 最简单的自定义界面效果
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
雲服務器部署 Web 項目