473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf padding with alternate character?

pb
Im wanted to pad out blank spaces with a specific character instead of
spaces or zeros, does C support that?

printf("$%*d", '*', 5); // Not sure what the format string is supposed to
look like to do this

example output i would want is this:
$********5
Nov 14 '05 #1
13 22585
"pb" <gl*****@yahoo. com> writes:
Im wanted to pad out blank spaces with a specific character instead of
spaces or zeros, does C support that?


No. You will have to write your own code to do it.
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 14 '05 #2
In article <1103068973.180 47@sj-nntpcache-3> pb <gl*****@yahoo. com> wrote:
Im wanted to pad out blank spaces with a specific character instead of
spaces or zeros, does C support that?
No.
printf("$%*d ", '*', 5); // Not sure what the format string is supposed to
look like to do this
(Note that //-comments wrap around, so that if this had been intended
to be a code example, it would not have worked so well.

The "*" in "%*d" is a field-width specifier that reads an "int"
argument from the argument list, so:

printf("%*d", 2, 5);

prints the value "5" in a ten-character field. The field is blank
or zero padded depending on the pad option selected: blank by
default, zero if you use a 0 modifier.)
example output i would want is this:
$********5


There is no standard way to do this. It is easy to build your own
though: just sprintf() the numeric value, and then work with the
string. In this case, to get an integer printed into a ten digit
field and replace leading blanks or zeros with spaces, just do
something like:

char buf[SOME_SIZE]; /* must be at least 11 chars */
int val;
...
sprintf(buf, "%010d", val); /* produces, e.g., 0000000005 */
subst(buf, '0', '*');

where the subst() function reads:

/*
* Do substitutions on leading characters in the given string:
* Replace all occurrences of "from" with "to". (We assume
* from != '\0'.)
*/
void subst(char *s, char from, char to) {
while (*s == from)
*s++ = to;
}

Note that if you print with leading blanks, you will need to subst()
from ' ' instead of '0'. (This trick works either way.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
pb wrote:
Im wanted to pad out blank spaces with a specific character instead of spaces or zeros, does C support that?
Yes, but not directly through a standard function.
printf("$%*d", '*', 5); // Not sure what the format string is supposed to look like to do this

example output i would want is this:
$********5


#include <stdio.h>
#include <string.h>

#define HASH "*********"

int main(void)
{
int amount = 520;
char number[100];
sprintf(number, "%d.%02d", amount / 100, amount % 100);
printf( "$%.*s%s\n" ,
(int) (sizeof HASH - 1 - strlen(number)) ,
HASH,
number );
return 0;
}

BTW, $ is not a member of the basic character set.

--
Peter

Nov 14 '05 #4

"pb" <gl*****@yahoo. com> wrote in message
news:1103068973 .18047@sj-nntpcache-3...
Im wanted to pad out blank spaces with a specific character instead of
spaces or zeros, does C support that?

printf("$%*d", '*', 5); // Not sure what the format string is supposed to
look like to do this

example output i would want is this:
$********5


You've got many good answers already. Here's another
alternative:

#include <iostream>
#include <string>
#include <stdio.h>

unsigned int digits(int value, unsigned int radix)
{
unsigned int result = 0;

if(value < 0)
value *= -1;

result = !value;

while(value)
{
++result;
value /= radix;
}

return result;
}

int main()
{
int value = 42;
int wid = 5;
char prefix = '$';
int leading = 0;
char pad = '*';
int i = 0;
unsigned int d = digits(value, 10) + (value < 0);

if(d > wid)
wid = d;

leading = wid - d ;

putchar(prefix) ;

for(i = 0; i < leading; ++i)
putchar(pad);

printf("%d\n", value);
return 0;
}

-Mike
Nov 14 '05 #5
Mike Wahler wrote:
You've got many good answers already. Here's another
alternative:

#include <iostream>
#include <string>
#include <stdio.h>


Did you forget this was comp.lang.c? It's a good thing, since you might
have gotten flamed in comp.lang.c++ for <stdio.h> instead of <cstdio>,
or, in their anti-C exuberance, for using either.
Nov 14 '05 #6

"Martin Ambuhl" <ma*****@earthl ink.net> wrote in message
news:32******** *****@individua l.net...
Mike Wahler wrote:
You've got many good answers already. Here's another
alternative:

#include <iostream>
#include <string>
#include <stdio.h>
Did you forget this was comp.lang.c?


Actually, no. Those C++ headers are 'residue' from
a 'scratch' file I forgot to delete. (I suppose they
must have been scrolled off the screen.)
It's a good thing, since you might
have gotten flamed in comp.lang.c++ for <stdio.h> instead of <cstdio>,
Any flames about that would be unjustified. <stdio.h>
is as valid a standard header in C++ as in C. But yes,
I know, some folks don't know any better.
or, in their anti-C exuberance, for using either.


Let's not go there. :-)

But thanks for pointing out my error.
I'll try to pay better attention in the future.

-Mike
Nov 14 '05 #7
On Wed, 15 Dec 2004 00:37:38 +0000, Chris Torek wrote:

....
printf("%*d", 2, 5);

prints the value "5" in a ten-character field.
Looks like a 2 character field to me. :-)
The field is blank
or zero padded depending on the pad option selected: blank by
default, zero if you use a 0 modifier.)
example output i would want is this:
$********5


