"Indefinite reference to`itoa' " message when trying to use itoia function

Asked

Viewed 126 times

0

I found on some websites people talked about using the itoa function to convert an integer number into a string containing the number in binary format.

Code example that should do this:

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

int main(){

 int i = 5; // Valor a ser convertido
 char c[33]; // Onde vou guardar o valor convertido (use o tamanho 33)

 itoa(i, c, 2);  // converter na base 2, acredito que converta até base 32

 printf("%s\n", c);

 getchar();
}

My intention is to make tests to see the results of the conversions before using in my program but when trying to copy I get the message :

Mensagem que recebo ao tentar copilar o código.

Right away I thought that defines it with the Head file that contained the prototype of the function was missing, but according to the websites I consulted she is in stdlib. h.

I have tried to add some library, search error message among other things.

Like other examples like of that question also present a similar problem.

Can anyone explain to me why the problem occurs and how to solve ? Thanks in advance.

  • 1

    Taken from the answer you yourself linked: "itoa() is Indeed non-standard, as mentioned by several helpful commenters, it is best to use sprintf(target_string,"%d",source_int)" that is, is not part of the specification, so it is not any version of the compiler that will have. - would have some reason not to use the snprintf in place? Here’s a sort of "homemade" example to print in binary: https://stackoverflow.com/a/3208376/916193 - The ideal in your case would be to do a proper "binaryprint()" (or qq type) function, if you want portability. Any way it’s gonna be short.

  • No, I didn’t know the function snprintf, I’m looking now if you answer me.

  • 1

    I can tell you that there will be no native binary, search in "binary to decimal" right here on the site, you will find some example functions. Basically just go splitting the number by two and storing in the final string as bit[largurafinal - posicao] = '0' + (valor & 1)

  • I managed to create a better and more suitable solution for my problem. I appreciate the help.

  • If applicable to what you asked, you can post below as a reply.

  • It’s not, it’s a little out of context from what I asked here. I needed it to solve a bigger problem. Abstracting the idea a little bit was testing all possible possibilities for an array of N items. So I would take an integer that went from 0 to N, and at each iteration I would turn that number into binary to access one of the n possible combination of my data structure.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.