I coded for a bit more, and in another interpretation of your question(this one not only detects if you just pressed enter but if you didnt place an integer i didnt check if negative numbers work) I used this function://DONT FORGET TO #DEFINE WRONG_REQUEST_MACRO "SOME MESSAGE"void readString(const char message*, char arrayChars *, int maxChars){ int stringSize; unsigned char flag=0; do{ flag =0; printf("%s", message); fgets(arrayChars, maxChars, stdin); stringSize = strlen(arrayChars); if (stringSize == 1){ printf("[INFO]Empty request. You just pressed ENTER.\n"); flag=1; } if (atoi(arrayChars)==0&&arrayChars[0]!=0){ printf("[INFO]You didn't enter a number.\n"); flag=1; } } while (flag == 1); if (arrayChars[stringSize - 1] != '\n'){ clearBuffer(); }else{ arrayChars[stringSize - 1] = '\0'; } while (strchr(arrayChars, '\'') != NULL || strchr(arrayChars, '?') != NULL || strchr(arrayChars, '*') != NULL || strchr(arrayChars, '\"') != NULL){ printf("%s ' %s '", WRONG_REQUEST_MACRO, arrayChars); break; }}应该像这样使用int command;char message[20];//yes this could be used with a char pointer but lets assume op doesnt know how to allocate memory or work with char pointers it wouldn't change that much but if he does know how to do it he will promptly changereadString("something something i suppose\n",message,20);command=atoi(message);尽管最后一个充满了调试重复"应该工作Welp the last although its filled with debugging "duplicates" should work 这篇关于有没有办法限制 C 中的 scanf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 05-31 10:47