LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
C - Constants in printf


Hello,

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)"
stdout:
"00013"

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, 08:34 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
use the *

Code:
#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 08:45 AM.
 
Old 11-26-2009, 09:14 AM   #3
marcojrfurtado
LQ Newbie
 
Registered: Nov 2009
Posts: 3

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


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 04:07 PM.

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