introduce
In C language,sizeof
andstrlen
The main difference lies in their functions and usage scenarios:
-
sizeof:
- It is a keyword (operator) in C language, which can determine the result at compile time.
- Used to calculate the size of space occupied by a type or variable in memory, in bytes.
- For array names,
sizeof
Returns the memory space occupied by the entire array, without relying on the content currently stored in the array. - For pointer variables,
sizeof
The size of space occupied by the pointer itself is usually the length of a machine (for example, it is usually 4 bytes on a 32-bit system, and it is usually 8 bytes on a 64-bit system). - Example:
sizeof(int)
Returns the number of bytes occupied by an integer variable;sizeof(array)
Returns the total number of bytes occupied by the array.
-
strlen:
- is a function provided by the C standard library, defined in
<>
in the header file. - Dynamically calculate the number of valid characters that appear continuously in a non-empty character array (i.e., string) at runtime until the end character is encountered.
\0
Until (null characters). - No ending character is calculated
\0
The length of the , returns only the number of actual printable characters. - Example:
strlen("Hello, World!")
will return 12 because there are 12 valid ASCII characters in the string.
- is a function provided by the C standard library, defined in
The difference between sizeof and strlen in handling strings
sizeof
andstrlen
can be used to process strings, but their functions and usage are different.
-
sizeof
Operator:sizeof
is an operator in C language that obtains the number of bytes occupied by a data type or variable in memory. For arrays,sizeof
Returns the size of the entire array in bytes. For strings,sizeof
Returns the size of the entire string array, including the terminating character'\0'
。
Example of usage:
#include <> int main() { char str[] = "Hello, World!"; printf("Size of string: %zu\n", sizeof(str)); return 0; }
-
strlen
Function:strlen
is a function in the standard C library, located inin the header file. It is used to calculate the length of a string, but not the terminating character.
'\0'
. Therefore, if there is no string'\0'
,strlen
The memory will be read until one'\0
, which may lead to undefined behavior.
Example of usage:
#include <> #include <> int main() { char str[] = "Hello, World!"; printf("Length of string: %zu\n", strlen(str)); return 0; }
Processing character summary
-
sizeof
is an operator that gets the size of a data type or variable in bytes. For string arrays, it returns the size of the entire array, including the terminating character'\0'
。 -
strlen
is a function that calculates the length of a string (excluding the terminating character'\0'
)。 - When processing strings, if you need to get the size of the entire array (including the terminating characters), you can use
sizeof
. If you only need to get the length of the string (excluding the terminating character), you can usestrlen
。
Summarize
sizeof
What is concerned is the size of the data type or the space statically allocated by variables in memory, andstrlen
The focus is on\0
The actual character length of the ending string.
This is the end of this article about the difference between sizeof and strlen in C language. For more related content in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!