the mystery of <ctrl-d>

C

chandanlinster

/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

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

int main(void)
{
int j;

for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

exit(EXIT_SUCCESS);
}
 
F

Flash Gordon

chandanlinster wrote, On 29/04/07 11:31:
/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

<snip correct program for counting characters>

It's the way your system handles indicating EOF from the keyboard and
nothing to do with C. For full details you should ask in a group
dedicated your your system, so a Linux or Unix group probably, but check
the FAQ first. Thsi simple answer is your system, on seeing ^d^d decided
to tell your program it was the end of file instead of sending the ^d^d
to your program.
 
B

Bill Pursell

/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

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

int main(void)
{
int j;

for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

exit(EXIT_SUCCESS);

It seems weird to exit() here instead of simply returning a value...

Your question isn't really specific to C, but the answer might be:
the first ^D flushes the readline buffer (ie, your program is
probably blocking on the first getchar until you hit ^d, at which
point it receives all three characters. ) When you hit ^d the
2nd time, there is nothing in the buffer, so an EOF is sent instead.
In other words, the behavior of typing ^D is described by the
following
pseudo-code:
if (buffer is empty)
write EOF
else
flush buffer
 
J

Joe Wright

chandanlinster said:
/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

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

int main(void)
{
int j;

for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

exit(EXIT_SUCCESS);
}
What happens to ^D? It is a low-level control and never makes it into
the character stream. If it is the first thing you type on a new line,
it may be converted to EOF and presented to the stream.

How it really works is system specific and you'll have to read your
documentation. C has no requirements for ^D or ^Z.
 
K

Keith Thompson

Bill Pursell said:
It seems weird to exit() here instead of simply returning a value...
[...]

Why? Within the main() function, "exit(EXIT_SUCCESS)': and
"return EXIT_SUCCESS;" are (almost) exactly equivalent.
 
B

Bill Pursell

It seems weird to exit() here instead of simply returning a value...

[...]

Why? Within the main() function, "exit(EXIT_SUCCESS)': and
"return EXIT_SUCCESS;" are (almost) exactly equivalent.

Just because it seems odd. It seems cleaner to return
than to exit, but there's no reason for that other than
aesthetics. exit() feels like something that should be
reserved for an error condition.
 
K

Keith Thompson

Bill Pursell said:
Bill Pursell said:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int j;
for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

It seems weird to exit() here instead of simply returning a value...

[...]

Why? Within the main() function, "exit(EXIT_SUCCESS)': and
"return EXIT_SUCCESS;" are (almost) exactly equivalent.

Just because it seems odd. It seems cleaner to return
than to exit, but there's no reason for that other than
aesthetics. exit() feels like something that should be
reserved for an error condition.

Ok, so it's a matter of style -- and one on which the authors of the
standard disagree with you, or they wouldn't have included the
EXIT_SUCCESS macro.
 
A

Al Balmer

Bill Pursell said:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int j;
for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

It seems weird to exit() here instead of simply returning a value...

[...]

Why? Within the main() function, "exit(EXIT_SUCCESS)': and
"return EXIT_SUCCESS;" are (almost) exactly equivalent.

Just because it seems odd. It seems cleaner to return
than to exit, but there's no reason for that other than
aesthetics.

There is one reason. It prevents the compiler complaining that the
function doesn't return a value :)
 
R

Richard Heathfield

Bill Pursell said:
Keith said:
Bill said:
chandanlinster wrote:

It seems weird to exit() here instead of simply returning a
value...

[...]

Why? Within the main() function, "exit(EXIT_SUCCESS)': and
"return EXIT_SUCCESS;" are (almost) exactly equivalent.

Just because it seems odd. It seems cleaner to return
than to exit, but there's no reason for that other than
aesthetics.

Al Balmer already pointed out that a good compiler will diagnose the
absence of a return statement from a function declared as returning a
value.
exit() feels like something that should be
reserved for an error condition.

I wouldn't dream of using it for that purpose. That's what:

return EXIT_FAILURE;

is for. In fact, I can't think of any use whatsoever for exit().
 
N

Nick Keighley

I wouldn't dream of using it for that purpose. That's what:

return EXIT_FAILURE;

is for. In fact, I can't think of any use whatsoever for exit().

terminating the program somewhere other then main().

As a one -exit-per-funtion person I suspect you wouldn't approve.
:)
 
R

Richard Heathfield

Nick Keighley said:
terminating the program somewhere other then main().

And I can't think of any use whatsoever for that, either.
As a one -exit-per-funtion person I suspect you wouldn't approve.
:)

I couldn't possibly comment. :)
 
R

Richard Tobin

terminating the program somewhere other then main().
[/QUOTE]
And I can't think of any use whatsoever for that, either.

If C had a reasonable exception mechanism I might agree, but writing
all your functions to return and check errors is too tedious in cases
where there's nothing sensible to do about the error other than exit.

-- Richard
 
K

Kevin Handy

chandanlinster said:
/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

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

int main(void)
{
int j;

for (j = 0; getchar() != EOF;j++)
;
fprintf(stdout, "Number of characters = %d\n", j);

exit(EXIT_SUCCESS);
}

Ok, I admit it! I stole them! I'm really sorry!!

Look, it's been a really really tough month, and I am seriously
behind on my ctrl-D's. My boss is chewing on my a**, and I have
all these TSP reports to do. I didn't think that anyone would
really miss them, so I took them, all right!
 
B

Ben Pfaff

chandanlinster said:
/* if the program is executed as shown below:
* $./a.out
* ges<ctrl-d><ctrl-d>
*
* OUTPUT:
* Number of characters = 3
*
* Question: What happens to the first and second <ctrl-d>?
*/

This is really a Unix question, not a C question. The best
formulation of the answer (that I'm aware of) was published in
Kernighan and Pike, _The Unix Programming Environment_, toward
the beginning of chapter 2, "The File System".
 
R

Richard Heathfield

CBFalconer said:
So what do you call outside of main? :)

The question seems ambiguous, so I'll answer for each of the two
alternate meanings that I spotted.

If you are talking about the run-time system's behaviour after main()
has terminated, the answer is that I don't care, since as far as I'm
concerned my job is done when main returns.

If, on the other hand, you are asking how I terminate a program from
some user-defined function other than main, the answer is that I don't.

And if you meant something else, I can't guess what it is.
 

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top