2
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}

Output:

Enter name: Dennis Ritchie Your name is Dennis.

So far I haven't found any specific valid reason for this question. Can anyone help me out?

Satyendra Yadav
  • 156
  • 3
  • 14
  • 4
    It's not the `printf` that the issue it's the `scanf`. It stops reading once it reaches the space. Look into using `fgets` instead. – AntonH Jan 26 '17 at 17:42
  • 2
    I'm not sure if you understand what I meant. The `scanf` stops reading when it reaches the space, so the variable `name` only contains `Dennis\0`. In fact, you could do another `scanf` and it would read the rest of the string, i.e. "Ritchie". The simple fact if that `printf("%s", ...);` does print spaces, but your string does not contain any as it was not read by the `scanf`. – AntonH Jan 26 '17 at 18:14
  • 1
    @satyendrayadav scanf stops at space and newline both See below answer. – Suraj Jain Jan 26 '17 at 18:19
  • Does The Below Answer solves Your Query ? – Suraj Jain Feb 09 '17 at 15:46
  • Soo it worked ? – Suraj Jain Feb 15 '17 at 18:11

1 Answers1

4

scanf only read till it gets to space that is why it is not storing after the first space , so your printf function is not faulty , it is the scanf that is not storing the complete string , stopping on encountering first space.

One should never use gets() , unless they completely know what they are doing , because it does not have buffer overflow protection , it continue to read after the buffer ends until it finds a new line or encounter a EOF. You can read more about that here.Please Check This Why is the gets function so dangerous that it should not be used?

You should instead use fgets().

    #include <stdio.h>
    int main(){

           char name[20];
           printf("Enter name: ");
           fgets(name,20,stdin);
           printf("Your name is %s.", name);
            return 0;
         }

Remember fgets() also reads newline character(the one you get when you press enter) so you should manually remove that.

Also I highly Recommend this answer for using fgets() to its full potential and avoiding common pitfalls.

This answer tells about using scanf to read string.What it says is the following:

   int main(){ 
      char string[100], c;
      int i;
      printf("Enter the string: ");
      scanf("%s", string);
      i = strlen(string);      // length of user input till first space
     do{          
        scanf("%c", &c);
        string[i++] = c;  // reading characters after first space (including it)
       }while (c != '\n');     // until user hits Enter

       string[i - 1] = 0;       // string terminating
       return 0;
     }

How this works? When user inputs characters from standard input, they will be stored in string variable until first blank space. After that, rest of entry will remain in input stream, and wait for next scanf. Next, we have a for loop that takes char by char from input stream (till \n) and appends them to end of string variable, thus forming a complete string same as user input from keyboard.

Community
  • 1
  • 1
Suraj Jain
  • 4,463
  • 28
  • 39