getchar putchar字符输入输出函数;gets puts字符串输入输出函数。
getchar putchar
getchar:
函数原型:int getchar ( void );
函数功能:从键盘输入的信息中获取一个字符
函数参数:空
函数返回值:
"On success, the character read is returned (promoted to an int value).
The return type is int to accommodate for the special value EOF, which indicates failure:
If the standard input was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stdin.
If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead."
函数用法实例示例:
#include<stdio.h>
int main()
{
char c=getchar();
putchar(c);
return 0;
}
putchar:
函数原型:int putchar ( int character );
函数功能:输出一个字符,这个字符来自于调用的函数参数
函数参数:空
函数返回值:
"On success, the character written is returned.
If a writing error occurs, EOF is returned and the error indicator (ferror) is set."
函数用法实例示例:
#include <stdio.h>
int main ()
{
char c;
for (c = 'A' ; c <= 'Z' ; c++)
putchar (c);
return 0;
}
gets puts
gets
函数原型:char * gets ( char * str );
函数功能:从键盘输入的信息中获取一个字符串
函数参数:指向一个内存块(char数组)的指针,在这里读取的字符串被复制为C字符串。
函数返回值:
"On success, the function returns str.
If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed"
函数用法实例示例:
#include <stdio.h>
int main()
{
char string [256];
printf ("Insert your full address: ");
gets (string); // warning: unsafe (see fgets instead)
printf ("Your address is: %s\n",string);
return 0;
}
puts
函数原型:int puts ( const char * str );
函数功能:输出一串字符串
函数参数:要打印的c字符串。
函数返回值:
"On success, a non-negative value is returned.
On error, the function returns EOF and sets the error indicator (ferror)."
函数用法实例示例:
#include <stdio.h>
int main ()
{
char string [] = "Hello world!";
puts (string);
}
问题:
(getchar putchar与gets puts的关系)
有时先用了一个getchar 然后再用gets 发现 程序运行的时候,没有停下来让你输入;或者先用了一个getchar 然后再用getchar发现 程序运行的时候,没有停下来让你输入。
这是为什么呐?
首先了解一个关于计算机的相关知识:
就是当你输入信息的时候:有一个存储从键盘输入的信息的空间。
当这个空间为空什么都没有的时候,getchar/gets才会调用这块空间,停下来,给你时间让你输入;当这个空间里面有信息的时候,他就直接在这里面获取了,他不会停下来等你输入了。\n
So:看这么一个程序:
#include<stdio.h>
int main()
{
char c=getchar();//停下来让你输入,然后你可能输入了一个字符串,然后getchar取了一个字符
putchat(c);//这个是输出变量的一个字符
printf("\n");
char b[20];
gets(b);//他先会去那个空间里面检索一下有没有东西,有他就取走,没有他就停下让你输入;
puts(b);//他会输出字符串
printf("\n");
putchar(b);
printf("\n");
return 0;
}