字符串输入输出

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

int mian01()

{

/*scanf和gets:由于scanf()和gets()无法知道字符串s大小,必须遇到换行符或读到文件结尾为止才接收输入,因此容易导致字符数组越界 (缓冲区溢出)的情况;scanf和gets都是不安全的*/

  char ch[10];

//scanf不能接收空格或空格以后的字符
  //scanf("%s", &ch);

//通过键盘获取一个字符串。需要导入头文件<stdio.h>;gets接收字符串带空格。
  //gets(ch);

//通过正则表达式,接收非 的所有内容,包括空格

  scanf("%[^ ]",ch)

  

  printf("%s ",ch);

  return 0;

}

int main02()

{

  char ch[10];

//"hello ";fgets可以接收空格;fgets获取字符串少于元素个数会有 ,大于等于没有 ;fgets是安全的

  fgets(ch,sizeof(ch),stdin);

  printf("%s ",ch);

  return 0;

}

原文地址:https://www.cnblogs.com/wanghong19991213/p/13515701.html