anotherOne
anotherOne

Reputation: 1573

\0 when initializing a char array with a loop

I need to initialize a char array with a loop and print it. Just like that:

int main( void )
{
    char array[ 10 ];

    for( int i = 1; i < 10; ++i ) {
        array[ i - 1 ] = i;
    }

    // array[] contains numbers from 1 to 9 and an unitialized subscript

    printf( "%s", array );
}

I want to know if I need to put the '\0' character in array[ 9 ] or if it is already there.
In other words: once I declared char array[ 10 ]; does the last subscript contains '\0' ?

I searched for similar questions and the better I could find is this where the array is filled with a loop but till the end, leaving no space for the terminating character.

Please tell me the truth.

Upvotes: 1

Views: 1099

Answers (4)

chqrlie
chqrlie

Reputation: 144695

once I declared char array[10]; does the last subscript contains '\0' ?

The answer is NO: when you define the array as an automatic variable (a local variable in a function), it is uninitialized. Hence none of its elements can be assumed to have any specific value. If you initialize the array, even partially, all elements will be initialized, either explicitly from the values provided in the initializer or implicitly to 0 if there are not enough initializers.

0 and '\0' are equivalent, they are int constants representing the value 0. It is idiomatic to use '\0' to represent the null byte at the end of a char array that makes it a C string. Note that '0' is a different thing: it is the character code for the 0 digit. In ASCII, '0' has the value 48 (or 0x30), but some ancient computers used to use different encodings where '0' had a different value. The C standard mandates that the codes for all 10 digits from 0 to 9 must be consecutive, so the digit n has the code '0' + n.

Note that the loop in your code sets the value of 9 elements of the array to non zero values, and leaves the last entry uninitialized so the array is not null terminated, hence it is not a C string.

If you want to use the char array as a C string, you must null terminate it by setting array[9] to '\0'.

Note also that you can print a char array that is not null terminated by specifying the maximum number of bytes to output as a precision field in the conversion specifier: %.9s.

Finally, be aware that array[0] = 1; does not set a valid character in the first position of array, but a control code that might not be printable. array[0] = '0' + 1; set the character '1'.

#include <stdio.h>

int main(void) {
    char array[10];

    /* use the element number as the loop index: less error prone */
    for (int i = 0; i < 9; ++i) {
        array[i] = `0` + i + 1;
    }

    // array[] contains numbers from 1 to 9 and an unitialized subscript
    printf("%.9s\n", array);  // prints up to 9 bytes from `array`

    array[9] = '\0';
    // array[] contains numbers from 1 to 9 and a null terminator, a valid C string
    printf("%s\n", array);  // produce the same output.
    return 0;
}

Upvotes: 3

MD DANISH
MD DANISH

Reputation: 36

NO, You dont need to put '\0' at the end that is array[9]. why?

because when an array (char,int,float) is uninitialized it contains garbage values. After initializing partial or full all other elements becomes 0 or \0 in case of char array.

example: char array[10];
all elements contains garbage value.

after intialization

char array[10]={'a' ,'b'}; all other elements automitically becomes '\0'

this is true in case of structures also.

Upvotes: 1

Gerhardh
Gerhardh

Reputation: 12404

In other words: once I declared char array[ 10 ]; does the last subscript contains '\0' ?

No.

You define a local variable and do not initialize it. These variables are not initialized by default but hold indetermined values. If you want to have a defined value, you need to initialize or assign it:

char array[ 10 ] = "";

This will define an array with 10 elements. As there is an initializer, the first element will be set to 0 (=='\0') due to the provided string literal. Furthermore all other elements will be set to 0 because you provide less initializer values than you have elements in your array.

Upvotes: 2

once I declared char array[ 10 ]; does the last subscript contains '\0' ?

No. It's uninitialized. It has garbage values in every element from whatever code used the same piece of memory last.

Upvotes: 1

Related Questions