【问题标题】:i have some problems with C in xcode [closed]我在xcode中遇到了一些C问题[关闭]
【发布时间】:2012-12-16 00:32:09
【问题描述】:

我正在使用 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]);
        }

}

【问题讨论】:

标签: c xcode string itoa


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以使用:

    snprintf(str, sizeof(str), "%d", num);
    

    避免缓冲区溢出(当您转换的数字不适合您的字符串大小时)。

    【讨论】:

      【解决方案3】:

      itoa 和 strrev 都不是标准的。

      其他 StackOverflow 问题:

      Where is the itoa function in Linux?

      Is the strrev() function not available in Linux?

      解决这两个问题可能会解决中间一个问题,但如果没有,请阅读http://forums.bignerdranch.com/viewtopic.php?f=77&t=1743

      【讨论】:

        猜你喜欢
        • 2012-05-13
        • 2023-03-27
        • 2020-04-29
        • 1970-01-01
        • 2022-11-23
        • 2022-08-19
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多