Eric Sosman
3/20/2011 4:06:00 PM
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