2

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

2 Answers 2

4
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'};
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.