Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My character arrays are merging in C

Tags:

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

like image 209
OMAX Avatar asked Dec 14 '15 16:12

OMAX


People also ask

What is the correct way to initialize character array?

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)

Can we store character in array in C?

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.

How do you concatenate a character array?

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 .


2 Answers

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).

like image 188
Mxsky Avatar answered Oct 19 '22 07:10

Mxsky


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"; |        |        |        |        |        |        | +--------+--------+--------+--------+--------+--------+ 
like image 33
haccks Avatar answered Oct 19 '22 05:10

haccks