嵌入式 打印在同一行printf行缓冲与退格使用技巧完成C语言倒计时

V2.00.000:

root@u12d32:/home/kongjun/work/hi_test/time_count_down# cat time_test.c 
#include<stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{ 
#if 1
	int bootdelay = 10;
	printf("Hit any key to stop autoboot: %2ds ", bootdelay);
	fflush(stdout);
	while(bootdelay > 0)
	{
		sleep(1);
		--bootdelay;
		printf("\b\b\b\b%2ds ", bootdelay);
		fflush(stdout);
	}
#endif
	printf("\n");
	int i;
	printf(".................\n");
	for(i = 5; i > 0; i--)
	{
		if(i == 5)
		{
			printf("Wait %ds",i);
			fflush(stdout);
			sleep(1);
		}
		else
		{
			printf("\b\b%ds",i);
			fflush(stdout);
			sleep(1);
		}
	}
	printf("\n");
	return 0;
}


运行效果图:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的编辑程序,使用 C 语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 1000 // 输入的最大长度 int main() { char input[MAX_LEN]; // 存储输入的字符串 char output[MAX_LEN]; // 存储输出的字符串 int outPos = 0; // 输出字符串的当前位置 int inPos = 0; // 输入字符串的当前位置 int len = 0; // 输入字符串的长度 printf("Enter the input string:\n"); fgets(input, MAX_LEN, stdin); // 从标准输入读取字符串 len = strlen(input); while (inPos < len) { if (input[inPos] == '\n') { break; // 遇到换符时退出循环 } if (input[inPos] == '\b') { if (outPos > 0) { outPos--; // 如果当前位置不是输出字符串的第一个位置,则删除一个字符 } } else { output[outPos] = input[inPos]; outPos++; } inPos++; } output[outPos] = '\0'; // 在输出字符串末尾添加结束符 printf("Output string:\n%s\n", output); return 0; } ``` 这个程序使用了两个字符数组,一个用于存储输入字符串,一个用于存储输出字符串。程序先从标准输入中读取字符串,然后逐个字符地处理输入字符串。如果当前字符是退符,则删除输出字符串中的一个字符;否则,将当前字符添加到输出字符串中。当遇到换符时,程序退出循环,并输出最终的输出字符串。 请注意,这个程序假定输入字符串不会超过 MAX_LEN 个字符。如果输入字符串超过了这个限制,可能会导致程序崩溃或为不可预测。为了处理这种情况,程序可以使用动态分配内存来存储输入和输出字符串。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值