Why all the characters of an array are not printed?

Debojit Acharjee 455 Reputation points
2023-06-21T11:26:09.67+00:00

The following program uses an array of size 10 is assigned with a '-' character and then takes a string "hello" from the user input, but while printing the array it only shows the word "hello". Whereas, it shows all the 10 characters when printed using a character specifier (%c). Why all the 10 characters are not printed using the string specifier (%s)?

#include <stdio.h>

int main()
{

    char a[10];
    int i;

    for (i = 0; i < 10; i++)
    {
        a[i] = '-';
     
    }

    printf("%s\n", a);

    printf("Enter a word: ");

    scanf("%s", a);

    printf("Output string of a: %s when size of a is %d\n", a, sizeof(a));

    printf("Output characters of a: ");

    for (i = 0; i < 10; i++)
    {
        printf("%c", a[i]);
     
    }

}

Output:


Enter a word: hello

Output string of a: hello when size of a is 10

Output characters of a: hello----

Developer technologies | C++
Developer technologies | Visual Studio | Other
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 49,541 Reputation points
    2023-06-21T12:06:45.97+00:00

    Using the %s type specifier with printf for an array that is not null terminated is an error. You may get lucky or the contents of the a array will be printed and followed by garbage characters (whatever is in memory until a null is encountered) or experience other undefined behavior.

    Calling scanf with %s without specifying a width is an invitation to a buffer overrun. The scanf function automatically appends a null terminator to the input. You got lucky because the 5 characters of "Hello" plus a null terminator was less than size of the receiving array (10 characters).

    After reading "Helllo" into the a array printf with a %s type specifier recognizes the null terminator for the string. Calling printf with a %c type specifier treats each member of the a array individually.


  2. Giovanni Dicanio 160 Reputation points
    2023-06-22T17:17:47.33+00:00

    Whereas, it shows all the 10 characters when printed using a character specifier (%c). Why all the 10 characters are not printed using the string specifier (%s)?

    %s prints the string characters in sequence, until it finds a \0 NUL-terminator, which marks the end of the string.

    On the other hand, %c considers each character separately, so if you pass a[i] and use %c , the single a[i] char is printed out. %c doesn't bother about a sequence of characters ended by a NUL-terminator; %c just prints out the single char you pass to it.

    As a side note, since this is a C++ forum, I would encourage you to write C++ code, and use safer "tools" provided by the C++ language and its Standard Library, like using the robust std::string class instead of a raw C-style array, and reading strings with std::cin or std::getline, instead of using the C bug-prone scanf.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.