cEnter language multi-character scanf_s

Asked 2 years ago, Updated 2 years ago, 100 views

You do not enter it as a string, but as a character. But

    char a;

    scanf_s("%c", &a);
    printf("%c", a);

If I do this, there is no error.

    char a, b;

    scanf_s("%c%c", &a, &b);
    printf("%c%c", a, b);

This will cause an error. Which part is wrong??

string c error scanf

2022-09-21 10:18

1 Answers

https://docs.microsoft.com/ko-kr/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2019

Here,

Unlike scanf and wscanf, scanf_s and wscanf_s require you to specify buffer sizes for some parameters. Specify the sizes for all c, C, s, S

For scanf_s, wscanf_s (unlike scanf and wscanf), the buffer size must be specified for c, C, s, S, etc.

If you look at the example below, you can see that C also specifies the size as 1.

   result = scanf_s( "%d %f %c %C %s %S", &i, &fp, &c, 1,
                     &wc, 1, s, (unsigned)_countof(s), ws, (unsigned)_countof(ws) );


2022-09-21 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.