some C puzzles

  • Thread starter madhav_a_kelkar
  • Start date
M

madhav_a_kelkar

I was going through the following code:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.



one sol is to use n-- instead of i, so it will print minus sign 20
times.
 
D

Dan Pop

In said:
I was going through the following code:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character.

This is not possible, in general, because the code must output a newline
character after the last minus sign (otherwise it might not produce any
output at all) and this cannot be achieved by changing exactly one
character.

Dan
 
F

Flash Gordon

Please don't top post. Look at the posts within the last day or so for
the arguments against it.

On 2 Sep 2004 01:21:16 -0700
(e-mail address removed) (Dan Pop) wrote in message
1. we can change i-- to i++

That is changing 2 characters.
2. we can change i-- to n--
3. ??

Neither of your changes causes a new line to be output before the
program terminates. As Dan correctly stated (and he is generally correct
even if some find him abrasive) programs must output a newline before
terminating if they want the last line to be guaranteed to be output.
 
P

Paul

I was going through the following code:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.



one sol is to use n-- instead of i, so it will print minus sign 20
times.

simple one...
for( i = 0; i + n; i-- )

for( i = 0; -i < n; i-- )

for( i = 0; i < n; n-- )

for( i = 0; ~i < n; i-- )

4 is enough, but why do you want this ;-)

-Paul.
 
M

madhav_a_kelkar

Flash Gordon said:
Please don't top post. Look at the posts within the last day or so for
the arguments against it.

On 2 Sep 2004 01:21:16 -0700



That is changing 2 characters.


Neither of your changes causes a new line to be output before the
program terminates. As Dan correctly stated (and he is generally correct
even if some find him abrasive) programs must output a newline before
terminating if they want the last line to be guaranteed to be output.


hi all,
thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.
Madhav.
 
K

Keith Thompson

thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.

C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

C90 7.9.2p2 says the same thing.
 
D

Dan Pop

In said:
(e-mail address removed) writes:
[...]
thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.

C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

C90 7.9.2p2 says the same thing.

Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> '
> cat test.c
#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}
> gcc test.c
> ./a.out
>

I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text. Other
Unix shells behave differently, the last line of program output being
concatened with the prompting text, giving something like:

fangorn:~/tmp 86> ./a.out
Hello worldfangorn:~/tmp 87>

Dan
 
K

Keith Thompson

Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> '
cat test.c
#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}
gcc test.c
./a.out

I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text.

What's really happening here (or at least what happened when I tried
the same thing) is that the program prints "Hello world", and the
shell then clears the line and writes its prompt in the same location.
A sufficiently perverse shell could as easily erase the output of
printf("Hello world\n"); (just as all the output of a console-based
program is typically erased on Windows unless you take steps to keep
the window open). As far as the C implementation is concerned, the
trailing newline is not required; the output appears anyway.

But it's certainly another good reason to provide a newline at the end
of your output.
 
O

Old Wolf

for( i = 0; ~i < n; i-- )

As has been mentioned before (but not on this thread), this
one doesn't work on 2's complement systems (which is the vast
majority of all conforming systems).
 
D

Dan Pop

In said:
(e-mail address removed) (Dan Pop) writes:
[...]
Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> '
cat test.c
#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}
gcc test.c
./a.out

I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text.

What's really happening here (or at least what happened when I tried
the same thing) is that the program prints "Hello world", and the
shell then clears the line and writes its prompt in the same location.

Which, from the user's point of view, is undistinguishable from not having
the last line displayed at all, as long as stdout is connected to an
interactive terminal.

Dan
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top