[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

A question about a missing '\n' in printf

Chad

3/20/2011 3:42:00 PM

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
18 Answers

Sherm Pendley

3/20/2011 3:55:00 PM

0

Chad <cdalten@gmail.com> writes:

> Is the following code legal?
....
> printf("%2d ", i); /*The line of code in question*/

Yes, it's legal... What makes you think it might not be?

sherm--

--
Sherm Pendley
<http://camelbones.sourcefor...
Cocoa Developer

Eric Sosman

3/20/2011 4:06:00 PM

0

On 3/20/2011 11:41 AM, Chad wrote:
> 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;
> }

Nothing wrong with it. Assuming no I/O errors, power failures,
stray alpha particles, and malware infestations, the output is (as
a C source string) " 0 1 2 3 4 5 6 7 8 9 \n".

A properly-formed line of text output must end with '\n', but
a single line's content can come from many different function calls.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *p = "Hello, world!";
while (*p != '\0') {
switch (rand()) {
case 3: putc(*p++, stdout); break;
case 7: putchar(*p++); break;
case 4: printf ("%c", *p++); break;
case 11: printf ("%.1s", p++); break;
default: ; /* roll the dice again */
}
}
puts("");
return 0;
}

.... is a slow and stupid, but perfectly valid, helloworld.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Keith Thompson

3/20/2011 4:19:00 PM

0

Sherm Pendley <sherm.pendley@gmail.com> writes:
> Chad <cdalten@gmail.com> writes:
>> Is the following code legal?
> ...
>> printf("%2d ", i); /*The line of code in question*/
>
> 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.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Chad

3/20/2011 4:40:00 PM

0

On Mar 20, 9:19 am, Keith Thompson <ks...@mib.org> wrote:
> Sherm Pendley <sherm.pend...@gmail.com> writes:
> > Chad <cdal...@gmail.com> writes:
> >> Is the following code legal?
> > ...
> >>     printf("%2d ", i); /*The line of code in question*/
>
> > 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.
>

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

Morris Keesan

3/20/2011 4:46:00 PM

0

On Sun, 20 Mar 2011 11:41:45 -0400, Chad <cdalten@gmail.com> wrote:

> 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);

--
Morris Keesan -- mkeesan@post.harvard.edu

ram

3/20/2011 7:32:00 PM

0

Chad <cdalten@gmail.com> writes:
>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.

Keith Thompson

3/20/2011 8:49:00 PM

0

Chad <cdalten@gmail.com> writes:
> On Mar 20, 9:19 am, Keith Thompson <ks...@mib.org> wrote:
>> Sherm Pendley <sherm.pend...@gmail.com> writes:
>> > Chad <cdal...@gmail.com> writes:
>> >> Is the following code legal?
>> > ...
>> >>     printf("%2d ", i); /*The line of code in question*/
>>
>> > 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.
>>
>
> 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.

[...]

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Bacarisse

3/20/2011 9:24:00 PM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Chad <cdalten@gmail.com> writes:
>>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.

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

--
Ben.

ram

3/21/2011 8:10:00 AM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>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.

Urs Beeli

3/21/2011 10:04:00 AM

0

On 21 Mar 2011 08:10:17 GMT Stefan Ram wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> >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.

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
--
"Change is inevitable, except from a vending machine."
-- Urs Beeli