how does this string trick work?

B

Bill Cunningham

Ok say I have a string and I only want to print out so many chars.

printf("%*s\n",5,string+5);

What exactly is this doing? Can one use -5 too in their if they wanted?

Bill
 
A

ais523

Bill said:
Ok say I have a string and I only want to print out so many chars.

printf("%*s\n",5,string+5);

What exactly is this doing? Can one use -5 too in their if they wanted?

The "%*s" means "pad with spaces, to a width of <next argument to
printf> characters, the string <argument to printf after that>". Then
the "\n" outputs a newline, once that is done.

The next argument is a 5, meaning that the output will be padded to 5
spaces, if shorter. This is probably not particularly useful for
producing substrings.

The argument after that is "string+5", i.e. "5 bytes after the start of
string". This will cause printf to ignore the first five characters of
the string, if it's at least five characters long. Otherwise, unless
you somehow have control of what exists beyond the end of the string
(say, because it's just one string stored in a large buffer), it's
undefined behaviour.

None of this is likely to be what you want.
 
B

Barry Schwarz

Ok say I have a string and I only want to print out so many chars.

printf("%*s\n",5,string+5);

This code does not place an upper limit on the number of characters to
be printed. It places a lower limit of 5.

If you want to insure that no more than 5 characters are printed, you
would use
printf("%.5s", address_of_first_character_in_string_to_be_printed)

If the maximum number of characters to be printed is computed and
stored in an int named n, then you could use
print("%.*s", n, address_as_described_above)
What exactly is this doing? Can one use -5 too in their if they wanted?

It is demonstrating that you are copying or generating code without
attempting to understand what the format string means to printf. Is
there some reason you could not look it up in your own reference?
n1256 is freely available and very clear on this point.

In the code you provided, using -5 for the second argument causes the
output to be left justified but still sets the minimum field width to
5.

Using -5 instead of +5 in the third argument is allowed only if string
points to an element at least 5 characters into a char array.
 
B

Bill Cunningham

Barry said:
This code does not place an upper limit on the number of characters to
be printed. It places a lower limit of 5.

If you want to insure that no more than 5 characters are printed, you
would use
printf("%.5s", address_of_first_character_in_string_to_be_printed)

If the maximum number of characters to be printed is computed and
stored in an int named n, then you could use
print("%.*s", n, address_as_described_above)


It is demonstrating that you are copying or generating code without
attempting to understand what the format string means to printf. Is
there some reason you could not look it up in your own reference?
n1256 is freely available and very clear on this point.

In the code you provided, using -5 for the second argument causes the
output to be left justified but still sets the minimum field width to
5.

Using -5 instead of +5 in the third argument is allowed only if string
points to an element at least 5 characters into a char array.

I have lost the standard in a computer crash. I need to get it back. I'm
using a simple parses. Very simple and I do not want printf to print the
first character of a string. That being in my project a '+'. Also I want If
a string starts with two ++ to print one +. If a string doesn't begin with a
'+' it is ignored. If it is it is printed. Minus the leading '+'.

Bill

PS I have never formated strings with printf before.
 
B

Bill Cunningham

ais523 said:
The "%*s" means "pad with spaces, to a width of <next argument to
printf> characters, the string <argument to printf after that>". Then
the "\n" outputs a newline, once that is done.

The next argument is a 5, meaning that the output will be padded to 5
spaces, if shorter. This is probably not particularly useful for
producing substrings.
[snip]

I'm not quite sure what you mean by the above. In my example the
"%*s\n",5...Was not needed to skip 5 characters into the string?

Bill
 
B

Barry Schwarz

I have lost the standard in a computer crash. I need to get it back. I'm

A pretty poor excuse. You obviously have internet access. It would
take less time to reload it than it takes to get answers from Usenet.
using a simple parses. Very simple and I do not want printf to print the
first character of a string. That being in my project a '+'. Also I want If

Then why didn't you code string+1 instead of string+5? And what does
this have to do with your original question about limiting string
output to a specified number of characters?
a string starts with two ++ to print one +. If a string doesn't begin with a

Since you intend to skip the first character, won't this happen
automatically.
'+' it is ignored. If it is it is printed. Minus the leading '+'.

You should be able to code an if statement that determines whether the
first character in a string is a '+' or not.
PS I have never formated strings with printf before.

You need to define what you mean by formatting a string. The effect
of calling printf is output on stdout, not a string of any kind,
formatted or otherwise.
 
B

Bill Cunningham

Barry said:
A pretty poor excuse. You obviously have internet access. It would
take less time to reload it than it takes to get answers from Usenet.

I will have to find where to look. There are various answers and ways to
do this.
Then why didn't you code string+1 instead of string+5?

Do I have to use the example string+1 ? And what's wrong with string +5 ?

And what does
this have to do with your original question about limiting string
output to a specified number of characters?


Since you intend to skip the first character, won't this happen
automatically.

Yes it would.
You should be able to code an if statement that determines whether the
first character in a string is a '+' or not.


You need to define what you mean by formatting a string. The effect
of calling printf is output on stdout, not a string of any kind,
formatted or otherwise.

I'm learning more about printf and its' formatting ability.
 
J

Johannes Löthberg

I will have to find where to look. There are various answers and ways to
do this.

All you have to do is click the first result when you Google 'n1256'.
(or when you DDG it, or the second result on Bing)
 
B

Bill Cunningham

Johannes said:
All you have to do is click the first result when you Google 'n1256'.
(or when you DDG it, or the second result on Bing)

Got it on www.open-std.org and I have the C11 version. I guess there's a
new header called uchar.h.

Bill
 
B

Barry Schwarz

ais523 said:
The "%*s" means "pad with spaces, to a width of <next argument to
printf> characters, the string <argument to printf after that>". Then
the "\n" outputs a newline, once that is done.

The next argument is a 5, meaning that the output will be padded to 5
spaces, if shorter. This is probably not particularly useful for
producing substrings.
[snip]

I'm not quite sure what you mean by the above. In my example the
"%*s\n",5...Was not needed to skip 5 characters into the string?

You wrote the code. You also stated you didn't know what it was
doing. At no point in your original post did you mention skipping
characters. Neither did ais523 in his response. So why are you
bringing it up now?

Maybe it would be a good idea if you provided some specific detail on
exactly what it is you want to do. Don't post code you copied or just
scribbled on a hunch.
 
B

Barry Schwarz

I will have to find where to look. There are various answers and ways to
do this.

An even poorer excuse. You spend an awful lot of time justifying your
lack of progress.
Do I have to use the example string+1 ? And what's wrong with string +5 ?

You were the one who said to start printing at the second character.
Unless you are doing something you are not telling us, the address of
that character is string+1. Where did you come up with string+5?

Does the +5 have something to do with you original request to limit
the output to 5 characters? If so, what makes you think the address
of the characters has anything to do with the quantity of characters?
I'm learning more about printf and its' formatting ability.

All evidence to the contrary.
 
B

Bill Cunningham

Barry Schwarz said:
An even poorer excuse. You spend an awful lot of time justifying your
lack of progress.


You were the one who said to start printing at the second character.
Unless you are doing something you are not telling us, the address of
that character is string+1. Where did you come up with string+5?

Does the +5 have something to do with you original request to limit
the output to 5 characters? If so, what makes you think the address
of the characters has anything to do with the quantity of characters?

It was just an example barry. Just an example. But I did make clear what
I am actually looking for and I needed to do that.
All evidence to the contrary.
As always.
 
B

Bill Cunningham

[snip]
Maybe it would be a good idea if you provided some specific detail on
exactly what it is you want to do. Don't post code you copied or just
scribbled on a hunch.

I think you may be right here Barry. There is an experiment I am just
trying out.
 
S

Seebs

Apart from that, nobody knows for sure if Bill Cunningham is
incapable of learning, has some weird learning disorder, or is just
trolling.

It is something of a mystery. I seem to recall five years ago or more he was
talking about his plans to develop a new database engine because he didn't
think MySQL and PostgreSQL were fast enough or well-designed enough, but he
was stuck on trying to figure out how to print a filename.

I've probably got the details wrong, but it's been pretty much exactly like
that all along. At this point I'm inclined to bet on trolling, because I
can't come up with any kind of learning disorder that would produce the
unchanging pattern of confusion we see.

-s
 
B

Bill Cunningham

Barry Schwarz wrote:

[snip]

Barry I don't spend every minute of my time with C and it is a lot to learn.
If I was a computer scientist I should be able to pound out code like
nobody's business. And I think he'd (nobody) would agree. But I am a simple
hobbyist. printf is pretty complicted when you get beyond simple format
specifers. I think I pretty much have that but ^ and * symbols I have had no
need for yet.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Barry Schwarz wrote:
[snip]

Barry I don't spend every minute of my time with C and it is a lot to learn.
If I was a computer scientist I should be able to pound out code like
nobody's business. And I think he'd (nobody) would agree. But I am a simple
hobbyist. printf is pretty complicted when you get beyond simple format
specifers. I think I pretty much have that but ^ and * symbols I have had no
need for yet.

printf doesn't use the "^" character for anything special.
 
B

Bill Cunningham

Keith said:
printf doesn't use the "^" character for anything special.

I thought for sure I saw that somewhere. Maybe it was 2 carats. Maybe I
was mistaken. I guess I'll get there when I do.

Bill
 
B

Barry Schwarz

Bill Cunningham said:
Barry Schwarz wrote:
[snip]

Barry I don't spend every minute of my time with C and it is a lot to learn.
If I was a computer scientist I should be able to pound out code like
nobody's business. And I think he'd (nobody) would agree. But I am a simple
hobbyist. printf is pretty complicted when you get beyond simple format
specifers. I think I pretty much have that but ^ and * symbols I have had no
need for yet.

printf doesn't use the "^" character for anything special.

Bill is too busy changing the scope of the discussion to be bothered
with mere details. I'm sorry I got sucked into another one of his
trollings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top