i'm new to programming and would like to know whether the maximum no. of characters that an array of characters
eg. char c[10]
would be 10 or 11(adding the null character)
thanks!!
i'm new to programming and would like to know whether the maximum no. of characters that an array of characters
eg. char c[10]
would be 10 or 11(adding the null character)
thanks!!
[10] means 10 elements. If that array is used as a string then it is expected that the string terminates with a null. Therefore, the string has a maximum up to 9 non-null characters maximum, followed by null.
The maximum number of characters in this array would be 10. They are indexed from 0 to size-1. This means that the array consists of the following elements:Code:char c[10];
If you're making a string (an array of characters terminated with a null character, '\0'), then it's up to you, the programmer, to make sure the number of elements is large enough to fit the null character.Code:c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9]
got it...thanks!
if char s[10] represents a character array of 10 elements then why does printing s[15] give a garbage value instead of showing an error at accessing an element that does not exist??Code:char s[10]; printf("%c", s[15]);
One of the beauties of C. There is no run-time checking of array bounds. The programmer is free to make bugs such as this... the most common form of bug. Accessing an array beyond its defined bounds will get garbage - or you could cause a more severe effect if you are violating some memory privilege area.