getchar putchar字符输入输出函数详解 gets puts字符串输入输出函数详解

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;
    
}

  • 34
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zorita.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值