Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

strlen() vs. sizeof() in C: Understanding Their Differences and Proper Usage

What is strlen()

In C, strlen() returns the number of characters in a null‑terminated string, excluding the terminating '\0'. It counts alphabetic characters, digits, symbols, and spaces.

This article explains the role of strlen(), sizeof(), and why they behave differently.

What is sizeof()

The sizeof operator yields the size, in bytes, of its operand. It is evaluated at compile time for static types and at run time for variable-length arrays. It applies to all C data types, including primitives, pointers, structs, and unions. The result is of type size_t, an unsigned integer type guaranteed to be large enough to hold the size of the largest object.

Because the size of types such as int or double can vary between 32‑bit and 64‑bit systems, sizeof ensures portability.

Key Differences

Syntax of strlen()

size_t length = strlen(my_string);

Here, my_string is a character array or pointer to a null‑terminated string.

Example of strlen()

#include <stdio.h>
#include <string.h>

int main() {
    char strng[] = "January";
    printf("Length of given string is: %zu\n", strlen(strng));
    return 0;
}

Output:

Length of given string is: 7

Syntax of sizeof()

1) sizeof(type)

sizeof(type) yields the size of the specified type.

#include <stdio.h>
#define my_sizeof(type) ((char*)(&type+1) - (char*)&type)

int main() {
    double x;
    printf("%zu", my_sizeof(x));
    return 0;
}

Output:

8

2) sizeof(variable)

Pass a variable name to obtain its memory footprint.

#include <stdio.h>

int main() {
    printf("sizeof(char) = %zu\n", sizeof(char));
    return 0;
}

Output:

sizeof(char) = 1

3) sizeof(expression)

Evaluate an expression’s type size without computing its value.

#include <stdio.h>

int main() {
    char p = 'S';
    double q = 4.65;
    printf("Size of variable p : %zu\n", sizeof(p));
    printf("Size of an expression : %zu\n", sizeof(p+q));
    int a = (int)(p+q);
    printf("Size of explicitly converted expression : %zu\n", sizeof(a));
    return 0;
}

Output:

Size of variable p : 1
Size of an expression : 8
Size of explicitly converted expression : 4

Visual Comparison

strlen() vs. sizeof() in C: Understanding Their Differences and Proper Usage

Below is a side‑by‑side summary of the two constructs.

Feature strlen() sizeof()
Header file string.h No header required (built‑in operator)
Purpose Compute string length (excluding null) Determine memory size of a type or variable
Return type size_t size_t
Evaluation time Runtime (may traverse the string) Compile‑time (except for VLA)
Applicable to Only character arrays or pointers to null‑terminated strings All data types and expressions

When to Use Which

Adhering to these guidelines ensures efficient, portable, and maintainable C code.

C Language

  1. Structures vs. Unions in C: A Practical Guide
  2. C vs. C++: Key Differences & When to Choose Each
  3. C vs Java: A Comprehensive Comparison of Features, History, and Applications
  4. Python vs JavaScript: Key Differences, Features, and When to Choose Each
  5. Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases
  6. Joining vs Welding: Key Differences Explained
  7. Welding vs Brazing: Key Differences Explained for Better Metal Joining
  8. Up vs Down Milling: Key Differences Explained
  9. Soldering vs. Brazing: Understanding the Critical Differences
  10. Key Differences Between Ships and Boats Explained