If you want to allow embedded spaces, modify the scanf formats this way:
#include <stdio.h>
int main(void) {
char job[100], hobby[100];
printf("Enter job:");
scanf("%99[^\n]%*c", job);
printf("Enter hobby:");
scanf("%99[^\n]%*c", hobby);
printf("%s,%s", job, hobby);
return 0;
}
But be aware that empty lines will not be accepted by this scanf format. The linefeed will stay in the input stream, the second scanf will fail too and job and/or hobby will have indeterminate contents, letting printf invoke undefined behavior.
Is is much more reliable to use fgets() and strip the '\n'.
#include <stdio.h>
#include <string.h>
int main(void) {
char job[100], hobby[100];
printf("Enter job:");
if (!fgets(job, sizeof job, stdin))
return 1;
job[strcspn(job, "\n")] = '\0';
printf("Enter hobby:");
if (!fgets(hobby, sizeof hobby, stdin))
return 1;
hobby[strcspn(hobby, "\n")] = '\0';
printf("%s,%s", job, hobby);
return 0;
}
scanf("%[^\n]s",s1);-->scanf("%[^\n]%*c", s1);printf(), not a comma. The problem is that there's a newline in the input and your format won't allow the the newline to be read, so it fails. Check the return status ofscanf(). And read the newline before scanning, or put a space before the%[^\n]. Also notice that a scan set is%[…]; it is not a modifier for%s.scanfshould be given the maximum number of characters to read and store intos1ands2, otherwise invalid input will invoke undefined behavior. This kind of bug is a blatant security flaw.