Diego Salas
Diego Salas

Reputation: 153

Why is the printf specifiers, "%s", printing multiple variables at once?

I declared four string variables and initialized in four different ways. I then used the printf function four times, as the name implies, to print to the console the four string variables.

I already tried printing one at a time, but that didn't worked out. After the first attempt, I tried using the escape sequence but that didn't work. I then tried searching online of finding the proper ways to print out string, but all I've found came back to same using the printf function.

#include <stdio.h>
int main() {
    char name1[] = "Ignacio";
    char name2[8] = "Ignacio";
    char name3[] = {'D', 'i', 'e', 'g', 'o'};
    char name4[5] = {'D', 'i', 'e', 'g', 'o'};

    printf("Name: %s\n", name1);
    printf("Name: %s\n", name2);
    printf("Name: %s\n", name3);
    printf("Name: %s\n", name4);

    return 0;
}

EXPECTED OUTPUT:

Name: Ignacio
Name: Ignacio
Name: Diego
Name: Diego

ACTUAL OUTPUT:

Name: Ignacio
Name: Ignacio
Name: DiegoIgnacio
Name: DiegoDiegoIgnacio

Upvotes: 2

Views: 1101

Answers (2)

Cabbage Champion
Cabbage Champion

Reputation: 1213

Whats happening is the a null terminating character is implicitly being added when you initialize a char[] like below:

char myStr[] ="Ignacio";

If you want to initialize a char[] with individual characters you must initialize it like the following because the null terminating character must be explicitly added:

char myStr[] ={'I', 'g', 'n', 'c', 'i','0','\0'}).

Explanation for the Behaviour in Your Case:

Memory Layout:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] [10] [11]

'D' 'i' 'e' 'g' 'o' 'D' 'i' 'e' 'g' 'o' 'I' 'g' 'n' 'a' 'c' 'i' 'o' '\0'

After you initialize your strings, the characters are arranged in memory like above. In the C language, all valid strings must have a null terminating character at there end to specificity the end of the string. The reason why is printf will just start at the address of the char* passed to it and will keep reading characters until a '\0' character is encountered. Because of this, if you pass printf an address to an array of characters that does not contain a '\0', then printf will keep marching past those characters into memory until a null terminating character is encountered. (This could result in a segmentation fault)

Explanation for Your Output: (Reference the memory layout above)

Name: DiegoIgnacio

printf will start at index [5] and will print all characters until the '\0' character, thus "DiegoIgnacio" ([5] - [11]) is the output.

Name: DiegoDiegoIgnacio

printf will start at index [0] and will print all characters until the '\0' character, thus "DiegoDiegoIgnacio" ([0] - [11]) is the output.

Upvotes: 2

Yu Hao
Yu Hao

Reputation: 122373

char name3[] = {'D', 'i', 'e', 'g', 'o'};
char name4[5] = {'D', 'i', 'e', 'g', 'o'};

These two are not strings, because they are not null-terminated.

Try:

char name3[] = {'D', 'i', 'e', 'g', 'o', '\0'};
char name4[6] = {'D', 'i', 'e', 'g', 'o', '\0'};

Upvotes: 4

Related Questions