The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[C,Linux/gcc] My strings manipulations seem crude
Discuss [C,Linux/gcc] My strings manipulations seem crude in the C Programming forum on Dev Shed. [C,Linux/gcc] My strings manipulations seem crude C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 25th, 2012, 11:17 PM
|
|
Registered User
|
|
Join Date: May 2012
Location: NJ, USA
Posts: 6
Time spent in forums: 1 h 34 m 10 sec
Reputation 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] = "";
sprintf( strPr, "%%4.0%sf", fmt_spec);
strcat( fmtPr, strPr);
strcat( fmtPr, "\t");
printf( fmtPr, t);
*fmtPr = '\0';
strcat( fmtPr, strPr);
strcat( fmtPr, "\n");
fprintf( t_steps, fmtPr, t);
Thanks for your feedback.
|

October 25th, 2012, 11:44 PM
|
 |
Contributed User
|
|
|
|
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);
|

October 26th, 2012, 12:38 AM
|
|
Registered User
|
|
Join Date: May 2012
Location: NJ, USA
Posts: 6
Time spent in forums: 1 h 34 m 10 sec
Reputation Power: 0
|
|
Quote: | 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.
|

October 26th, 2012, 06:47 AM
|
|
|
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);
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|