#include<stdio.h> int main() { char commandA[5]; char option[4]; printf("Input string: "); scanf_s("%s",commandA,(unsigned int)sizeof(commandA)); printf("%s ", commandA); printf("Input second string: "); scanf_s("%s", option, (unsigned int)sizeof(option)); printf("%s ", option); return 0; }
代码如上,如果commandA输入的长度超过定义的长度减一的时候,就会读取字符串异常。
谨记定义好字符串的长度。可以对scanf_s的返回值进行判断。