-4
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char firstname[15];
    char lastname[15];
    char crush_first[15];
    char crush_last[15];
    int babies;


    printf("What is your first name?\n");
    scanf("%s", firstname );

    printf("What is your last name?\n");
    scanf(" %s", lastname);
    /* see i have added space before the character conversion but on exectution 
    of this file no space is in between the two strings*/


    printf("What is your crush's first name?\n");
    scanf("%s", crush_first );

    printf("What is your crush's last name?\n");
    scanf(" %s", crush_last );

    printf("How many kids will you have?");
    scanf("%d", &babies );

    printf("%s%s will have a lovely marriage with %s%s and they will have %d kids",firstname,lastname,crush_first,crush_last,babies);

}

now here i want to do is to add space by default in the string. "__etc" i want the string to also store these values . Though i have added space before %s repeatedly but it is not recognizing.

  • Possible duplicate of [scanf() doesn't accept whitespace](https://stackoverflow.com/questions/25256697/scanf-doesnt-accept-whitespace) – kiner_shah Jul 15 '18 at 07:54
  • What do you mean by "*no space is in between the two strings*"? You're the one typing the input; you determine what's there. – melpomene Jul 15 '18 at 08:09
  • The leading space before `%s` only instructs `scanf` to filter out *leading* whitespace but `%s` does that anyway. The space is only necessary before the specifier `%[]` or `%c` if you want to filter leading whitespace. – Weather Vane Jul 15 '18 at 08:09
  • Are you simply looking for `printf("%s %s will have a lovely marriage ...")` (note the space between `%s` and `%s`)? Your question is really unclear. – melpomene Jul 15 '18 at 08:57

1 Answers1

1

From scanf doc:

s   matches a sequence of non-whitespace characters (a string) [...]

Also if someone enters string longer then your receive buffer, you will overflow the buffer.

Maybe use fgets if you want to read the line up until a newline:

fgets(lastname, sizeof(lastname), stdin);
KamilCuk
  • 120,984
  • 8
  • 59
  • 111