Pratik K. Shah
Pratik K. Shah

Reputation: 457

C code int to string not working

Here is my code which uses itoa() function , seems not working. Let me make it clear, I am working on C.

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

void main()
{       
    int i,j;
    for(i = 0;i<= 4; i++)
    {
        for (j = 0; j <= 9; j++)
        {
            //printf("Hi\n");
            char fileName[10]="A";
            char append[2];

            itoa(i,append,10);
            strcat(fileName,append);    

            itoa(j,append,10);
            strcat(fileName,append);

            printf("i=%d j=%d\n", i,j);
            printf("%s\n", fileName);
            //FMS()             
        }
        //printf("Anuj=%d\n",i );
    }
}

Output

RC4Attack.c:(.text+0x5e): undefined reference to itoa' RC4Attack.c:(.text+0x8e): undefined reference toitoa' collect2: error: ld returned 1 exit status

Upvotes: 1

Views: 743

Answers (3)

evenro
evenro

Reputation: 2646

I had a similar issue two weeks ago,
I solved it by using sprtinf instead of itoa.

http://www.cplusplus.com/reference/cstdio/sprintf/


Did it look like this?

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

void main()
{  
int i,j;
for(i = 0;i<= 4; i++)
{
    for (j = 0; j <= 9; j++)
    {
        //printf("Hi\n");
        char fileName[10]="A";
        char append[2];

        sprintf(filename,"%s%d",fileName,i);    
        sprintf(filename,"%s%d",fileName,j);    

        printf("i=%d j=%d\n", i,j);
        printf("%s\n", fileName);
    }
}
}

Upvotes: 0

Schuiram
Schuiram

Reputation: 301

I can offer you 2 solutions:

1.) You could try to use a different c-compiler, some may work

2.) You could write this function easily yourself. Its only a small piece of code (see the following link) http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdlib.h/itoa

Also you could use a former thread about this topic which may help in your case: Where is the itoa function in Linux?

Greetings Marius

Upvotes: 0

avmohan
avmohan

Reputation: 2000

There's no itoa in standard C library. Instead, use sprintf.

sprintf(string_value, "%d", integer_value);

EDIT Use snprintf to guard against buffer overflow as well.

snprintf(string_value, max_size, "%d", integer_value);

Upvotes: 1

Related Questions