Setting width in printf with a #define macro?

This is a discussion on Setting width in printf with a #define macro? within the C Programming forums, part of the General Programming Boards category; Hi, So in my program I'm printing some strings, and I have the maximum length the string can have in ...

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    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
    Posts
    311
    How bout this

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

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    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, 03: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, 08: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21