Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-26-2009, 08:59 AM   #1
LQ Newbie
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
C - Constants in printf


I'm trying to set a minimum number of fields to write an integer variable in C.
I know it can be done this way:
"printf(" %05d\n",13)"

I wonder if there is a way for me to do the same thing using a constant?
If I declare a constant (" #define CONST 5"), can I use it in my printf in order to generate the same result?
Old 11-26-2009, 09:34 AM   #2
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,371

Rep: Reputation: 192Reputation: 192
use the *

#include <stdio.h>
#define WIDTH 5

int main (int argc, char ** argv) {

    printf("%0*d\n", WIDTH, 3);
    return 0;
can be a run time int also

Last edited by bigearsbilly; 11-26-2009 at 09:45 AM.
Old 11-26-2009, 10:14 AM   #3
LQ Newbie
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you very much. It worked really well.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie C Question on character constants. paulclark Programming 1 10-20-2009 08:21 PM
c: define constants kpachopoulos Programming 5 08-19-2006 04:31 PM
LXer: Variables Won't Constants Aren't: LXer Syndicated Linux News 0 07-26-2006 05:33 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 02:08 PM
How do you write binary literal constants in C? vasudevadas Programming 5 01-18-2004 08:39 AM

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration