#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    NJ, USA
    Posts
    6
    Rep Power
    0

    [C,Linux/gcc] My strings manipulations seem crude


    Is there a better way in C for me to accomplish the following string manipulations? It seems slightly convoluted.

    Is there a way to use strings which might vary in length or am I forced to declare the size (as in strPr and fmtPr, below) ahead of time?
    PHP Code:
    #if defined(SUNDIALS_EXTENDED_PRECISION)
      
    char *fmt_spec "L";
    #elif defined(SUNDIALS_DOUBLE_PRECISION)
      
    char fmt_spec 'l';
    #else
      
    char fmt_spec '';
    #endif

    char strPr[10] = "";
    char fmtPr[10] = "";

    sprintfstrPr"%%4.0%sf"fmt_spec);
    strcatfmtPrstrPr);
    strcatfmtPr"\t");
    printffmtPrt);

    *
    fmtPr '\0';
    strcatfmtPrstrPr);
    strcatfmtPr"\n");
    fprintft_stepsfmtPrt); 
    Thanks for your feedback.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    I think I would go with
    Code:
    #if defined(SUNDIALS_EXTENDED_PRECISION)
      char *fmt_spec = "%4.0Lf";
    #elif defined(SUNDIALS_DOUBLE_PRECISION)
      char *fmt_spec = "%4.0lf";
    #else
      char *fmt_spec = "%4.0f";
    #endif
    
    printf( fmtPr, t); 
    fputc('\t',stdout);
    
    fprintf( t_steps, fmtPr, t);
    fputc('\n',t_steps);
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    NJ, USA
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    I think I would go with
    Code:
    #if defined(SUNDIALS_EXTENDED_PRECISION)
      char *fmt_spec = "%4.0Lf";
    #elif defined(SUNDIALS_DOUBLE_PRECISION)
      char *fmt_spec = "%4.0lf";
    #else
      char *fmt_spec = "%4.0f";
    #endif
    
    printf( fmtPr, t); 
    fputc('\t',stdout);
    
    fprintf( t_steps, fmtPr, t);
    fputc('\n',t_steps);
    Well, I don't want more than just the format specifier in the compiler code (by which I mean the #if..#endif) so I can use it with other lines to be printed. But I can definitely use the fputc to make the code better. I can just define the string once and I won't have to keep zeroing them out.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    These features may be of use to you:

    Constant strings can be concatenated at compile-time.
    Printf can take precision values from the argument list.

    Code:
    #include <stdio.h>
    #define SHORT_FORMAT
    
    #ifdef LONG_FORMAT
        #define MAKE_FORMAT(prec)  "%" prec "Lf"
    #else
        #define MAKE_FORMAT(prec)  "%" prec "lf"
    #endif
    
    #ifdef LONG_FORMAT
        #define PREC_FORMAT  "%*.*Lf"
    #else
        #define PREC_FORMAT  "%*.*lf"
    #endif
    
    int main() {
        float value = 1234.5678;
    
        printf( "The value is: " MAKE_FORMAT("4.0") "\n", value );
        printf( "The value is: " PREC_FORMAT "\n", 4, 0, value );
    
        return 0;
    }
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo