Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5084946
  • 博文数量: 553
  • 博客积分: 13864
  • 博客等级: 上将
  • 技术积分: 11041
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-28 21:25
个人简介

个人Blog: hhktony.com

文章分类

全部博文(553)

文章存档

2015年(1)

2014年(2)

2013年(12)

2012年(384)

2011年(154)

分类: LINUX

2011-12-25 20:02:52

在C语言中,能构获取字符串的函数至少有两个:
1.scanf()
  所在头文件:stdio.h
  语法:scanf("格式控制字符串",变量地址列表);
  接受字符串时:scanf("%s",字符数组名或指针);

2.gets()
  所在头文件:stdio.h
  语法:gets(字符数组名或指针);

两者在接受字符串时:
1.不同点:
  scanf不能接受空格、制表符Tab、回车等;
  而gets能够接受空格、制表符Tab和回车等;

2.相同点:
  字符串接受结束后自动加'\0'。
例1:
#include
main()
{
  char ch1[10],ch2[10];
  scanf("%s",ch1);
  gets(ch2);
}
依次键入asd空格fg回车,asd空格fg回车,则ch1="asd\0",ch2="asd fg\0"。

例2:
#include
main()
{
  char ch1[10],ch2[10],c1,c2;
  scanf("%s",ch1);
  c1=getchar();
  gets(ch2);
  c2=getchar();
}
3、fgets函数
从流中读一行或指定个字符,原型是char *fgets(char *s, int n, FILE *stream);
从流中读取n-1个字符,除非读完一行,参数s是来接收字符串,如果成功则返回s的指针,否则返回NULL。   
形参注释:*s结果数据的首地址;n-1:一次读入数据块的长度,其默认值为1k,即1024;stream文件指针

阅读(2757) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

GFree_Wind2011-12-26 12:27:18

gets很危险。。。不要用

Never use gets().  Because it is impossible to tell without knowing the data in advance how  many  characters
       gets()  will  read,  and  because  gets() will continue to store characters past the end of the buffer, it is
       extremely dangerous to use.  It has been used to break computer security.  Use fget