79515322

Date: 2025-03-17 17:24:13
Score: 0.5
Natty:
Report link

1. String Literals Include a Null Terminator

When you write "Hello", the compiler automatically appends a null character (\0) at the end to indicate the end of the string.

So, "Hello" in memory is actually:

['H', 'e', 'l', 'l', 'o', '\0']

Thus, sizeof("Hello") counts all 6 bytes, including the \0

2. Difference Between sizeof and strlen

Example:

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

int main() {
    printf("sizeof: %lu\n", sizeof("Hello"));  // Output: 6
    printf("strlen: %lu\n", strlen("Hello"));  // Output: 5
    return 0;
}

How to Avoid This Confusion?

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Alphin Thomas