Home > Enterprise >  Is a number between the 2 square brackets necessary regarding char variables
Is a number between the 2 square brackets necessary regarding char variables

Time:01-14

I just started programming in C and I see on the internet people declaring chars like, for example, char name[], without putting any number between the 2 square brackets. I used to code in C in high school and we always used to put a number when declaring char. Can someone explain when do we put a number, or if it is even necessary to use one when declaring a char variable?

I used to do it like this:

char name[20] = "John";

but I've seen on the internet people doing it like this:

char name[] = "John";

CodePudding user response:

char curse[] = "fie! cometh h're and englut mine own coxcomb thee distemperate fooleth!";

In this case, the compiler automatically computes the number of bytes, including the null-terminator. The size of the array is equal to the length of the string, plus the null-byte.

char curse[100] = "fie! cometh h're and englut mine own coxcomb thee distemperate fooleth!";

Whereas, this defines an array of 100 chars, and initializes it with a string of 70 or so bytes. The rest of the bytes are initialized to 0¹. The size of the array is 100 bytes, whereas the length of the string can be determined with strlen. NB that the size of the array and the length of the string it contains are not the same.

Aside: It's better to define the array size as a macro instead of having magic numbers all over your code. It's easier to maintain that way.

[1] From C11:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

i.e. 0.

CodePudding user response:

char name[] = "John"; will define a char array long enough to accommodate "John" (5 chars).

char name1[20] = "John"; defined an array of 20 char and initialization string only takes 5 bytes (abstracting from the rest of bytes) So you can use those remaining 15 bytes for example by appending another string to it.

strcat(name1, " Travolta");
`

CodePudding user response:

From the C Standard (6.7.9 Initialization)

22 If an array of unknown size is initialized, its size is determined by the largest indexed element with an explicit initializer. The array type is completed at the end of its initializer list.

and

14 An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

So in this declaration of a character array

char name[] = "John";

that is according to the second quote from the C Standard equivalent to

char name[] = { "John" );

the array name is initialized by the string literal "John".

According to the first quote from the C Standard the size of the array is determined by the number of characters (including the terminating zero character '\0') in the string literal. In fact the above declaration has the same effect as the following declaration

char name[] = { 'J', 'o', 'h', 'n', '\0' };

So as the number of characters in the string literal is equal to 5 then the array has exactly 5 elements and its size is also equal to 5 because sizeof( char ) is always equal to 1.

In this declaration

char name[20] = "John";

that again may be written like

char name[20] = { "John" };

the array is declared specifying explicitly 20 elements. The first 5 elements of the array have corresponding explicit initializers (characters of the string literal). All other elements of the array are implicitly initialized by 0.

Pay attention to that in C you may write

char name[4] = "John";

that is you may exclude the terminating zero character '\0' of the string literal from the list of initializers of the array. In this case the array name will not contain a string. In C such an initialization is incorrect and the C compiler will issue an error for such a declaration.

To output a character array that contains a string you can write for example

printf( "name = %s\n", name );

For the last shown declaration where the declared array does not contain a string you can write

printf( "name = %.*s\n", ( int )sizeof( name ), name );
  • Related