c - 我在 xcode 中遇到一些 C 问题

标签 c xcode string itoa

<分区>

我正在使用 Microsoft Visual Studio 学习 C,我写了一些代码,效果很好。但是当我尝试使用 xcode 进行调试时,它不起作用。

this is my error in xcode

这是我将数字转换为罗马数字的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int dec,length,indice,irom,itab=0;
    char rom[4][5]={'\0'};
    char dconvert[10];
    char convert[2];
    char tab[7]={'I','V','X','L','C','D','M'};
    float dec2;

    puts("give a number");
    scanf("%d",&dec);

    itoa(dec,dconvert,10);
    length=strlen(dconvert);
    strrev(dconvert);

    for (indice=0; indice < length;indice ++)
    {   
        convert[0]=dconvert[indice];
        dec=atoi(convert);
        if (dec<=3)
        {
            for (irom=0; irom<dec;irom++)
                {
                    rom[indice][irom]=tab[itab];
                }
        }
        if (dec>3 && dec<9)
        {   
            irom=0;
            dec2=dec-5;
            if (dec2<0)
            {
                rom[indice][irom]=tab[itab];
                rom[indice][irom+1]=tab[itab+1];
            }
            else 
            {
                rom[indice][irom]=tab[itab+1];
                for (irom=1;irom<=dec2;irom++)
                {
                    rom[indice][irom]=tab[itab];
                }
            }
        }
        if (dec==9)
        {   
            irom=0;
            rom[indice][irom]=tab[itab];
            rom[indice][irom+1]=tab[itab+2];
        }


        itab=itab+2;
    }
    for (indice=length; indice>=0;indice--)
        {
            printf("%s",rom[indice]);
        }

}

最佳答案

如前所述,itoa 不是 C99 标准的一部分。相反,使用 sprintf(或 snprintf 以避免缓冲区溢出):

sprintf(target_string, "%d", int_value);

关于c - 我在 xcode 中遇到一些 C 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13897496/

相关文章:

c - TLB、CPUID 和 Hugepages?

c++ - C 或 C++ 返回状态

c++ - 使用 gdb 调试 C++

c++ - 字符串变量的简单错误

java - 这个生成了多少 Java 对象 - new String ("abcd")

c - 试图释放这个二维数组

swift - 嵌套类的名称不稳定。修复带有奇怪参数的@objc

objective-c - 保持 NSTableCellView 处于焦点

c++ - C++中迭代器值的垃圾值

c - 在 RTOS 中安排帧的传输