首页 > 其他分享 >getc()、getchar()、getch() 和 getche() 的区别

getc()、getchar()、getch() 和 getche() 的区别

时间:2023-10-16 15:55:25浏览次数:43  
标签:getch int getc getche include getchar

所有这些函数都从输入中读取一个字符并返回一个整数值。返回整数以容纳用于指示失败的特殊值。EOF值通常用于此目的。

getc()

    它从给定的输入流中读取单个字符,并在成功时返回相应的整数值(通常是读取字符的ASCII值)。失败时返回EOF。
    语法:

int getc(FILE *stream);
  • 1

    示例:

// Example for getc() in C
#include <stdio.h>
int main()
{
printf("%c", getc(stdin));
return(0);
}

 

    输出:

Input: g (press enter key)
Output: g 

 

getchar()

    getc() 和 getchar() 之间的区别是 getc() 可以从任何输入流读取,但是 getchar() 只能从标准输入流读取。因此 getchar() 等价于 getc(stdin)。
    语法:

int getchar(void);
  • 1

    示例:

// Example for getchar() in C
#include <stdio.h>
int main()
{
printf("%c", getchar());
return 0;
}

 

    输出:

Input: g(press enter key)
Output: g 
  • 1
  • 2
getch()

    getch() 是一个非标准函数,存在于 conio.h 头文件中,该文件主要由 MS-DOS 编译器(如 Turbo C)使用。它不是 C 标准库或 ISO C 的一部分,也不是由 POSIX 定义的。
    与上述函数一样,它也从键盘读取单个字符。 但它不使用任何缓冲区,因此输入的字符会立即返回,无需等待回车键。
    语法:

int getch();
  • 1

    示例:

// Example for getch() in C
#include <stdio.h>
#include <conio.h>
int main()
{
printf("%c", getch());
return 0;
}

 

 

 

    输出:

Input:  g (Without enter key)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        it shows a single g, i.e., 'g'
  • 1
  • 2
  • 3
  • 4
getche()

    与 getch() 一样,这也是conio.h中的一个非标准函数。它从键盘读取单个字符,并立即显示在输出屏幕上,而无需等待回车键。
    语法:

int getche(void);
  • 1

    示例:

#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
printf("%c", getche());
return 0;
}

    输出:

Input: g(without enter key as it is not buffered)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        double g, i.e., 'gg'

标签:getch,int,getc,getche,include,getchar
From: https://www.cnblogs.com/wenyutao1/p/17767514.html

相关文章

  • 使用 expo-location 获取不到位置信息 Location.getCurrentPositionAsync() 结果无打
    问题描述:reactnative中使用expo-location获取位置信息时,按照官方文档执行案例,获取不到位置结果。Location.requestForegroundPermissionsAsync()执行后可以获取授权状态,但是执行到Location.getCurrentPositionAsync()时不会打印结果。解决方法:给Location.getCurrentPos......
  • service.getClass().getAnnotation(XXXAnnotation.class) 取值为Null
    springboot2.7java8问题在使用工厂模式封装service时,需要通过service的class获取其类型注解,但是有些工厂类可以取到annotation注解,有些取不到渠道注解:/***xxx渠道注解**/@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@int......
  • 字符数组和字符串的输入:cin,,getchar,cin.get,cin.geiline
    1#include<iostream>2usingnamespacestd;3intmain()4{5//cin.get输入字符6////charc;7/*while((c=cin.get())!=EOF)8{9cout<<c;10}*/11/*while(cin.get(c))12{13......
  • getClass()方法----getName()方法
    publicclassTest{publicstaticvoidmain(String[]args){Personp=newPerson(1,"刘德华");System.out.println(p.getClass());System.out.println(p.getClass().getName());}}classPerson{intid;Stri......
  • 关于getClass().getClassLoader().getResourceAsStream——转载自https://www.cnblogs
    关于getClass().getClassLoader().getResourceAsStreamInputStreamis=getClass().getClassLoader().getResourceAsStream("helloworld.properties");getClass():取得当前对象所属的Class对象getClassLoader():取得该Class对象的类装载器类装载器负责从Java字符文件将字符流读......
  • getchar()与putchar()
    ()getchar()与putchar()getchar与putchar其实最基本的作用就是用getchar读取一个字符,再用putchar输出一个字符基本用法:#include<stdio.h>intmain(){ charc=0; c=getchar(); putchar(c); return0;}运行结果:输入一个a,然后再打印a这里getchar和putcha......
  • dotnet 理解 IConfigurationProvider 的 GetChildKeys 方法用途
    我最近遇到了一个有趣的Bug让我调试了半天,这个Bug的现象是我的好多个模块都因为读取不到配置信息而炸掉,开始我没有定位到具体的问题,以为是我的配置服务器挂掉了。经过了半天的调试,才找到了是我新加入的使用COIN配置库的ReadonlyCoinConfiguration类型导致的,此ReadonlyCoi......
  • Linux下非阻塞getch实现示例
    作者:fbysss关键字:getch问题1:标准c中没有getch函数,等待键盘输入之后,总是需要按下回车才能得到值,如何解决?问题2:在等待键盘输入的时候,程序被阻塞了,下面的代码必须在输入处理之后才能执行。如果不使用线程,如何实现非阻塞模式?解决办法:#include<curses.h>#include......
  • .super.getClass()方法调用?
    下面程序的输出结果是多少?importjava.util.Date;publicclassTestextendsDate{publicstaticvoidmain(String[]args){newTest().test();}publicvoidtest(){System.out.println(super.getClass().getName());}}答:结果是Test。原因:在test方法中,直接调用getC......
  • Java踩坑3.org.gradle.api.tasks.sourceset.getcompiloconfigurationname
    idea2018自带gradle版本4.1我改properties文件为7.5启动项目,sync时报错,如标题。解决步骤:1.我尝试更改jdk9和11都一样报错。2.我尝试改properties文件为6.4不报错。3.我仍然使用properties文件为7.5,不理会build窗口的这个错误。在右侧的gradle插件build,clean这些正常使用。......