A question about a missing '\n' in printf()

C

Chad

Is the following code legal?

#include <stdio.h>

int main(void)
{
int i = 0;

for (i = 0; i < 10; i++) {
printf("%2d ", i); /*The line of code in question*/
}

printf("\n");

return 0;
}




Chad
 
K

Keith Thompson

Sherm Pendley said:
Yes, it's legal... What makes you think it might not be?

That would have been clearer if Chad had put his entire question
in the body of the message rather than having part of it only in
the subject.

I think Chad is concerned that the lack of a "\n" in the printf
might cause it to be illegal.

It's implementation-defined whether the last line of a text stream
requires a terminating new-line (C99 7.19.2p2). This just means
that a '\n' should be the last character written to stdout before
the end of the program; there's no requirement for each printf call
to write a new-line. It's perfectly valid to build up an output
line with multiple calls to printf:
printf("Hello");
printf(", world");
printf("\n");

Note that the Standard doesn't use the terms "legal" and "illegal"
(apart from one reference to "illegal instruction" in a footnote).
A program that writes to stdout and fails to write a terminating
new-line:

#include <stdio.h>
int main(void) {
printf("Missing newline");
return 0;
}

is not "illegal". If it's executed on an implementation that
*doesn't* require a terminating new-line, the standard doesn't
actually say what happens, but there's certainly no requirement
for the system to diagnose any problem. I believe the behavior
is undefined. (For example, the string "Missing newline" might
never appear.)
 
C

Chad

That would have been clearer if Chad had put his entire question
in the body of the message rather than having part of it only in
the subject.

Then what would I've written on the subject line?
I think Chad is concerned that the lack of a "\n" in the printf
might cause it to be illegal.

Yes. That's what I was concerned about.


Chad
 
M

Morris Keesan

for (i = 0; i < 10; i++) {
printf("%2d ", i); /*The line of code in question*/
}

printf("\n");

Note that the output might not actually be written to stdout until
the '\n' is sent, which is why you'll see code like
printf("Enter some input value: ");
fflush(stdout);
 
S

Stefan Ram

Chad said:
Is the following code legal?

It returns 0 even when printf has failed and prints a space
in front of the end of the line.

So, I'd prefer:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{ int ok = 1; int i = 0;

for( int looping = 1; looping; looping = ++i < 10 && ok )
ok = ok && 2 +( i < 9 )== printf( i < 9 ? "%2d " : "%2d", i );

if( 1 != printf( "\n" ))ok = 0;

return ok ? EXIT_SUCCESS : EXIT_FAILURE; }

This terminated the loop on error, but tries to terminate
the line anyways.
 
K

Keith Thompson

Chad said:
Then what would I've written on the subject line?

The subject line was fine; it would have been helpful to repeat
that information in the body of the message.

In my newsreader, for example, I see about 5 lines of headers.
A couple of them are highlighted; the subject line isn't. (I could
probably configure that, but I haven't bothered.) For most articles,
the subject header tells me whether it's worth reading, and the
body tells me everything I need to know.

[...]
 
B

Ben Bacarisse

It returns 0 even when printf has failed and prints a space
in front of the end of the line.

So, I'd prefer:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{ int ok = 1; int i = 0;

for( int looping = 1; looping; looping = ++i < 10 && ok )
ok = ok && 2 +( i < 9 )== printf( i < 9 ? "%2d " : "%2d", i );

if( 1 != printf( "\n" ))ok = 0;

return ok ? EXIT_SUCCESS : EXIT_FAILURE; }

This terminated the loop on error, but tries to terminate
the line anyways.

Seriously? Sometimes c.l.c challenges my sense of humour so I may just
be missing the joke.
 
S

Stefan Ram

Ben Bacarisse said:
Seriously?

Sorry, I do not understand what you want to know.

Possibly, you could ask more specifically and make
it clear, what part of the code you refer to.
 
U

Urs Beeli

Sorry, I do not understand what you want to know.

Possibly, you could ask more specifically and make
it clear, what part of the code you refer to.

I suspect that Ben was referring to the fact that you turned an easily
readable program with two minor flaws [1][2] into an obfuscated piece of code
that takes some serious study to understand in order to get rid of said
flaws.

[1] of course, if there is a requirement that no trailing space be left at
the end of the output line, the solution would need to solve that. However,
the short sample program certainly didn't suffer from that extra space.

[2] admittedly, printf can theoretically fail. However, do you really check
teh return value of every printf you use? And while that may or may not be
necessary in production code, it certainly isn't in an example that tries to
illustrate a completely different point.

Cheers
/urs
 
S

Stefan Ram

Urs Beeli said:
[1] of course, if there is a requirement that no trailing space be left at

You are right that this depends on the requirements, which
are IIRQ unspecified here or unknown to us.

However, it seems as if the program has to print something,
and »return 0« means »I did my job«. When printf explicitly
tells me that it was not able to print and my sole purpose
is printing, then why should I return that I did succeed?
 
E

Eric Sosman

Sorry, I do not understand what you want to know.

Possibly, you could ask more specifically and make
it clear, what part of the code you refer to.

I suspect that Ben was referring to the fact that you turned an easily
readable program with two minor flaws [1][2] into an obfuscated piece of code
that takes some serious study to understand in order to get rid of said
flaws.

And adds a new one.
 
B

Ben Bacarisse

Sorry, I do not understand what you want to know.

I was expressing surprise, that's all. It occurred to me that maybe you
were being humorous -- it would not be the first time I've missed a joke
here.
Possibly, you could ask more specifically and make
it clear, what part of the code you refer to.

There's nothing specific. The program seemed designed to be overly
complex and stylistically obscure. You seemed to be advocating
techniques that don't scale well. In once place (duplicating the loop's
upper bound) I thought you'd deliberately laid a trap for the unwary.
It all seemed to rather over the top.
 
S

Spiros Bousbouras

[1] of course, if there is a requirement that no trailing space be left at

You are right that this depends on the requirements, which
are IIRQ unspecified here or unknown to us.[/QUOTE]

The usually very knowledgeable www.acronymfinder.com doesn't know what
IIRQ means.
 
S

Seebs

Then what would I've written on the subject line?

The subject line should be an overview, but you should always put
the full actual text in the article; the text of the article should
stand without seeing the subject line.

-s
 
S

Seebs

Note that the output might not actually be written to stdout until
the '\n' is sent, which is why you'll see code like
printf("Enter some input value: ");
fflush(stdout);

Interestingly, C99 suggests that it's desireable for implementations to
flush output before waiting for input, and experiments on a couple of
modernish systems (BSD and Linux) suggests that people do this now.

-s
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top