There is no standard way to do this. It is easy to build your own
though: just sprintf() the numeric value, and then work with the
string. In this case, to get an integer printed into a ten digit
field and replace leading blanks or zeros with spaces, just do
something like:

char buf[SOME_SIZE]; /* must be at least 11 chars */
int val;
...
sprintf(buf, "%010d", val); /* produces, e.g., 0000000005 */
subst(buf, '0', '*');

where the subst() function reads:

/*
* Do substitutions on leading characters in the given string:
* Replace all occurrences of "from" with "to". (We assume
* from != '\0'.)
*/
void subst(char *s, char from, char to) {
while (*s == from)
*s++ = to;
}

Note that if you print with leading blanks, you will need to subst()
from ' ' instead of '0'. (This trick works either way.)


That depends on whether you want 0 to be output as ********** or
*********0

Lawrence
Nov 14 '05 #8
Chris Torek <no****@torek.n et> wrote:
In article <1103068973.180 47@sj-nntpcache-3> pb <gl*****@yahoo. com> wrote: .... printf("%*d", 2, 5); ^^^
10???
prints the value "5" in a ten-character field. The field is blank

^^^
two???
--
Z (zo**********@w eb.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #9
>On Wed, 15 Dec 2004 00:37:38 +0000, Chris Torek wrote:
printf("%*d", 2, 5);
prints the value "5" in a ten-character field.
In article <pa************ *************** *@netactive.co. uk>
Lawrence Kirby <lk****@netacti ve.co.uk> wrote:
Looks like a 2 character field to me. :-)


Oops. Hasty posting....
Note that if you print with leading blanks, you will need to subst()
from ' ' instead of '0'. (This trick works either way.)


That depends on whether you want 0 to be output as ********** or
*********0


Right, something else I managed to forget to bring up. And if this
is intended for printing money-amounts, one may have to fiddle with
negative numbers as more special cases.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7012
by: Adrian Neumeyer | last post by:
Hello folks, I have a simple question: I want to control the indentation of strings printed out with printf(...). Not just a fixed indentation, but lets say one blank character more for each new line, so that a staircase effect is produced. Example:
7
96263
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me...
9
2616
by: Erik Leunissen | last post by:
L.S. I've observed unexpected behaviour regarding the usage of the '#' flag in the conversion specification in the printf() family of functions. Did I detect a bug, or is there something wrong with my expectations regarding the effect of the following code: printf("NULL as hex: %#4.2x\n", '\0');
31
2593
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
29
17516
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding (void) to printf. Thanks, Whatluo.
0
3126
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as Base64) with the host's dpapi using the entropy value(without storing it as Base64), and then store the encrypted Aes key value and the entropy value...
5
24415
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit number". Is this correct understanding? However this is not always the case. I sometimes see 4, sometimes 2, that does not complete the whole number into 8...
1
1760
by: Dino Viehland | last post by:
I'm assuming this is by-design, but it doesn't appear to be documented: >>> '%8.f' % (-1) ' -1' >>> '%#8.f' % (-1) ' -1.' The docs list the alternate forms, but there isn't one listed for f/F. Itwould seem the alternate form for floating points is truncate & round the floating point value, but always display the . at the end....
6
2817
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.