typedef char *va_list;

#define   _AUPBND        (sizeof (acpi_native_int) - 1)
#define   _ADNBND        (sizeof (acpi_native_int) - 1)

#define _bnd(X, bnd) (((sizeof (X)) + (bnd)) & (~(bnd)))
#define va_arg(ap, T) (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
#define va_end(ap)    (void) 0
#define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))

//start.c
];
int printf(char *fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vsprintf(sprint_buf, fmt, args);
va_end(args);
write(stdout, sprint_buf, n);
return n;
}

int vsprintf(char *buf, const char *fmt, va_list args)
{
  int len;
  unsigned long num;
  int i, base;
  char *str;
  char *s;

  int flags;            // Flags to number()

  int field_width;    // Width of output field
  int precision;    // Min. # of digits for integers; max number of chars for from string
  int qualifier;    // 'h', 'l', or 'L' for integer fields

  for (str = buf; *fmt; fmt++)
  {
    if (*fmt != '%')
    {
      *str++ = *fmt;
      continue;
    }

    // Process flags
    flags = ;
repeat:
    fmt++; // This also skips first '%'
    switch (*fmt)
    {
      case '-': flags |= LEFT; goto repeat;
      case '+': flags |= PLUS; goto repeat;
      case ' ': flags |= SPACE; goto repeat;
      case '#': flags |= SPECIAL; goto repeat;
      ': flags |= ZEROPAD; goto repeat;
    }

    // Get field width
    field_width = -;
    if (is_digit(*fmt))
      field_width = skip_atoi(&fmt);
    else if (*fmt == '*')
    {
      fmt++;
      field_width = va_arg(args, int);
      )
      {
    field_width = -field_width;
    flags |= LEFT;
      }
    }

    // Get the precision
    precision = -;
    if (*fmt == '.')
    {
      ++fmt;
      if (is_digit(*fmt))
        precision = skip_atoi(&fmt);
      else if (*fmt == '*')
      {
        ++fmt;
        precision = va_arg(args, int);
      }
      ) precision = ;
    }

    // Get the conversion qualifier
    qualifier = -;
    if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
    {
      qualifier = *fmt;
      fmt++;
    }

    // Default base
    ;

    switch (*fmt)
    {
      case 'c':
    ) *str++ = ' ';
    *str++ = (unsigned char) va_arg(args, int);
    ) *str++ = ' ';
    continue;

      case 's':
    s = va_arg(args, char *);
    if (!s)    s = "<NULL>";
    len = strnlen(s, precision);
    if (!(flags & LEFT)) while (len < field_width--) *str++ = ' ';
    ; i < len; ++i) *str++ = *s++;
    while (len < field_width--) *str++ = ' ';
    continue;

      case 'p':
    )
    {
      field_width =  * sizeof(void *);
      flags |= ZEROPAD;
    }
    str = number(str, (unsigned , field_width, precision, flags);
    continue;

      case 'n':
    if (qualifier == 'l')
    {
      long *ip = va_arg(args, long *);
      *ip = (str - buf);
    }
    else
    {
      int *ip = va_arg(args, int *);
      *ip = (str - buf);
    }
    continue;

      case 'A':
    flags |= LARGE;

      case 'a':
    if (qualifier == 'l')
      str = eaddr(str, va_arg(args, unsigned char *), field_width, precision, flags);
    else
      str = iaddr(str, va_arg(args, unsigned char *), field_width, precision, flags);
    continue;

      // Integer number formats - set up the flags and "break"
      case 'o':
    ;
    break;

      case 'X':
    flags |= LARGE;

      case 'x':
    ;
    break;

      case 'd':
      case 'i':
    flags |= SIGN;

      case 'u':
    break;

      case 'E':
      case 'G':
      case 'e':
      case 'f':
      case 'g':
        str = flt(str, va_arg(args, double), field_width, precision, *fmt, flags | SIGN);
    continue;

      default:
    if (*fmt != '%') *str++ = '%';
    if (*fmt)
      *str++ = *fmt;
    else
      --fmt;
    continue;
    }

    if (qualifier == 'l')
      num = va_arg(args, unsigned long);
    else if (qualifier == 'h')
    {
      if (flags & SIGN)
    num = va_arg(args, short);
      else
    num = va_arg(args, unsigned short);
    }
    else if (flags & SIGN)
      num = va_arg(args, int);
    else
      num = va_arg(args, unsigned int);

    str = number(str, num, base, field_width, precision, flags);
  }

  *str = '/0';
  return str - buf;
}

