How to limit scanf function in C to print error when input is too long?

前端 未结 6 1017
梦谈多话
梦谈多话 2020-12-11 07:17

I want to limit the scanf function so when I enter for example a char* array that has more then 30 characters, it will not get it and my outpu

6条回答
  •  隐瞒了意图╮
    2020-12-11 08:13

    You could use fgets and sscanf. With fgets you can read a little bit more than 30 characters and then check that you didn't get more than 30 characters.

    Or if you really want to use scanf use it with something more than 30 like %32s.

提交回复
热议问题