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

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配