当前位置:网站首页>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 .
边栏推荐
- OpenCL线程代数库ViennaCL的使用
- 如何制作CSR(Certificate Signing Request)文件?
- 86. separate linked list
- Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
- [untitled] user defined function
- Configure the user to log in to the device through telnet -- AAA local authentication
- Golang之手写web框架
- Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
- Qt之QListView的简单使用(含源码+注释)
- 剑指 Offer 22. 链表中倒数第k个节点
猜你喜欢

Learning about functions QAQ

86. 分隔链表

OpenCL线程代数库ViennaCL的使用

Projet Web de déploiement du serveur Cloud

Dynamic programming -- gliding wing of the strange thief Kidd

Official win 10 image download
![[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point](/img/bb/ea0e78065ba54ff253995faeeb6901.png)
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

MySQL数据库用户管理
随机推荐
[GPU] basic operation
Detailed explanation of issues related to SSL certificate renewal
UE4_ Editor UMG close window cannot destroy UMG immediately
OSPF - authentication and load balancing summary (including configuration commands)
What kind of answer has Inspur given in the big AI model landing test?
MySQL transaction
English grammar_ Adjective / adverb Level 3 - superlative
[road of system analyst] collection of wrong topics in Project Management Chapter
You don't know how to deduce the location where HashSet stores elements?
Learning about functions QAQ
Promise knowledge points
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
Solidity - Security - reentrancy attack
超简单 STM32 RTC闹钟 时钟配置
STM32F103系列控制的OLED IIC 4针
How to automatically renew a token after it expires?
The definition of strain was originally from stretch_ Ratio started
云服务器部署 Web 项目
[OSPF] comparison between rip and OSPF
What are membrane stress and membrane strain