LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C - Constants in printf (https://www.linuxquestions.org/questions/programming-9/c-constants-in-printf-771709/)

marcojrfurtado 11-26-2009 07:59 AM

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?

bigearsbilly 11-26-2009 08:34 AM

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

marcojrfurtado 11-26-2009 09:14 AM

Thank you very much. It worked really well.


All times are GMT -5. The time now is 05:48 PM.