home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!caen!rphroy!einstein!wenner
- From: wenner@einstein.eds.com (Rich Wenner)
- Newsgroups: comp.lang.c
- Subject: Re: printf question
- Message-ID: <1226@pascal.einstein.eds.com>
- Date: 28 Aug 92 18:09:32 GMT
- References: <1992Aug27.143434.26965@dartvax.dartmouth.edu> <kimcm.714943581@login.dkuug.dk>
- Organization: Electronic Data Systems
- Lines: 43
-
- In article <kimcm.714943581@login.dkuug.dk> kimcm@login.dkuug.dk (Kim Chr. Madsen) writes:
- >eht@coos.dartmouth.edu (Edward H. Truex) writes:
- >
- >>I am trying to format a hexadecimal number whose width
- >>is always a constant - like
- >
- >> printf("%04x\n", address );
- >
- >try to use the variable width specification method:
- >
- > printf("%0*x\n",4, adress);
- >
- >an asteriks in the width specification is taken as a variable that
- >must be taken from the arguments and be evaluated at runtime...
-
- The above will work just fine, but if you have a full ANSI compiler, the
- syntax ...
-
- #define HDPI "4" /* Hex Digits Per Int */
-
- printf( "%0" HDPI "x\n", address );
-
- ... will also work, provided that your constant is defined as a character
- string literal. The ANSI preprocessor will see character string literals
- with no intervening symbols other than whitespace, and will concatenate
- them into a single string. This is the same trick you can use for ...
-
- fputs( "This is a really long message to write out to a file, but "
- "now, thanks to ANSI, I don't have to do that hokey crap "
- "of putting backslashes in awkward places or doing a bunch "
- "of strcats.", file_ptr );
-
- And if you have read this far, you surely won't mind one more irrelevant
- remark ;-) Using this syntax on the original problem will save you some
- time because the format is evaluated at compile time instead of run time.
- Depending on your hardware platform, this may actually amount to several
- nanoseconds!
-
- --
- Rich Wenner | It is by the goodness of God that in our country we
- wenner@pascal.tsd.eds.com | have those 3 unspeakably precious things: freedom
- | of speech, freedom of conscience, and the prudence
- #include <stddisclaimer.h>| never to practice either of them. Mark Twain
-