0

Can anyone explain why I used 2 times scanf or 2 gets or gets then scanf works but if scanf then gets, it doesn't work thanks !!!

#include <stdio.h>
#include <string.h>
int main() {
    char firstname[100], lastname[100], name[100] = "";
    printf("first name: ");
    scanf("%s", firstname);
    printf("last name: ");
    gets(lastname);
    strcat(name, firstname);
    strcat(name, " ");
    strcat(name, lastname);
    printf("your name is ");
    puts(name);

}
Khánh Phạm
  • 31
  • 1
  • 7
  • 2
    Never ever use`gets` _ever_ See here: https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – mediocrevegetable1 Feb 16 '21 at 03:21
  • You can't mix `scanf` and `fgets` (or `gets`, but don't use `gets` at all). `scanf` treats the newlines differently, and this leads to endless confusion. Either use `fgets` to read all your input, or `scanf`, but don't try to use a mixture. – Steve Summit Feb 16 '21 at 04:03

0 Answers0