c语言conio.h和stdio.h函数

本文详细解析了控制台输入输出库conio的功能,对比getch()与getchar()的区别,包括按键响应特性及如何在程序中应用。同时介绍了sprintf函数用于格式化字符串输出的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数等等。 

来源百度百科 https://baike.baidu.com/item/conio.h/2912801?fr=aladdin

getch()与getchar()的主要区别是

getch()不用回车即可获得字符enter

getchar()需要按下enter才会获取

getch()一次只能读取一个字符

键盘的上下左右都是两个字符

ascii码表对应的上下左右

上72

下80

左75

右77

函数名: getch
功  能: 从控制台无回显地取一个字符
用  法: int getch(void);
程序例:

#include <stdio.h>
#include <conio.h>

int main(void)
{
   char ch;

   printf("Input a character:");
   ch = getche();
   printf("\nYou input a '%c'\n", ch);
   return 0;
}

函数名: getchar
功  能: 从stdin流中读字符
用  法: int getchar(void);
程序例:

#include <stdio.h>

int main(void)
{
   int c;

   /* Note that getchar reads from stdin and
      is line buffered; this means it will
      not return until you press ENTER. */

   while ((c = getchar()) != '\n')
      printf("%c", c);

   return 0;
}

sprintf

函数名: sprintf
功  能: 送格式化输出到字符串中
用  法: int sprintf(char *string, char *farmat [,argument,...]);
程序例:

#include <stdio.h>
#include <math.h>

int main(void)
{
   char buffer[80];

   sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
   puts(buffer);
   return 0;
}
/*将整形变量转化为字符串格式*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值