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

前端 未结 6 1039
梦谈多话
梦谈多话 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条回答
  •  猫巷女王i
    2020-12-11 07:59

    Well in C you can do:

    #include 
    
    ...
    
    if(strlen(array_ptr) > 0) error();
    

    Obviously you need a bigger buffer to actually first get the input to it, and then check it's length, so the array could be of e.g. 512 bytes. When you copy strings to it, you need to check that you are getting 0 at the end.

提交回复
热议问题