3

This code returns n=11, with 10th and 11th character as ' ' and '@' How does this work? How does strlen function take it as 11 characters? It seems like it takes the string length as 12 characters in some compilers.

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

void fun(char *arr)
{
   int i;
   unsigned int n = strlen(arr);
   printf("n = %d\n", n);
   for (i=0; i<n; i++)
     printf("%c  ", arr[i]);
}

int main()
{
   char arr[] = {'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z'};
   fun(arr);
   return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
PyRookie
  • 75
  • 2
  • 4
  • 9
    Undefined behaviour - anything could happen here, including seg fault or non termination. – Oliver Charlesworth Jul 21 '16 at 08:46
  • You happen to have a nul character in memory somewhere after the end of the "geeksquiz" constant. There may be some logic to it given other values you're going to have in a read-only data segment (nothing springs to mind) but it's definitely not something you should rely on. – Rup Jul 21 '16 at 08:49
  • 1
    "a string without null character" **is not a string** – pmg Jul 21 '16 at 08:57

3 Answers3

10

According to standard, since your "string" does not have null terminator, this is not a string. Calling strlen with anything but string is undefined behavior, so anything can happen including the case you're observing.

If you wonder, how exactly this is happening, this is likely because of strlen keeps trying to find null terminator, and occasionally finds it in memory somewhere after the arr.

Note that this code can even segfault, if null terminator will not be found "fast enough".

You can use tools like valgrind to detect such memory access violations.

Anton K
  • 670
  • 6
  • 19
3

In C, a "string" has a NUL character at the end. A char[] without NUL is just a char[] not a "string".

Arrays in C don't have their length stored anywhere, so strlen() goes as long as NUL hasn't been found. You were probably lucky to have NUL right few memory cells after array ended.

alk
  • 69,737
  • 10
  • 105
  • 255
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 1
    `NUL` and `NULL` are not the same. `NUL`, `'\0'`, `0` and "null character" are the same. – alk Jul 21 '16 at 08:58
0

Whenever you initialize string, the compiler adds nul operator \0 automatically for you. For example,like what you are trying to do in your code is just making the char array, instead of doing this you can use, " " and compiler will automatically add the \0 for you same case when you ask input for string from users. But certainly there are cases when you need to add the nul operator by yourself, example, when you are trying to break the character string word by word and try to save them in array of pointers , syntax : char *str[20] then you have to have add \0 at the end. And If you are not happy with strlen() function, you can code your own like this :

size_t count = 0;
while (str != '\0'){     //str is the string name
count++;
}

Thank you!

  • There is no such thing as a "*`nul` operator*" in C. You probably meant the `0`-terminator, aka null-terminator, aka `NUL`-terminator. – alk Jul 22 '16 at 09:15
  • I suggest you to open and revise book again. There is `NULL` which is for pointer, value assign by compiler to global pointer , for local pointer you need to assign that for yourself, and there is `nul` terminator which is the part of string, if you open your book and read the strings, then you will know what this term and sign `\0` called. Well thanks for and yeah, `0` is the digit , its not any terminator. @alk – Devesh Pratap Jul 22 '16 at 14:00
  • 1
    `NUL` is the name for the character with the value `'\0'` which is equal to the value `0`. It is used to terminate a C-"string". Those have *nothing* to do with the null-pointer value `NULL`, at least not in C. Mind the different number and capitalisation of the various `L/l`s. I recommend the C Standard here: http://port70.net/~nsz/c/c11/n1570.html – alk Jul 22 '16 at 14:18
  • Thanks, I guess I am the one who needs to read more, thanks for the link @alk – Devesh Pratap Jul 22 '16 at 22:41