-2

I’m wondering why this code prints a zero.

int x = 10;
char newChar[x];
printf(“%d\n”,strlen(newChar));
STL34
  • 315
  • 3
  • 6
  • 15
  • You should never use an uninitialized variable (`newChar` in your case). – Fiddling Bits May 15 '20 at 15:19
  • 2
    I'm wondering why you would expect it to not print 0. It's undefined behavior and that array could be full of non zero garbage but I'm not sure why you would expect something other than 0 or any specific number. – Retired Ninja May 15 '20 at 15:21
  • @RetiredNinja That's a good point. If OP is wondering why it prints zero, OP must expect it to print something else. – Fiddling Bits May 15 '20 at 15:24
  • That isn't how `strlen` works. The length of a string is measured up to the first 0 byte. Perhaps you are confusing it with `sizeof`? – Retired Ninja May 15 '20 at 15:24
  • I’m using the code to copy one char array into a new bigger char array, for the purpose of adding commas the string. Eg 1000 becomes 1,000. Would it be better to use malloc? – STL34 May 15 '20 at 15:30
  • 3
    Now I'm more confused than ever. If you have some real code you're having trouble with then ask a question about that code. I don't see how `strlen` of an uninitialized array has anything to do with adding commas to a string. – Retired Ninja May 15 '20 at 15:36
  • No, using an array created with `malloc` is the same (for the purposes of this question) as using the uninitialized array. – user3386109 May 15 '20 at 15:42
  • Using sizeof() instead of strlen works for creating an empty char array of a particular size. Thank you. – STL34 May 15 '20 at 15:49

1 Answers1

2

strlen() computes the amount of characters in a string. It counts characters up to the null character \0, which terminates a string. It returns the amount of characters read up to but not including that null character.

In your provided example the char array newChar is uninitialized; it has no string in it.

Thus, the output shall be 0.

The behavior is per standard undefined, when using uninitialized objects. In production code, Always initialize objects or, in the case of VLAs like newChar is, assign its elements with "placeholder" values or use strcpy (with VLAS of char) to copy an empty-string ("") into it, before using them in any manner.

Technically, it is possible that strlen() will return a value different than 0 on a different machine or already at a different execution of the program just because the behavior is undefined -> one or more elements could have a non-zero value followed by an element with a 0 value - making the pseudo-string effect perfect.


Maybe you wanted to calculate the number of elements in newChar.

Therefore, sizeof instead of strlen() would be appropriate:

int x = 10;
char newChar[x];
printf("%zu\n", sizeof(newChar) / sizeof(*newChar));

Online example

#include <stdio.h>

int main(void)
{
    int x = 10;
    char newChar[x];
    printf("The number of elements in newChar is: %zu\n", sizeof(newChar) / sizeof(*newChar));
}

Output:

The number of elements in newChar is: 10