sprintf not switching data types

Discussion in 'Embedded Systems and Microcontrollers' started by Stuntman, Feb 23, 2012.

  1. Stuntman

    Thread Starter Active Member

    Mar 28, 2011
    181
    47
    Perhaps I am not familiar enough with how HiTech C handles sprintf, but i have an interesting problem.

    I have two data types that are converted on a PIC16F1934. One is int, the other is a long.

    In order to retain the fastest conversion possibly, I wanted to be able to call

    sprintf(buffer,%d,integer)

    when the integer type was used, then

    sprintf(buffer,%ld,long)

    when a long was needing to be converted. This was all done through an if statement.

    However, I found that the execution time was way too slow with int inputs. Knowing this shouldn't be correct, I commented out the call to sprintf(buffer,%ld,long) and whoila, execution time was cut down dramatically. So I tested my if statement and found that it IS operating correctly, it seems when the compiler sees sprintf, it automatically loads the routine with the highest precision for the rest of the program.

    Any ideas here?
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If I were doing this and had a situation where I needed to convert an integer to a string at maximum speed, I'd write the code to do it, keeping in mind anything I knew about the incoming data that would make it easier. Like, the normal range for an INT is +32767 to -32768, but does the data have less range?

    As for what the compiler does, maybe because the sprintf() routine takes so much space, it loads it into the code only once and does it at the maximum precision your program needs. Then it calls that code as a function, and you aren't meant to care that it's more than you need (and wasting time) if you aren't using its full capability.
     
  3. Stuntman

    Thread Starter Active Member

    Mar 28, 2011
    181
    47
    John P. - Unfortunately, this unit will be processing a large number of different data values. The reason I have had to go to long is to offer added precision for fixed point math (I compute to two additional decimal places, then drop them off the end after the sprintf). So it would just be nice to use %d for most conversion, yet have %ld for when I was concerned with added precision.

    The truth is, I can afford the extra delay in conversion, I'm just curious if this is standard protocol for mcu compilers, and if there was a workaround.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,393
    1,606
    Rule one: RTFM

    3.12.1 The printf Routine
    The code associated with the printf function is not found in the library files. The printf() function is generated from a special C template file that is customized after analysis of the user’s C code.
    ...
    The parser and code generator analyze the C source code, searching for calls to the printf function.
    ...
    As more features of printf() are detected, the size of the code generated for the printf() function will increase.



    sprinf() is similar, they both build off the same underlying routines.
     
  5. Stuntman

    Thread Starter Active Member

    Mar 28, 2011
    181
    47

    Yes, I read the manual, numerous times. However, that information still doesn't clarify to me that you the compiler will only allow this function to operate at it highest precision called.
     
Loading...