当前位置:网站首页>The difference between sizeof and strlen
The difference between sizeof and strlen
2022-06-25 15:02:00 【52Hertz. six hundred and fifty】
1、 meaning
sizeof It's the operator strlen Is the function sizeof() It works on everything , And the whole size of the calculation strlen() The scope of action is relatively narrow , Only string sizes can be calculated ( barring \0), And the string is \0(NULL) For the end sign , So as long as strlen meet \0 ends
2、sizeof
sizeof The code for
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "hello good\0man";// Written 14 Characters
char b[] = "sad";
int c[5] = {
2,5,4,6,8};
printf(" Array str Its size is %d byte \n",sizeof(str));
printf(" Array a Its size is %d byte \n",sizeof(b));
printf(" Array b Its size is %d byte \n",sizeof(c));
return 0;
}
Running results
Array str Its size is 30 byte
Array a Its size is 4 byte
Array b Its size is 20 byte
why?str[30], When you create an array, you already give str Opens the 30 Byte size space , So the result is 30
Array b Although the stored string is sad, But it's actually d There's another one in the back \0, Practice means a[] In fact, it is equal to asd\0, So there are four bytes , because \0 Is also a byte
Array c Deposited 5 It's an integer , The size of an integer is 4 Bytes , therefore c Size is 20 byte , instead of 5 Bytes , because sizeof Is to measure the whole
3、strlen
strlen() The code for
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "hello good\0man";// Written 14 Characters
char b[] = "sad";
printf(" Array str Its size is %d byte \n",strlen(str));
printf(" Array a Its size is %d byte ",strlen(b));
return 0;
}
Running results
Array str Its size is 10 byte
Array a Its size is 3 byte
why?
Array str There is one in the middle of the string stored in \0, So I met \0, End the measurement , Because the end of the string is \0, So we only measured 10 Characters , Finish ahead of time
Array a There are actually four characters ,asd\0, But we said , String to \0 end , therefore strlen Measured to d Hidden behind \0, It's over
边栏推荐
- [untitled] PTA check password
- How to cut the size of a moving picture? Try this online photo cropping tool
- Review of arrays and pointers triggered by a topic
- 买卖股票的最佳时机
- Sequential programming 1
- Customization and encapsulation of go language zap library logger
- 如何裁剪动图大小?试试这个在线照片裁剪工具
- QT inline dialog
- dev/mapper的解释
- 14 -- validate palindrome string II
猜你喜欢

C language escape character and its meaning

Mining procedure processing

15 -- k points closest to the origin

Learning notes on February 5, 2022 (C language)

使用sphinx根据py源文件自动生成API文档

Learning notes on February 8, 2022 (C language)

Character encoding minutes

Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori

Design and implementation of timer

从408改考自主命题,211贵州大学考研改考
随机推荐
Qt: Pro project file
How to package rpm
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
如何裁剪动图大小?试试这个在线照片裁剪工具
Study notes of cmake
Clipboard tutorial
Arithmetic operations and expressions
Character encoding minutes
Design and implementation of thread pool
What is the difference between escape, encodeuri and encodeuricomponent?
Uniapp icon configuration
2. operator and expression multiple choice questions
Flexible layout (display:flex;) Attribute details
Modal and modeless dialogs for QT
Daily question, Caesar code,
QT pop up open file dialog box QFileDialog
Stack and queue
Gif动画怎么在线制作?快试试这款gif在线制作工具
Golang channel reading data
Judging the number of leap years from 1 to N years