gets输入,puts输出,字符串,字符串数组 | 您所在的位置:网站首页 › ch数组怎么输出 › gets输入,puts输出,字符串,字符串数组 |
1、输入输出一个字符串 #include #include int main() { char str1[50]; gets(str1); puts(str1); return 0; }2、输入输出一个字符串数组 #include #include int main() { char str2[10][50]; printf("请输入3个字符串:\n"); for(int i=0;i puts(str2[i]); } return 0; }3、gets注意点: gets会识别换行符 \n 为一个字符串的输入结束标志 gets获取一个字符串之后识别字符串后面的换行符 \n,并将其丢弃,不会留在缓冲区 因为scanf在读入一个数据后,会把末尾的回车,换行符 \n 留在缓冲区 所以,如果后面紧接着gets读入字符串会识别 \n 为一个完整字符串 错误示例: #include #include int main() { int n; char str2[10][50]; printf("请输入一个整数:\n"); scanf("%d",&n); /*这里用了scanf进行输入,缓冲区会有\n遗留*/ printf("请输入3个字符串:\n"); for(int i=0;i puts(str2[i]); } return 0; }
|
CopyRight 2018-2019 实验室设备网 版权所有 |