Hi guys I need to now that I can control the white spaces in the printf("%5d") defined by 5.
In my program I need to control it to implement or decrement. So instead of using %5 what can I use.
I need ideas, thanks for help!
Printable View
Hi guys I need to now that I can control the white spaces in the printf("%5d") defined by 5.
In my program I need to control it to implement or decrement. So instead of using %5 what can I use.
I need ideas, thanks for help!
You can create the format string on the fly:
Code:char format[100];
sprintf(format, "%%%dd", 5); // or snprintf if available
printf(format, some_int);
Eg.Quote:
Originally Posted by man page
Code:printf( "%*d\n", 5, some_int );
Is this C or C++? This is C code, yet is posted in the C++ section.
actually I am trying to control
sprintf(*form, "%s", "%5d");
in this line %5d must be controlled by a variable!
and it is c++ but we're not using its GUI
What are you doing? You have already been given code to accomplish exactly this.Quote:
in this line %5d must be controlled by a variable!
What GUI? If you are using C++, then you can also do this the C++ way.Quote:
and it is c++ but we're not using its GUI
Code:std::cout << std::setw(5) << i << std::endl;
sorry it is
sprintf(form, "%s", "%5d");
not *form
If you're using C++, why not use cout and iomanip's setw?
but this must be controlled by a variable "%5d" part must be controlled
and also I am obligated to write in C but in c++ file
So you should read the man page on printf/sprintf/etc, and note how to use a variable to control the precision.
I ve already read but nothing founr so I am here!
Then you need to read better.
Quote:
Originally Posted by man 3 printf