hello

I need to do something like this:
for ex. if the N is 12 then, i will produce strings like "001" "002" "003" "004" ... "012"
if the N is 6 then, I will produce strings like "01" "02" ... "06"
is it possible to do that with standart string functions in C ?

Recommended Answers

All 5 Replies

For example ... printf("%03d", 12); prints:

012


printf()

thanks very much. but actually, i need to do that dynamically. i mean, i can't know if it would be "%3d" at first...

Member Avatar for jencas

Use '*' instead of a fixed number and you can supply the length in the parameter list
see printf() -> "width"

thanks..that solved the problem:
int N=12;
char buf[10];
itoa(N,buf,10);
printf("%0*d",strlen(buf)+1,N);

This is the C++ forum. Are you sure you mean C functions and not C++?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.