itoa usage - difference in stdlib.h header?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello - first post here but I have learned so much from reading the forums here as I work on my project.  So thank you in advance!

 

My question is with regards to the function call of itoa, like this example pulled from online:

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    int a=54325;

    char buffer[20];

    itoa(a,buffer,2);   // here 2 means binary

    printf("Binary value = %s\n", buffer);

 

    itoa(a,buffer,10);   // here 10 means decimal

    printf("Decimal value = %s\n", buffer);

 

    itoa(a,buffer,16);   // here 16 means Hexadecimal

    printf("Hexadecimal value = %s\n", buffer);

    return 0;

}

 

the itoa takes 3 arguments, but when in a likewise fashion, i get an error that says :

 

Error: C:\cvavreval\BIN\LCDproject2.c(53): too many arguments in function call

 

Then I referred to the stdlib.h header file, and saw the following:

 

void itoa(int n,char *str);

 

with only two arguments.  In the case of only having the two arguments to work with, how can I specify if I want binary numbers or decimal numbers?

 

FYI ~ using avrstudio 4.19 and CodeVisionAVRv3.28

 

Thanks again,

Ben

Last Edited: Tue. Aug 16, 2016 - 12:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

itoa() is a common function that is supplied by most Compiler libraries since the beginning of C.

an example on page 60 of my 1st edition K&R

 

However,  itoa() has never been part of the "Standard C Library".

The Standard library expects you to use sprintf(buf, "%d", val) or similar.    And you can format it nicely too e.g "%04X"

 

Historically,  void itoa(int n,char *str); would be more common.

Since GCC has become widely used,  void itoa(int n,char *str, int radix); has become prevalent at least on AVR.

 

All that I can suggest is that realise itoa() may vary across Compilers.

 

Oh,  and the GCC function may appear attractive for Binary, Hexadecimal, etc.    But most of the time you want a fixed width field.

I can manage hex but looking at a 32-bit Binary is pretty unreadable to normal humans.

 

If you want to output 8 bits or 16 bits,  write your own function. 

 

David.

Last Edited: Tue. Aug 16, 2016 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As David says, itoa() is not standardized. So various compilers deliver their own interpretation of something like itoa(). It just so happens that in the AVR world GCC happens to have the 3 parameter variant (value, buffer, radix) while CodeVision chooses to supply a 2 parameter variant (value, buffer). There's nothing wrong with that. Because nothing says "itoa() MUST have this interface" it can be one or the other (or even something else again). It does mean, of course, that the CodeVision one only does decimal conversions. You either have to use sprintf() if you want hex or roll your own if you want binary or some other number base.

 

Also, as David says, while itoa() is a "quick and dirty" solution for the purposes of debug (what is in variable 'x') it's not really practical for the real world as you have so little control over the actual format - which is where printf() comes into its own.

 

And yes it is a shame that printf() doesn't have a format character like x/X for hex that means "binary"!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

benjymon wrote:
this example pulled from online

As you have found, itoa() will vary among toolchains.

 

itoa/ltoa/ftoa are not part of standardized C.  Toolchain implementors often add those functions to stdlib.h.

 

GCC has the three-parameter version.

 

benjymon wrote:
In the case of only having the two arguments to work with, how can I specify if I want binary numbers or decimal numbers?

As a long-time CV user I don't think I've ever had the urge (or need or desire) to do binary or octal.

 

For hexadecimal, I'll use (s)printf if I've used it for other purposes in the app.  But often, I use my own routine as I'll want the output to be right-justified in a fixed-width field, and leading-zero padded.  itoa() doesn't do that for you anyway.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh and your example in #1 makes no sense whatsoever. What is itoa() even doing there apart from the binary case? For the other two:

    itoa(a,buffer,10);   // here 10 means decimal
    printf("Decimal value = %s\n", buffer);

    itoa(a,buffer,16);   // here 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);

could just as easily be:

    printf("Decimal value = %d\n", a);

    printf("Hexadecimal value = %x\n", a);

Presumably the example is contrived to demonstrate the use of the ",2" radix?

 

Oh and the other day I was looking for a good way to show binary in a printf(). Google lead me to SatckOverflow here@

 

http://stackoverflow.com/questio...

 

I went with:

#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte)  \
  (byte & 0x80 ? '1' : '0'), \
  (byte & 0x40 ? '1' : '0'), \
  (byte & 0x20 ? '1' : '0'), \
  (byte & 0x10 ? '1' : '0'), \
  (byte & 0x08 ? '1' : '0'), \
  (byte & 0x04 ? '1' : '0'), \
  (byte & 0x02 ? '1' : '0'), \
  (byte & 0x01 ? '1' : '0') 
printf("Leading text "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(byte));

(regular readers will appreciate the use of the Conditional Operator in this! ;-)

Last Edited: Tue. Aug 16, 2016 - 01:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And just to fill out the field, ICC has itoa(*buff, int value, base) similar to GCC but changes the order of the parameters!

 

Jim

an ICC user too!

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all! I got it!