1

I am learning C and the concept of the char array in my tutorial seems to conflict with what I observe.

#include <stdio.h>

int main(){
    char input[64];
    printf("%c is found for the last index\n", input[64]);
    return (0);
}

According to the tutorial, this char array would have room up to 63 characters plus a null character at the end of the string for 64.

But it outputs ( is found for the last index, which is not either empty or \0 character displayed. What am I missing here for the understanding? or Is my compiler un-desirably behaving?

6
  • 8
    The array has room for 63 characters plus the \0 character. Those 64 characters are in indexes 0-63 so the last character is index 63. Index 64 does not exist at all. Commented Sep 24, 2020 at 21:37
  • 1
    The array has room for 64 total char, including the terminator. The last valid index of the array is 63. Attempting to read input[64] is an error. Commented Sep 24, 2020 at 21:39
  • 1
    As stated in the other comments, you are accessing the array out of bounds. That means that you are not reading from the array, but from some other neighboring memory address that contains data that is unrelated to the array. The data read from from such random addresses is meaningless and therefore often referred to as "garbage data". Commented Sep 24, 2020 at 21:43
  • 1
    The array has room for up to 64 characters, including a terminating NUL. But you didn't initialize it, so you can't know what characters there are in it. Anyway, the array indices span a range of 0 through 63 - by reading input[64] you access data outside the array, causing Undefined Behavior. Commented Sep 24, 2020 at 21:44
  • 1
    The array has space for 64 char objects. The array can contain a string, which is a sequence of characters followed by the '\0'-byte, but it does not have to contain a string, it can be just a bunch of char's. In your program the values in the array are not initialized, can have any random value and reading them may cause UB (unlikely on modern systems where char does not have a trap representation). Commented Sep 24, 2020 at 21:44

3 Answers 3

3

First of all, input[64] is out of range. C uses zero-based array, so only indexes 0..63 are valid here. Reading out of range element results in getting unpredictable data, writing out of range may result in program data and code crash. (BTW, accessing out of string buffer range was a very common reason for programs security vulnerability.)

Secondly, the array you use is not initialized, so you may got any unpredictable (garbage, random) value of any element. To initialize it with zeros you may write

char input[64] = {0};

Thirdly, the strings and char arrays are not the same things. Each string literal like "Hello, world" is zero-terminated string containing 12 string characters and 1 zero character, each standard string being properly called would save a zero-terminated string into a char array (previously allocated string buffer). To store such a string in the buffer (array) this array should have a room for each char and for zero char. But not any array of a chars should contain a string, it could be just an array of any chars to be treated other way than zero-terminated string. To hold the string in the example you may use

char input[64] = "Hello, world";

but only input[12] would be '\0' (zero char), input[13] .. input[63] would still be uninitialized.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Nick. Would you mine if you explain more about char array and string in detail with strlen()? I declared input [64]; and strlen() outputs 6. I think it's about what you say that char array is not string.
@candito123, strlen counts actual string length, not the array or buffer size. Actual string length means number of chars up to the first zero char in the buffer (zero char is not calculated). In case of char input[64] = "Hello, world" strlen(input) would return 12.
Reading out of range element not only results in getting unpredictable data, but may also result in crash, e.g. by unhandled Access Violation exception.
1

This char array aka string is not initialized, so it just points to a data with length of 64 bytes reserved for your use, until you set a value for it, the data it points to is what it has "garbage" random data. If you run it again you may also get various results.

Note that even after you set a value to a string, the next character after the last one used will indeed be null, but after that null there will still be garbage data.

Also, as I've seen in other comments, the index 64 is out of bounds, and in containers that keep track of their length you would get an error, the last char is size - 1, meaning 63.

2 Comments

The array does not point to anywhere. The array is an object, in this case an object of the size 64, not a pointer. Don't mix up pointers and arrays, array variables decay to pointer when you access them but they are still not the same thing.
My last comment was bit misleading, pointers are objects too.
1

But it outputs ( is found for the last index, which is not either empty or \0

Index 64 is actually one memory location beyond the memory allocated for the array input. Attempting to access an area of memory not owned by the process invokes undefined behavior

To create an array, eg as you have:

   char input[64];// 64 (unknown) char elements

the array brackets indicate the number of elements of the array

When using the array, C indexing always goes from 0 to N - 1, in this case 63

printf("%c is found for the last index\n", input[63]); // will return the last char in the array

Which in this case may or may not be \0.

It is always recommended to initialize the array:

char input[64] = {0};// 64 char elements, all initialized to zero (`\0`)

Comments

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.