-1

I'm using strlen to get the length of a char array, sentence.

When I run the following, the sentence's length is output as 12, despite being only 10 bytes wide:

/* mre.c */
#include <stdio.h>
#include <string.h>


int
main (void)
{
  int length, i, beg;
  char sentence[10] = "this is 10";

  length = strlen (sentence);
  printf ("length: %d\n", length);

  for (i = 0; i < length; i++)
    {
      beg = 0;
    }

  return 0;
}
$ gcc mre.c && ./a.out 
length: 12

When beg = 0; is removed, it returns the expected result:

/* mre.c */
#include <stdio.h>
#include <string.h>


int
main (void)
{
  int length, i, beg;
  char sentence[10] = "this is 10";

  length = strlen (sentence);
  printf ("length: %d\n", length);

  for (i = 0; i < length; i++)
    {
      /* beg = 0; */
    }

  return 0;
}
$ gcc mre.c && ./a.out 
length: 10

I notice that if I print the sentence a char at a time within a shell within Emacs, I see two extra chars:

/* mre.c */
#include <stdio.h>
#include <string.h>


int
main (void)
{
  int length, i, beg;
  char sentence[10] = "this is 10";

  length = strlen (sentence);
  printf ("length: %d\n", length);

  for (i = 0; i < length; i++)
    {
      beg = 0;
      printf ("%c", sentence[i]);
    }

  return 0;
}
$ gcc mre.c && ./a.out
length: 12
this is 10\0\0

I'm at a loss for how to explain this.

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
  • 2
    Is your array supposed to not be big enough? – Davis Herring Jan 10 '23 at 03:56
  • 4
    There is no room for the null-terminating byte in your array so it is omitted during initialization. Using `strlen` on something that is not a *string* invokes [Undefined Behaviour](https://en.cppreference.com/w/c/language/behavior). – Oka Jan 10 '23 at 03:58
  • 2
    Takeaway: **Ask**, don't tell, the compiler the size of arrays. It's better at _getting it right_ than most programmers. (Hint: Learn to use `sizeof`.) – Fe2O3 Jan 10 '23 at 04:07

2 Answers2

3

char sentence[10] = "this is 10"; does not account for the terminating '\0' that is a part of the string literal character array.

char sentence[10 + 1] = "this is 10"; would demonstrate to the readers of your code that you are at least aware of and attempting to account for the '\0' that allows the character array to be used as a C 'string'.

Better yet would be simply: char sentence[] = "this is a sentence";, allowing the compiler to allocate what it knows to be the required space.

Using the last format, you'll likely find that strlen(mystr) is always one less than the value of sizeof mystr.

PS:
re: [10 + 1] - It may not be evident to a beginner. A human reading the code will see this attention to detail and be pleased to see that effort. The compiler will perform the arithmetic on the constants and the code emitted will contain only the value 11, not code to re-calculate the total at runtime.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20
1

Strings in C are character array and are terminated with a \0, which itself is taken as a character. So if you wanna take a sentence that has 10 characters in it, you have to declare the array of size at least 11. So, when you use the strlen function, it counts the characters up until \0 is encountered. If you change your array size to 11, it will print the length as 10 even if you comment out beg = 0