鱼C论坛

 找回密码
 立即注册
查看: 1437|回复: 4

[已解决]可变参数列表返回单个字符出现以下报错

[复制链接]
发表于 2021-9-26 18:09:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdarg.h>
  4. #include <math.h>

  5. int myprintf(const char* format,...)
  6. {
  7.         int i=0,temp,example,example1,j=0,zifushu=0,chushu1,chushu2;
  8.         char a;
  9.         char s;
  10.         va_list vap;
  11.         va_start(vap,format);
  12.        
  13.         while(1)
  14.         {
  15.                 if(*(format+i)=='\0')
  16.                 break;
  17.                 if(*(format+i)=='%')
  18.                 {
  19.             while(1)
  20.         {
  21.                         if(*(format+i+1)=='d')
  22.                         {
  23.                                 temp=va_arg(vap,int);
  24.                                 example=temp;
  25.                                 while(1)
  26.                                 {
  27.                                         if(temp==0)
  28.                                         {
  29.                                                 j=1;
  30.                                                 break;
  31.                                         }
  32.                                         if(example==0)
  33.                                         break;
  34.                                         j++;
  35.                                         example=example/10;
  36.                                 }
  37.                                 example=temp;
  38.                                 if(temp>=0)
  39.                                 {
  40.                                         while(1)
  41.                                   {
  42.                                         if(j==0)
  43.                                         break;
  44.                                         chushu1=pow(10,j);
  45.                                         chushu2=pow(10,j-1);
  46.                                         example1=example%chushu1/chushu2;
  47.                                         a=example1+48;
  48.                                         putchar(a);
  49.                                         j--;
  50.                                         zifushu++;
  51.                                   }
  52.                                 }
  53.                                 else
  54.                                 {
  55.                                         putchar('-');
  56.                                         while(1)
  57.                                   {
  58.                                         if(j==0)
  59.                                         break;
  60.                                         chushu1=pow(10,j);
  61.                                         chushu2=pow(10,j-1);
  62.                                         example1=example%chushu1/chushu2;
  63.                                         a=48-example1;
  64.                                         putchar(a);
  65.                                         j--;
  66.                                         zifushu++;
  67.                                   }
  68.                                 }
  69.                         }
  70.                     else if(*(format+i+1)=='c')
  71.                         {
  72.                                 s=va_arg(vap,char);
  73.                                 putchar(s);
  74.                         }
  75.                         i=i+2;
  76.                         if(*(format+i)!='%')
  77.                         break;
  78.         }
  79. }
  80.                 putchar(*(format+i));
  81.                 i++;
  82.                 zifushu++;
  83. }
  84.         return zifushu;
  85. }
  86.                        
  87. int main()
  88. {
  89.         myprintf("%d%c",-1234,'s');
  90. }
复制代码





1        0        E:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\stdarg.h        In file included from E:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/stdarg.h
3                C:\Users\DRD\Desktop\c\未命名51.cpp                         from C:\Users\DRD\Desktop\c\未命名51.cpp
C:\Users\DRD\Desktop\c\未命名51.cpp        In function 'int myprintf(const char*, ...)':
73        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Warning] 'char' is promoted to 'int' when passed through '...'
73        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Note] (so you should pass 'int' not 'char' to 'va_arg')
73        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Note] if this code is reached, the program will abort
最佳答案
2021-9-27 10:32:06
看这里:https://blog.csdn.net/llljjlj/article/details/84985295
73行 s=va_arg(vap,char);改成 char s=va_arg(vap,int);
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-26 19:54:39 | 显示全部楼层
vs2012 没有错误,可以运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-26 20:26:21 From FishC Mobile | 显示全部楼层
jhq999 发表于 2021-9-26 19:54
vs2012 没有错误,可以运行

我是 dev c++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-27 10:13:39 | 显示全部楼层
3        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Warning] 'char' is promoted to 'int' when passed through '...'
73        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Note] (so you should pass 'int' not 'char' to 'va_arg')
73        18        C:\Users\DRD\Desktop\c\未命名51.cpp        [Note] if this code is reached, the program will abort
一个警告两个注意,没有错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-27 10:32:06 | 显示全部楼层    本楼为最佳答案   
看这里:https://blog.csdn.net/llljjlj/article/details/84985295
73行 s=va_arg(vap,char);改成 char s=va_arg(vap,int);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表