-3
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

    int main(){
    char bname,date,sname;
    int scode;
    scanf("%[^\n]s \n %d \n  %[^\n]s \n  %[^\n]s"  ,&bname,&scode,&date,&sname);
    printf(" %s \n %d \n  %s \n  %s"  ,bname,scode,date,sname);
    return 0;

}

my need;

i/p:

The Alchemist
8741
03/02/2020
Finn Martin

o/p:

BOOK NAME:The Alchemist
SERIAL CODE:8741
ISSUE DATE:03/02/2020
STUDENT NAME:Finn Martin
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Ibu
  • 11
  • 1
  • 2
    Welcome to SO. There is no question visible. What exactly is your problem? What part of "my need" is input, output, expected output? – Gerhardh Aug 20 '21 at 15:34
  • 2
    `char bname,date,sname;` These are single character variables that can only hold a string of length 0 (including terminating 0 byte). Do you want to use arrays? – Gerhardh Aug 20 '21 at 15:35
  • 1
    Another problem: You've got some extra `s`'s in your format string. You'd want `%[^\n]`, not `%[^\n]s`. (But with that said, `fgets` is usually a better way of reading whole lines.) – Steve Summit Aug 20 '21 at 15:35
  • In C, a string is an ***array*** of *null-terminated* characters. – Some programmer dude Aug 20 '21 at 15:37
  • Ibu, care to explain why my answer was unaccepted? – Yun Aug 21 '21 at 06:17

2 Answers2

1

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.

Yun
  • 3,056
  • 6
  • 9
  • 28
  • You aren't printing the field names – stark Aug 20 '21 at 15:43
  • @stark True, I should have comment on that. Reading the data was the problem, I assumed. Printing the field names is trivial. – Yun Aug 20 '21 at 15:48
  • @SteveSummit Thank you. The question has since been updated with a clear input example _not_ showing spaces, so I'll update the answer. – Yun Aug 20 '21 at 15:49
  • 1
    @SteveSummit Good catch! Answer updated. – Yun Aug 20 '21 at 16:04
  • The `scanf` specifier `"%[^\n]"` is [as dangerous as `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). If you *must* teach `scanf`, please at the very minimum instruct others on the use of *field-width specifiers*; e.g. `"%63[^\n]"`. – Oka Aug 20 '21 at 16:51
  • @Gerhardh Good suggestion. And no, the `%d` format specifier skips all leading whitespace. – Yun Aug 20 '21 at 17:02
  • OK, I always miss that detail. ;) – Gerhardh Aug 20 '21 at 17:09
  • @Oka Good point, I agree. Unfortunately, `scanf` is still taught in many courses without mentioning security. Answer has been updated. – Yun Aug 20 '21 at 17:09
0

You want char array instead of char. Also, there are some format issue as pointed out by the comments.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(){
    char bname[100],date[100],sname[100];
    int scode;
    scanf("%[^\n]\n%d\n%[^\n]\n%[^\n]"  ,bname, &scode, date, sname);
    printf("BOOK NAME:%s\nSERIAL CODE:%d\nISSUE DATE:%s\nSTUDENT NAME:%s\n"  ,bname,scode,date,sname);
    return 0;
}