-1

In Visual studio 2017, I have a problem with scan_s(), printl_s() and char to code simple input and output app. Please review 2 app and please help to explain help me what is the issue when I used scan_s(), printl_s() and char.

This code is ok:

#include <stdio.h>
int main() {
    char name[30];
    printf("Enter name: ");
    gets(name); // enter string
    printf("Name: ");
    puts(name); // display string
    _getch();
}

Enter name: Dung_cute
Name: Dung_cute

This one is wrong:

#include <stdio.h>
int main() {
    char name[20];
    printf_s("Enter name: ");
    scanf_s("%c", name); // enter string
    printf("Your name is: %s.", name);
    _getch();
}

Enter name: Dung_cute
Your name is: D?????aietnauie'ai.
Mathieu
  • 8,840
  • 7
  • 32
  • 45
Le Dung
  • 33
  • 4

2 Answers2

4

scanf_s requires you to provide also the number of bytes to read. By default, it only reads one.

Please find here more information.

Example (from the link above):

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

Therefore, your

scanf_s should be something like:

scanf_s("%c", name, _countof(name));

You may also need to change the type specifier to "%s". I do not have Visual Studio at hand to test.

scanf_s("%s", name, _countof(name));
          ^
virolino
  • 2,073
  • 5
  • 21
  • I got it. Thank you so much for your help, i lost a lot of time for this issue. – Le Dung May 17 '19 at 07:03
  • 3
    You are welcome. It can happen to anyone. It is just a matter of reading the manuals more carefully - in this case, the syntax and parameters of scanf_s(). – virolino May 17 '19 at 07:05
  • @LeDung Is this answer fix your problem, do not hesitate to accept it (https://meta.stackexchange.com/a/5235/519684) – Mathieu May 17 '19 at 07:17
  • i want to become a embedded engineer so i try to learn C program and i feel it so hard but may be you say true: just need to learn to do things properly. Once again, thank you. This is the first time i ask at Stack overflow. – Le Dung May 17 '19 at 07:19
  • @Mathieu This is the first time i ask at Stack overflow so i don't know how to use them. I will read and do it. Thanks – Le Dung May 17 '19 at 07:24
  • 2
    @LeDung: take things step by step. Do not close a topic until you really understand WHY it works. Don't just copy and paste sequences of code until it compiles, but go the extra mile to understand each and every line and every character. If you do that, things will be (relatively) easy. – virolino May 17 '19 at 08:07
0

Whatever resource you are learning from, stop it immediately.

Programming in C is dangerous, and any resource that recommends the gets function is not up to the task. If you continue to use this resource for learning, you will write code that crashes all the time and will have security vulnerabilities like buffer overflows.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • 3
    Drinking water can be dangerous too, but it does not mean that we have to stop it. We just need to learn to do things properly. – virolino May 17 '19 at 06:48
  • 2
    @virolino There's no way to use `gets` properly: https://stackoverflow.com/q/1694036 – alx - recommends codidact May 17 '19 at 07:12
  • 4
    I agree that using gets() can lead to problems, but the same is true for pretty much everything else in C. "for" loops an go out of boundaries, "printf" and "scanf" can be are dangerous too, undesirable infinite loops are not a rarity, and examples can continue. With C, everything is a bless, and everything is also a curse. It is in the skills and the experience of the programmer to make things proper. And I did not even start discussing pointers and dynamic allocation. – virolino May 17 '19 at 07:17
  • @virolino As much as I'd like to comment on every beginner C question with "please use a reliable programming language", it's too much work for a single person. – Roland Illig May 17 '19 at 07:38
  • 1
    C is a very reliable if used properly. Modern cars and airplanes run safely thanks to it. Telling people to stay away from C because it is not reliable is like telling people to stay away from food because it can be spoiled. The C language is still VERY popular these days, unlike many other languages, exactly because it CAN BE very reliable. If you are a "single person" against everybody, then you should revise some of your opinions, maybe. – virolino May 17 '19 at 08:05
  • It's incredibly hard to write correct programs in C. Did you ever hear about a buffer overflows vulnerability in Java, Go, C#, Rust? Or a use-after-free? Or an array index out of bounds silently reading wrong data or writing to completely unrelated memory? – Roland Illig May 17 '19 at 09:17