printf 函数原型的更多相关文章

  1. 考察printf函数返回值

    最近偶然间见了这样一道题:  #include<stdio.h> int main() { ; printf("%d\n",printf("%d", ...

  2. 【C语言】浅谈可变参数与printf函数

    一.何谓可变参数 int printf( const char* format, ...); 这是使用过C语言的人所再熟悉不过的printf函数原型,它的参数中就有固定参数format和可变参数(用& ...

  3. 实现简单的printf函数

    首先,要介绍一下printf实现的原理 printf函数原型如下: int printf(const char* format,...); 返回值是int,返回输出的字符个数. 例如: int mai ...

  4. printf 函数的实现原理

    /* * ===================================================================================== * * Filen ...

  5. s3c2440——实现裸机的简易printf函数

    在单片机开发中,我们借助于vsprintf函数,可以自己实现一个printf函数,但是,那是IDE帮我们做了一些事情. 刚开始在ARM9裸机上自己写printf的实现的时候,包含对应头文件也会提示vs ...

  6. C语言中可变参数的原理——printf()函数

    函数原型: int printf(const char *format[,argument]...) 返 回 值: 成功则返回实际输出的字符数,失败返回-1. 函数说明: 使用过C语言的人所再熟悉不过 ...

  7. 关于 printf() 函数的三张表格

    函数原型: printf(Control-String, item1, item2, ...); 表一  转换说明符及作为结果的打印输出 转 换 说 明 输    出 %a 浮点数.十六进制数字和p- ...

  8. C语言函数的声明以及函数原型

    C语言代码由上到下依次执行,原则上函数定义要出现在函数调用之前,否则就会报错.但在实际开发中,经常会在函数定义之前使用它们,这个时候就需要提前声明.所谓声明(Declaration),就是告诉编译器我 ...

  9. 关于printf函数的所思所想

    缘起大一下学期,C语言程序设计徐小青老师的随口一提,经娄嘉鹏老师提醒,我觉得应该自己整理清楚这一问题.涉及网上资料将会标明出处. 关于printf函数的所思所想 * printf的定义 printf( ...

随机推荐

  1. vm12 安装ubuntu15.10详细图文教程 虚拟机安装ubuntu安装 ubuntu更新软件 ubuntu一直卡在下载语言怎么办?

    1,准备工作-ubuntu下载 ubuntu官网下载 如何官网下载ubuntu http://www.ubuntu.com/download/ 2,打开虚拟机 虚拟机安装ubuntu15.10 虚拟机 ...

  2. 图解ARP协议(二)ARP攻击原理与实践

    一.ARP攻击概述 在上篇文章里,我给大家普及了ARP协议的基本原理,包括ARP请求应答.数据包结构以及协议分层标准,今天我们继续讨论大家最感兴趣的话题:ARP攻击原理是什么?通过ARP攻击可以做什么 ...

  3. shell编程练习(二): 笔试11-20

    笔试练习(二): 11.写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. [root@VM_0_5_centos test]# vi 11.sh [root@VM_0_5_cento ...

  4. 第48章 UserInfo端点(UserInfo Endpoint) - Identity Server 4 中文文档(v1.0.0)

    UserInfo端点可用于检索有关用户的身份信息(请参阅规范). 调用者需要发送代表用户的有效访问令牌.根据授予的范围,UserInfo端点将返回映射的声明(至少需要openid作用域). 示例 GE ...

  5. 服务器控件的几个属性 SelectedIndex、SelectedItem、SelectedValue、SelectedItem.Text、selectedItem.value

    转自http://blog.csdn.net/iqv520/article/details/4419186 1. SelectedIndex ——选项的索引,为int,从0开始,可读可写 2. Sel ...

  6. 【转】NotificationCopat.Builder全部设置

    1.方法:setContentTitle(CharSequence title)   功能:设置通知栏标题.   例子:setContentTitle("测试标题"). 2.方法: ...

  7. css隐藏滚动条

    xhtml中隐藏滚动条在用ie6浏览有框架的xhtml页面的时候,默认会水平和垂直滚动条会一起出现,这是ie6的一个bug,在firefox上是正常的,出现的原因是其对XHTML 1.0 transi ...

  8. 2019-01-28 [日常]Beyond的歌里最多是"唏嘘"吗? - Python分词+词频

    看了一个Beyond的纪录片, 提到这个. 觉得心有不甘, 于是搜集了24首歌词, 用Python做了简单分词和词频统计. 源码(包括歌词)在: program-in-chinese/study 统计 ...

  9. 使用vue-cli 初始化 vue 项目

    1. 安装nodejs 2. 安装 vue-cli npm install -g vue-cli 安装前可以通过设置代理为淘宝仓库地址,以加快下载速度. npm config set registry ...

  10. (简单)华为荣耀4A SCL-TL00的usb调试模式在哪里打开的方法

    就在我们使用PC通过数据线连接上安卓手机的时候,如果手机没有开启Usb调试模式,PC则没办法成功检测到我们的手机,有时候,我们使用的一些功能强大的App好比之前我们使用的一个App引号精灵,老版本就需 ...