SoFunction
Updated on 2025-03-04

Summary of the difference between sizeof and strlen in C language

introduce

In C language,sizeofandstrlenThe 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,sizeofReturns the memory space occupied by the entire array, without relying on the content currently stored in the array.
    • For pointer variables,sizeofThe 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.\0Until (null characters).
    • No ending character is calculated\0The 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.

The difference between sizeof and strlen in handling strings

sizeofandstrlencan be used to process strings, but their functions and usage are different.

  • sizeofOperator:
    sizeofis an operator in C language that obtains the number of bytes occupied by a data type or variable in memory. For arrays,sizeofReturns the size of the entire array in bytes. For strings,sizeofReturns 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;
}
  • strlenFunction:
    strlenis 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'strlenThe 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

  • sizeofis 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'
  • strlenis 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 usesizeof. If you only need to get the length of the string (excluding the terminating character), you can usestrlen

Summarize

sizeofWhat is concerned is the size of the data type or the space statically allocated by variables in memory, andstrlenThe focus is on\0The 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!