Rajeesh Rarankurissi
Rajeesh Rarankurissi

Reputation: 149

String Declartion in C

What is the difference between Code 1 and Code 2 shown below. I am getting same out put in both case . Is there any difference internally ?

Code 1

char test[30]="KEL";
strcat (test,"DATA");

Code 2

char test[]="KEL"
strcat (test,"DATA");

Upvotes: 3

Views: 160

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Let's start from the C Standard. According to it (6.7.9 Initialization)

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.

and

19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

How these quotes are applied to the character array initializations?

In this code snippet

char test[30]="KEL";
strcat (test,"DATA");

the array test is declared having 30 elements. The first four elements of the array are explicitly initialized by the elements of the string literal that can be represented like { 'K', 'E', 'L', '\0' }. All other elements are implicitly initialized like objects with the static storage duration that is by the value '\0'.

So the array test after its declaration internally looks like

{ 
'K', 'E', 'L', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0' 
}

In this second code snippet

char test[]="KEL"
strcat (test,"DATA");

there is declared an array of unknown size where its size is calculated according to the number of initializers. As the string literal has four characters { 'K', 'E', 'L', '\0' } then the array will have exactly the same four characters.

In the first code snippet you can change the elements of the array that have the value '\0' . For example you could write

test[3] = 'D';
test[4] = 'A';
test[5] = 'T';
test[6] = 'A';

and the array will look like

{ 
'K', 'E', 'L', 'D', 'A', 'T', 'A', '\0', '\0', '\0', '\0', '\0', '\0', 
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0' 
}

That is as result it will contain the string "KELDATA". Instead of the several statements with assignments it is simpler to write

strcat (test,"DATA");

and get the same result.

However what about the second code snippet? As it is seen it has no any "free" elements except the elements initialized by the string literal "KEL". So what you can change is only his four elements. You can not append new data to the string literal stored in the array. You can only overwrite them.

Thus this statement

strcat (test,"DATA");

will result in overwriting the memory beyond the array and the program will have undefined behavior.

Upvotes: 2

In your first code sample, test will have space for 30 characters.
The first 4 will be the string "KEL"+ nul terminator (the rest will also be zero initialized if my memory serves, thanks chux). Concatenating "DATA" to it is well defined.

In the second sample, it will have space for 4 only, since the length of the buffer is deduced from the string literal. When you'll concatenate "DATA" to it, you'll write beyond the edge of the buffer. That's undefined behavior.

And if the behavior is explicitly undefined by the standard as it is here, the implementation of your compiler and run-time can do whatever. Your program can crash. Or it can be made to run sinister code. It can also appear to work as it does in your case. But that's not something you can rely on.

Upvotes: 6

nmd_07
nmd_07

Reputation: 706

char test[30]="KEL";

In this case the compiler allocates 30 bytes in the stack. Strings can be concatenated using strcat as long as final string does not exceed 30 bytes in size including the null-terminator.

char test[]="KEL"

In this case, however, the compiler itself decides to allocate sufficient memory for the given string. It is 3 bytes plus the '\0'(null terminator) so there is not enough room for even another character.

Attempting to write beyond what the compiler allocates is, as already stated by StoryTeller, undefined behavior, which means the program may execute correctly or may crush. Or may do both time to time. Undefined behavior is also very compiler specific attribute. Exactly the same source code compiled by gcc, clang or any other compiler may produce different results.

Note: A character type (char) is 1 byte in size.

Upvotes: 1

M.Abdullah
M.Abdullah

Reputation: 61

Code 1 char test[30]="KEL";// You are not allowed to enter a value greater than 30 characters but less than 30 its ok.

Code 2 char test[]="KEL" //Give you a free hand you are not bound to enter any fixed size value.

Upvotes: -1

Related Questions