Home  >  Article  >  Backend Development  >  What is the difference between sizeof and strlen in C language

What is the difference between sizeof and strlen in C language

王林
王林Original
2020-11-20 10:04:188152browse

The difference between sizeof and strlen in C language is: strlen is a function used to calculate the length of the specified string str, but does not include the end character (i.e. null character); and sizeof is a unary operator , not a function.

What is the difference between sizeof and strlen in C language

Difference analysis:

strlen is a function that is used to calculate the length of the specified string str, but does not include the end character (i.e. null character). The prototype is as shown in the following code:

size_t strlen(char const* str);

Because strlen is a function, a function call is required. The calling example is as shown in the following code:

char sArr[] = "ILOVEC";
/*用strlen()求长度*/
printf("sArr的长度=%d\n", strlen(sArr));

Obviously, the running result of the above example code is 6 (because the ending character null is not included). What needs special attention here is that the function strlen returns a value of type size_t, which may cause the program to cause unexpected results, as shown in the following sample code:

/*判断一*/
if(strlen(x)>= strlen(y))
{
}
/*判断二*/
if(strlen(x)- strlen(y)>= 0)
{
}

From the surface It seems that the two judgment expressions above are completely equal, but this is not the case in reality. Among them, there is nothing wrong with judging expression one, and the program can work exactly as expected; but the result of judging expression two is different. It will always be true. Why is this?

The reason is simple, because the return result of the function strlen is size_t type (that is, unsigned integer type), and the size_t type can never be negative. Therefore, the statement "if(strlen(x)-strlen(y)>=0)" will always be true.

Similarly, even if the expression contains both signed integers and unsigned integers, unexpected results may still occur, as shown in the following code:

/*判断一*/
if(strlen(x)>= 5)
{
}
/*判断二*/
if(strlen(x)- 5>=0)
{
}

Obviously, the judgment The result of expression 2 is still true for the same reason as above.

The keyword sizeof is a unary operator, not a function. Different from the function strlen, its parameters can be arrays, pointers, types, objects, functions, etc., as shown in the following sample code:

char sArr[] = "ILOVEC";
/*用sizeof求长度*/
printf("sArr的长度=%d\n", sizeof(sArr));

Relative to the function strlen, here is the sample code running result is 7 (because it includes the terminating null character). At the same time, for sizeof, because the buffer has been initialized with a known string, its length is fixed, so sizeof calculates the length of the buffer at compile time. It is precisely because it is calculated at compile time, so sizeof cannot be used to return the size of dynamically allocated memory space.

Related recommendations: C#.Net development graphic tutorial

The above is the detailed content of What is the difference between sizeof and strlen in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn