0

I am still a C programming newbie.

I have heard the character string has always '0' or '\0' as the final character.

Then I have a one question. why the sample line below has '5' at the last position?

Why isn't it '0' or '\0'?

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

The line above from How to initialize all members of an array to the same value?

TADASUKE
  • 95
  • 8
  • 2
    Why do you think it should? – Fildor Aug 24 '20 at 10:06
  • 4
    is this a character string? – user253751 Aug 24 '20 at 10:06
  • 2
    Even char arrays don't have to have sentinel `\0` (null byte); you only need if you're going to use it as a string. – P.P Aug 24 '20 at 10:07
  • 1
    the functions that invoke stdout (like printf) should all check and add the trailing 0. you don't need the 0 at the end, but many libraries and such depend on this. – clockw0rk Aug 24 '20 at 10:26
  • Thank you everyone for your commenting! I have not understood between the array of int and char. And for @P.P, I could get that the case that '\0' is needed! Thank you. – TADASUKE Aug 24 '20 at 11:26

3 Answers3

4
char cArr1[] = {'a', 'b', 'c'};
char cArr2[] = "def";
int iArr3[] = {1, 2, 3};
int iArr4[5] = {1, 2, 3};

memory layout
=============
var name    memory address    value
cArr1       AAAA0000          'a'
            AAAA0001          'b'
            AAAA0002          'c'
            AAAA0003          unknown (may have a '\0' by chance)
            ...
cArr2       BBBB0000          'd'
            BBBB0001          'e'
            BBBB0002          'f'
            BBBB0003          '\0' is inserted by the compiler
            ...
iArr3       CCCC0000          1
            CCCC0004          2
            CCCC0008          3
            CCCC000C          unknown (may have any value)
            ...
iArr4       DDDD0000          1
            DDDD0004          2
            DDDD0008          3
            DDDD000C          0 (not defined explicitly; initialized to 0)
            DDDD0010          0 (not defined explicitly; initialized to 0)
            ...
ssd
  • 2,340
  • 5
  • 19
  • 37
  • Thank you for providing clear formated information! Could I ask one more question? On the section cArr2, it is written that '\0' is inserted by the compiler. Does this mean that I can use three letters if I declare char cArr [3]? I mean, I have not to keep one last position for '\0' thanks for the compiler to add it automatically? – TADASUKE Aug 24 '20 at 11:20
  • 1
    @CaptainCookie : Nope! This is not a *dynamic* variable but a *compile time* variable; therefore, if your code line is `char cArr2[3] = "def";`, the compiler will, still, allocate 4 bytes (during compilation) and lay down the data in the order 'd', 'e', 'f', '\0'. However, the reason being obvious, it's a common practice to leave the brackets empty for this case. – ssd Aug 24 '20 at 11:33
  • Thank you for your quick reply! What you mean is that the '\0' here is just for the compiling processing? So, if my declaration is `char characterArray[4];`, can I use only 3 letters freely, like "sun", not "moon"? I wish I could get one additional clarification. – TADASUKE Aug 24 '20 at 12:18
  • 1
    @CaptainCookie : The issue is; whether **you define the string literal in the code** (where, mem is allocated at compile time) or **not**... For `char cArr2[4] = "abcd";`, the string literal is defined at compile time; so, the compiler automatically allocates 5 spaces and if any other variable exists, its memory allocation will start from the 6th byte... cont. – ssd Aug 24 '20 at 12:36
  • 1
    @CaptainCookie : ... However, if you define an array as `char cArr2[4];` (w/o initializing using a string literal), compiler will allocate only 4 bytes and the next variable in the code is very susceptible to start from the 5th byte. In the latter case, you'd better stick to 3 bytes and a '\0' (say, you `scanf` a string and place it in `cArr2`), which adds up to 4 (the available mem space). – ssd Aug 24 '20 at 12:38
  • I've understood clearly! Although I had referenced several articles, some points were not obvious to me. But now, I got them. Thank you @ssd so much for taking time for me! – TADASUKE Aug 24 '20 at 13:01
  • @ssd You maybe should note that `sizeof(int) == 4` , although common, is not guaranteed for every implementation. That means your illustration regarding the memory layout for the `int` array is not representative for every case possible. – RobertS supports Monica Cellio Aug 24 '20 at 14:02
2

"Why can this sample line have not 0 OR '\0' character at the end of the array?"

Because myArray is an array of int (and does not contain a string)! A null terminator (for strings) can only be applied to char, but not int arrays.

And even an array of char does not a null terminator per se. Only if the array of char should contain a string and you want to use the content of the array as string, a null terminator is required.

Beside that, an array of int can of course hold the int value 0 inside of the last element but you currently intermix two different things here.

  • 1
    I will do more search about the difference between the array of int and char. Thank you for your providing advice to tell what I have misunderstood! – TADASUKE Aug 24 '20 at 10:59
2

Strings are character arrays the actual elements of which are terminated by zero character. And most standard C string functions relay on this convention.

This

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

is an integer array. Zero is a valid integer value. And it is used very often as an actual value of an array among other integer values.

Of course you can make zero a sentinel value of an integer array. But there are no standard C functions that use zero as a sentinel value for integer arrays.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335