Go Down

Topic: atoi() function where does it come from ? (Read 22954 times) previous topic - next topic

PGT

When looking at the reference there is no atoi() function see http://arduino.cc/en/Reference/HomePage
Still however i see people advice or use it here, where does this program verb belong too, is there a binary to be included for it ?
Or is the reference not up to date, if so can someone please update it.

Tom Carpenter

It isn't an Arduino function, its part of the C++ standard libraries (stdlib.h), which are included automatically in all sketches.

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
~Tom~

MichaelMeissner


When looking at the reference there is no atoi() function see http://arduino.cc/en/Reference/HomePage
Still however i see people advice or use it here, where does this program verb belong too, is there a binary to be included for it ?
Or is the reference not up to date, if so can someone please update it.

Atoi, atol, and atoll are all defined in the ISO C standard (7.20.1.2 for C99).  I would imagine that it is the C++ standard, but I don't happen to have the C++ standard right here.

Docedison

Tom Carpenter... Does that also include "sprintf"?

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Tom Carpenter

sprintf() is from a different C library (stdio.h), but it is included in Arduino yes.

The vast majority of functions from these four libraries are all included.

http://www.cplusplus.com/reference/clibrary/cstdlib/
http://www.cplusplus.com/reference/clibrary/cstring/
http://www.cplusplus.com/reference/clibrary/cstdio/
http://www.cplusplus.com/reference/clibrary/math/
http://www.cplusplus.com/reference/clibrary/cctype/
http://www.cplusplus.com/reference/clibrary/cstring/

For more info as to which are included, if you have windows, you can look at the header files which are all here:
<Arduino Directory>\hardware\tools\avr\avr\include
On Mac the are located at:
<right click arduino.app, show Package contents>/Contents/Resources/Java/hardware/tools/avr/avr/include/
~Tom~

PGT

Then shouldnt the arduino reference also include these commands ?
If some basic c / c++ commands can also be run inside the sketches
and currently most commands are included then those should be to named

Tom Carpenter

Arduino is C++, which means there is little point rewriting the C++ reference . The Arduino reference is there for more Arduino specific stuff.
It would be highly sensible if there was a link (or several) in the Arduino Reference to the C++ reference.
~Tom~

PGT

ok well it are not that many functions I see http://www.cplusplus.com/reference/clibrary/cstdlib/
for people who start with arduino (like me and many others), it might be handy to also include them in the reference
or at least mention a link to a page that does explain it, so people are aware it isnt the full command set.

PeterH

There is a link from the Arduino reference page to the AVR reference page where all the runtime libraries that are available are defined. Note that the AVR library implementations have some optional components and the Ardunio IDE does not include all options, for example printf etc do not include support for formatting floating point values.
I only provide help via the forum - please do not contact me for private consultancy.

MichaelMeissner

#9
Aug 05, 2012, 02:57 am Last Edit: Aug 06, 2012, 02:44 am by MichaelMeissner Reason: 1

Tom Carpenter... Does that also include "sprintf"?

Doc

In the past when I did compilers for other embedded processors, the problem with using any of the *printf functions is they pull in the whole floating point and long long arithmetic support functions.  On some of the ports, *printf would use 3/4 of the total memory for all of the support functions.  So, the functions are convenient to use, but they can be big.

Tom Carpenter


In the past when I did compilers for other embedded processors, the problem with using any of the *printf functions is they pull in the whole floating point and long long arithmetic support functions.  On some of the ports, *printf would use 3/4 of the totally memory for all of the support functions.  So, the functions are convenient to use, but they can be big.


I think in WinAVR, the version of sprintf() supplied doesn't support floating point nor long long arithmetic. For comparison,

Code: [Select]
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
}

Compiles to 446 Bytes on an Uno


Code: [Select]
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
  sprintf(hi,"%s",lo);
}

Compiled to 554 Bytes on an Uno.
~Tom~

MichaelMeissner



In the past when I did compilers for other embedded processors, the problem with using any of the *printf functions is they pull in the whole floating point and long long arithmetic support functions.  On some of the ports, *printf would use 3/4 of the totally memory for all of the support functions.  So, the functions are convenient to use, but they can be big.


I think in WinAVR, the version of sprintf() supplied doesn't support floating point nor long long arithmetic. For comparison,

Code: [Select]
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
}

Compiles to 446 Bytes on an Uno


Code: [Select]
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
  sprintf(hi,"%s",lo);
}

Compiled to 554 Bytes on an Uno.


That is because GCC optimizes:

Code: [Select]

sprintf (hi, "%s", lo)


into

Code: [Select]

strcpy (hi, lo);


If you had printed an integer for instance, it has to call the sprintf function.

Back in the day when I was supporting GCC on embedded platforms, one of the optimizations I kept wanting to do, but never got around to it, is recognizing if you only call *printf with int/char * arguments, to call an alternative function that doesn't support, long, long long, and floating point.  However, most of our customers did not use *printf and *scanf due to their memory requirements.

Tom Carpenter

I see what you mean, changing the last bit to this causes it to jump to 2kB
Code: [Select]

  char num = 2;
  sprintf(hi,"%s%d",lo,num);
~Tom~

Jack Christensen


Then shouldnt the arduino reference also include these commands ?
If some basic c / c++ commands can also be run inside the sketches
and currently most commands are included then those should be to named


These may help:
http://www.nongnu.org/avr-libc/
http://winavr.sourceforge.net/
http://savannah.nongnu.org/projects/avrdude

Arduino is built on top of this toolchain consisting of AVR-GCC, AVR-Libc, and AVRDUDE.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

WizenedEE


I think in WinAVR, the version of sprintf() supplied doesn't support floating point nor long long arithmetic. For comparison,


Actually that's not quite true. There is support for printing floating point numbers with vfprintf (the function that all the printf functions call) but it's not linked in by default. You have to change the linker options to get it.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1

Go Up