Pages: [1] 2   Go Down
Author Topic: atoi() function where does it come from ?  (Read 20410 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

~Tom~

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2925
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Doc
Logged

--> 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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

~Tom~

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

~Tom~

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 05, 2012, 07:44:37 pm by MichaelMeissner » Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
}
Compiles to 446 Bytes on an Uno


Code:
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
  sprintf(hi,"%s",lo);
}
Compiled to 554 Bytes on an Uno.
Logged

~Tom~

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {}

void loop() {
  char hi[10];
  char lo[] = "Hello";
}
Compiles to 446 Bytes on an Uno


Code:
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:
sprintf (hi, "%s", lo)

into

Code:
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.
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see what you mean, changing the last bit to this causes it to jump to 2kB
Code:
  char num = 2;
  sprintf(hi,"%s%d",lo,num);
Logged

~Tom~

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1] 2   Go Up
Jump to: