C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 25th, 2012, 11:17 PM
mathMajor mathMajor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: NJ, USA
Posts: 6 mathMajor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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] = "";

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

*
fmtPr '\0';
strcatfmtPrstrPr);
strcatfmtPr"\n");
fprintft_stepsfmtPrt); 

Thanks for your feedback.

Reply With Quote
  #2  
Old October 25th, 2012, 11:44 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old October 26th, 2012, 12:38 AM
mathMajor mathMajor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: NJ, USA
Posts: 6 mathMajor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 26th, 2012, 06:47 AM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 738 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 3 m
Reputation Power: 928
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);

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [C,Linux/gcc] My strings manipulations seem crude

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap