C语言程序设计:简易版的printf函数实现

2024-04-22 14:28

本文主要是介绍C语言程序设计:简易版的printf函数实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简易版的printf函数实现

功能说明

(1)使用putchar函数、va可变参完成printf函数基本功能的实现;
(2)函数说明:

实现对下列数据类型的输出,并返回成功输出打印的字符个数:

  • 整数(正负)的输出;
  • 字符串输出;
  • 浮点数(float、double)输出;
  • 字符输出;

函数实现

#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <inttypes.h>int myprintf(const char *format, ...) {va_list args;va_start(args, format);int char_count = 0; // 记录写入的字符数量while (*format) {if (*format == '%') {format++;switch (*format) {case 'c': {char c = va_arg(args, int);putchar(c);char_count++;break;}case 'd': {int d = va_arg(args, int);if (d == INT32_MIN) {putchar('-');putchar('2');d = 147483648; char_count += 2;} else if (d < 0) {putchar('-');d = -d;char_count++;}int divisor = 1;while (d / divisor >= 10)divisor *= 10;while (divisor > 0) {putchar(d / divisor + '0');d %= divisor;divisor /= 10;char_count++;}break;}case 's': {char *s = va_arg(args, char *);while (*s) {putchar(*s);s++;char_count++;}break;}case 'l': {format++;switch (*format) {case 'l':format++;if (*format == 'd') {long long ll = va_arg(args, long long);if (ll == LLONG_MIN) {putchar('-');putchar('9');ll = 223372036854775808LL; char_count += 2;} else if (ll < 0) {putchar('-');ll = -ll;char_count++;}unsigned long long divisor = 1;while (ll / divisor >= 10)divisor *= 10;while (divisor > 0) {putchar(ll / divisor + '0');ll %= divisor;divisor /= 10;char_count++;}}break;case 'f': {double lf = va_arg(args, double);int int_part = (int)lf;int fract_part = (int)((lf - int_part) * 1000000); if (lf < 0) {putchar('-');lf = -lf;char_count++;}int divisor = 1;while (int_part / divisor >= 10)divisor *= 10;while (divisor > 0) {putchar(int_part / divisor + '0');int_part %= divisor;divisor /= 10;char_count++;}putchar('.');char_count++;divisor = 100000;while (fract_part / divisor == 0) {putchar('0');divisor /= 10;char_count++;}while (divisor > 0) {putchar(fract_part / divisor + '0');fract_part %= divisor;divisor /= 10;char_count++;}break;}}break;}default:putchar(*format);char_count++;}} else {putchar(*format);char_count++;}format++;}va_end(args);return char_count;
}

测试代码

void test_myprintf_v1() {long long big_number = 12345678901234LL;double pi = 3.141592653589793;myprintf("hello word !\n"); myprintf("LONG LONG: %lld\n", big_number);myprintf("FLOAT: %lf\n", pi);myprintf("INT32_MIN: %d\n", INT32_MIN);myprintf("INT32_MAX: %d\n", INT32_MAX);myprintf("LLONG_MIN: %lld\n", LLONG_MIN);myprintf("%c\n", 'A');return ;
}void test_myprintf_v2() {int ret;//打印字符常量printf("System:\n");ret = printf("hello word\n");printf("成功打印:%d\n", ret);printf("MySelf:\n");ret = myprintf("hello word\n");printf("成功打印:%d\n", ret);putchar(10);  //打印整型变量 int a = INT32_MIN, b = INT32_MAX;printf("System:\n");ret = printf("a = %d, b = %d\n", a, b);printf("成功打印:%d\n", ret);printf("MySelf:\n");ret = myprintf("a = %d, b = %d\n", a, b);printf("成功打印:%d\n", ret);putchar(10);//打印字符char c1 = 'a', c2 ='3';printf("System:\n");ret = printf("c1 = %c, c2 = %c\n", c1, c2);printf("成功打印:%d\n", ret);printf("MySelf:\n");ret = myprintf("c1 = %c, c2 = %c\n", c1, c2);printf("成功打印:%d\n", ret);putchar(10);//打印字符串char s1[150] = "adasdc asd as a", s2[150] ="sdadas asda sdaas";printf("System:\n");ret = printf("s1 = %s, s2 = %s\n", s1, s2);printf("成功打印:%d\n", ret);printf("MySelf:\n");ret = myprintf("s1 = %s, s2 = %s\n", s1, s2);printf("成功打印:%d\n", ret);return ;
}int main() {test_myprintf_v2();return 0;
}

运行效果

在这里插入图片描述

这篇关于C语言程序设计:简易版的printf函数实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/926023

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q