The difference and use of sizeof and strlen in C language

Foreword: This article is mainly used for personal review, the pursuit of simplicity, thank you for your reference, communication and handling, and may continue to be revised and improved in the future.

Because it is a personal review, there will be some compression and omission.

1. sizeof

        1. sizeof is used to calculate the size of the space occupied by a type or variable, in bytes. Include '\0' when calculating. ('\0' is counted as the end of the string and is not counted when using strlen to calculate the length)

        2. Two special cases: sizeof(array name) and & array name. In these two cases, the array name represents the entire array, and in other cases, the array name represents only the address of the first element of the array.

        3. sizeof is a (unary) operator, not a library function. The result returned by this operator is size_t (unsigned int), which is an unsigned integer.

        4. sizeof(), if a variable is placed in the parentheses, the parentheses can be omitted, if it is a type, it cannot be omitted. (The parentheses after the function cannot be omitted)

        5.C language standard stipulates: sizeof(long) >= sizeof(int)

  

6. The expressions in the sizeof brackets do not participate in the calculation

Because the expressions in sizeof parentheses are not involved in the operation ! Because sizeof is processed during compilation, the source file is preprocessed, compiled, assembled, linked, and judged during compilation when the source file is turned into an executable file. The size of c is given, the value is 4, and the expression does not participate in the operation, and the printf function is executed when it is running, so 4 is directly printed, and the value of the following s variable is 0.

7. When calculating the number of array elements, sizeof trap

When we use arrays and functions, we sometimes need to use the size of the array. At this time, if we use the address of the first element of the array passed in to calculate the size of the entire array in the function, it is easy to make mistakes.

 When using sizeof to find the size of an array, pay attention. When the array is passed as a parameter, the address of the first element of the array is passed. Because it is an address, it is received by a pointer, so the function used in sizeof is all pointers. The size of the array (the value is 4 or 8), not the size of the array, so when calculating the number of array elements, the pointer size will be divided by the pointer size, so the value of the number of array elements calculated in the function is 1

Second, the string manipulation function strlen

        1.strlen calculates the length of the string, excluding '\0'.

       

        2. The size of strlen can be compared with if(strlen("abcde")>strlen("abcdefeo")). But don't judge whether the strlen of the two strings is greater than 0

 The length of arr1 in this code is significantly smaller than arr2, and the result is correct when compared directly.

 However, it is prone to bugs by subtracting two lengths. The reason for bugs is that the return value type of strlen is size_t (unsigned integer type). Even if the subtraction is less than 0, the obtained number is still a positive integer.

 

 If you have to reduce, you can use cast to achieve

3. The problem of using strlen when the array does not end with '\0'

Because the end of strlen is '\0', that is, it ends when '\0' is encountered. Then when an array does not end with '\0', strlen will continue to find

'\0', because it is in unknown data, so the result calculated by strlen will be a random value.

Guess you like

Origin blog.csdn.net/weixin_60320290/article/details/123967998