Thread: sprintf values

  1. #1
    Registered User
    Join Date
    Aug 2003

    sprintf values


    stuck on sprintf again....

    #define BUFFER_SIZE 2048
    void set_output_field(RecordOut * output, char * text, int field_number)
    char buffer[BUFFER_SIZE];
          case CIRCUIT_LENGTH:
    printf("CIRCUIT_LENGTH [%s]\n", text);
    printf("CIRCUIT_LENGTH [%f]\n", atof(text));
             sprintf(buffer, "%019.3f", (int)(sizeof(output->circuit_length) - 1), atof(text));
    printf("CIRCUIT_LENGTH [%s]\n", buffer);

    /* OUTPUT */
    CIRCUIT_LENGTH [16.72]
    CIRCUIT_LENGTH [16.720000]
    CIRCUIT_LENGTH [000000000000000.000]

    the only undescribed item is circuit_length which is part of a structure

    #define  O_CIRCUIT_LENGTH_LEN  19
    typedef struct {
        char country  [O_COUNTRY_LEN];
        char circuit_length  [O_CIRCUIT_LENGTH_LEN];

    the quantity of characters output is fine, but I am blanking out the value somewhere,


  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    >> sprintf(buffer, "%019.3f", (int)(sizeof(output->circuit_length) - 1), atof(text));

    Why do you have atof(text) in the argument list?
    sprintf( buffer, "%019.3f", (int)(sizeof(output->circuit_length) - 1) );

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    sprintf(buffer, "%019.3f", (int)(sizeof(output->circuit_length) - 1), atof(text));
    Why do you have two arguments for your statement here? Note the "%f", followed by an integer and a float. You only have one format specifier, but you have two following arguments.

    I'm surprised you're not getting a warning about that. Anyway, the integer fills the %f requirement, and the second is ignored.

    Ah, beat me to the punch. Anyway, you want one or the other. If you want an integer, don't use %f, use %d, or if you want a floating point number, typecast it to a float or double instead of an integer.

    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well the number of args you pass to sprintf mis-match the number of conversions.
    If you're looking for a programmable fixed width, then perhaps

    sprintf(buffer, "%*.3f", (int)(sizeof(output->circuit_length) - 1), atof(text));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2003
    oh dear,

    I think I'll go back to cabbing

    text is the input value that I want to change, I put (int)(sizeof(output->circuit_length) to ensure that the right amount of space was being used, not realising that 19.3 would do that for me


    sprintf(buffer, "%019.3f", atof(text));
    is now fine

    CIRCUIT_LENGTH [16.72]
    CIRCUIT_LENGTH [16.720000]
    CIRCUIT_LENGTH [000000000000016.720]

    thank you all,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM