Thread: Setting width in printf with a #define macro?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    74

    Setting width in printf with a #define macro?

    Hi,

    So in my program I'm printing some strings, and I have the maximum length the string can have in a #define, so that it can be easily changed.

    What I was trying to do, is having the width parameter of printf(that is printing said strings) also defined by the macro, but I can't see a way to do such.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Yes. Whether it's elegant is another matter:
    Code:
    #include <stdio.h>
    
    #define XSTR(x) #x
    #define STR(x) XSTR(x)
    
    #define LEN 15
    
    int main(void)
    {
      char x[LEN] = "hi";
    
      printf("%" STR(LEN) "s\n", x);
    
      return 0;
    }
    The STR macro will yield "15" (that is, it converts the integer to a string literal). Adjacent string literals are concatenated, so this is effectively: printf("%15s\n", x);

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char format[32];
    char x[LEN] = "hi";
    
    sprintf(format, "%%%ds", LEN);
    printf(format, x);
    Is also another way...

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    its working nicely.

    thanks!

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    How bout this

    Code:
    printf("%*s", str, MAX_STRING_LEN);
    EDIT: i forget the order of the arguments, but you get the idea.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by MacNilly View Post
    How bout this

    Code:
    printf("%*s", str, MAX_STRING_LEN);
    EDIT: i forget the order of the arguments, but you get the idea.
    Sure, if you want to use the logical, easy solution....

    It's funny to look at the complicated things you do, sometimes, to "solve" problems. Not too long ago I needed to skip over a portion of a file, so I created a dummy array and read into it. It didn't take me too long to realize that fseek() made a whole lot more sense.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    aha, didn't know you could especify the width like that. It sure is much easier.

    (btw, the order is reversed, it's 1st the MAX_STRING_LEN and then str)

    thanks again, now its even better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  4. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM