For some reason, my second character array (var2) merges with the first one (var1). Here is my code:
#include <stdlib.h> #include <stdio.h> #include <string.h> int main() { char var1[5] = "Hello"; char var2[5] = "World"; printf("This program can write:\t%s\t%s\n", var1, var2); getch(); return 0; }
after compiling it, I got the following print:
This program can write: Hello WorldHello
When I changed the code to printf("This program can write:\t%s\n", var2);
I got the following print:
This program can write: WorldHello
So It's clear that that var1 is merging with var2.
Is this some kind of compiler bug. If so, how can I fix it? I tried reinstalling MINGW, but I'm still getting the same results.
Thanks a lot
You can initialize a one-dimensional character array by specifying: A brace-enclosed comma-separated list of constants, each of which can be contained in a character. A string constant (braces surrounding the constant are optional)
The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.
Concatenate Two String ArraysConcatenate text with the strcat function. Note that when concatenated in this way the output string will insert a whitespace character between the input strings. Strings and character vectors can be combined using strcat .
Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
char var1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
You can more simply do :
#include <stdlib.h> #include <stdio.h> #include <string.h> int main() { char var1[] = "Hello"; char var2[] = "World"; printf("This program can write:\t%s\t%s\n", var1, var2); getchar(); return 0; }
The C compiler automatically places the '\0'
at the end of the string when it initializes the array (if the array is long enough to contain the '\0'
, otherwise the '\0'
will be dropped rather than overwrite another variable).
When using %s
as format specifier in printf
, it reads the character from memory and stops upon finding a '\0'
character. If it doesn't find the '\0'
character then reads until it find a '\0'
character somewhere in the memory.
In the above snippet both of var1
and var2
are character arrays of length 5
. Since you are using %s
as a format specifier you need to terminate them with '\0'
. This can be done by increasing the size of arrays. This will append a '\0'
character by default
char var1[6] = "Hello"; char var2[6] = "World";
See the difference between char var1[5] = "Hello";
and char var1[6] = "Hello";
+--------+--------+--------+--------+--------+ | | | | | | | 'H' | 'e' | 'l' | 'l' | 'o' | char var1[5] = "Hello"; | | | | | | +--------+--------+--------+--------+--------+ +--------+--------+--------+--------+--------+--------+ | | | | | | | | 'H' | 'e' | 'l' | 'l' | 'o' | '\0' | char var1[6] = "Hello"; | | | | | | | +--------+--------+--------+--------+--------+--------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With