Create arrays for the strings instead of using single characters, remove the s
characters from the input format specifier, and remove the spaces from the output format specifier:
#include <stdio.h>
int main(void)
{
char bname[64], date[64], sname[64];
int scode;
scanf("%63[^\n]%d %63[^\n] %63[^\n]", bname, &scode, date, sname);
printf("%s\n%d\n%s\n%s\n", bname, scode, date, sname);
}
A space (' '
) or newline ('\n'
) character in the input format specifier instructs scanf
to ignore all whitespace starting at that position. The 63
's specify the maximum width that is read (excluding the null-terminator), which is a good practice to avoid buffer overflows. Although, it is better to avoid scanf
altogether for safety reasons. See here for more information on scanf
.
This still does not print the BOOK NAME
, SERIAL CODE
, etc., but the problems seemed to lie with reading the data.
Additionally, only the stdio.h
header is necessary here.
Credits also go to stark, SteveSummit, Gerhardh and Oka.