用putchar在屏幕上打印字符

printing character on screen with putchar

本文关键字:打印 字符 屏幕 putchar      更新时间:2023-10-16
void main ()
{
char ch='A';
while (ch <='F'){
switch (ch){
case 'A':
case 'B':
case 'C':
case 'D': ch++; continue;
case 'E';
case 'F': ch++;
}
putchar (ch);
}

我的问题是,为什么程序的输出仅是FG,为什么它不打印字母E(EFG)。而且,为什么putchar打印两个字符的定义是在屏幕上只有一个字符时,为什么要打印两个字符。

在案例E方面,因为没有休息;在该语句之后,它将在Switch语句中执行所有情况,从而执行CH ;案件f e 是f,所以putchar将打印f接下来,因为您的条件在while语句中较小或等于f,它将再次输入开关 如果f它将再次执行CH 现在CH拥有G和Putchar Prints G的值GG是不少或相等的F,它将退出时循环并结束。因此,您已经打印了F。

为什么不打印e的问题,请查看我在此处对您的代码的评论:

void main ()
{
char ch='A';
while (ch <='F'){
switch (ch){
case 'A': // ch: A=>B; continue;
case 'B': // ch: B=>C; continue;
case 'C': // ch: C=>D; continue;
case 'D': ch++; continue; // ch: D=>E; continue; | E isn't printed here "continue" is called instead.
case 'E'; // ch: E=>F; putchar(F); | E isn't printed here either F is printed because F is assigned just before "putchar" is called.
case 'F': ch++; // ch: F=>G; putchar(G);
}
putchar (ch);
}

第二个问题,为什么PutChar打印两个字符:不是。它每次仅将一个字符写入输出缓冲区,但输出缓冲区写在晚期呼叫的屏幕上。

putchar后使用std::cout.flush()立即显示您的输出。

还将case 'E';更改为case 'E':以使您的代码编译,请。