1

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.

1
  • Omg thank you for the information Commented Sep 7, 2020 at 18:16

4 Answers 4

3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you so much for telling me all the truth
2

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.

5 Comments

And if I initialize the array in this way, the last subscript contains '\0' or just '0' ?
As I wrote: 0. The C standard requires remaining elements to be initialized with 0=='\0' , not 48 == '0'
all right I got that the last subscript contains "zero" like in array[9] = 0; now what about the part 0 == '\0' they are different things, aren't they?
No, they are same '\0' is representation of value 0 as a character. You can use '\123' to represent an octal value.
You are right! Omg that's a revelation. I tried to start the loop from 0 and it doesn print nothing. However I also tried starting from 1 and it prints unknown characters. Is it because I try to print a char that got its value from an int? ( yes I posted the piece of code without trying it, I just wanted to understand the general rule, I din't need that specific piece code ). Anyway thank you so much for the explanations.
1

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.

1 Comment

Thank you so much for the quick and concise answer!
1

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.

1 Comment

I'm afraid you are not answering the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.