By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

the mystery of <ctrl-d>

P: n/a
/* 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);
}

Apr 29 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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.
--
Flash Gordon
Apr 29 '07 #2

P: n/a
On Apr 29, 11:31 am, chandanlinster <chandanlins...@gmail.comwrote:
/* 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
Apr 29 '07 #3

P: n/a
chandanlinster wrote:
/* 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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 29 '07 #4

P: n/a
Bill Pursell <bi**********@gmail.comwrites:
On Apr 29, 11:31 am, chandanlinster <chandanlins...@gmail.comwrote:
[...]
>#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...
[...]

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 29 '07 #5

P: n/a
On Apr 29, 9:12 pm, Keith Thompson <k...@mib.orgwrote:
Bill Pursell <bill.purs...@gmail.comwrites:
On Apr 29, 11:31 am, chandanlinster <chandanlins...@gmail.comwrote:
[...]
#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...

[...]

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.

Apr 29 '07 #6

P: n/a
Bill Pursell <bi**********@gmail.comwrites:
On Apr 29, 9:12 pm, Keith Thompson <k...@mib.orgwrote:
>Bill Pursell <bill.purs...@gmail.comwrites:
On Apr 29, 11:31 am, chandanlinster <chandanlins...@gmail.comwrote:
[...]
>#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...

[...]

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 29 '07 #7

P: n/a
On 29 Apr 2007 13:21:17 -0700, Bill Pursell <bi**********@gmail.com>
wrote:
>On Apr 29, 9:12 pm, Keith Thompson <k...@mib.orgwrote:
>Bill Pursell <bill.purs...@gmail.comwrites:
On Apr 29, 11:31 am, chandanlinster <chandanlins...@gmail.comwrote:
[...]
>#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...

[...]

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 :-)
exit() feels like something that should be
reserved for an error condition.
--
Al Balmer
Sun City, AZ
Apr 29 '07 #8

P: n/a
Bill Pursell said:
Keith Thompson wrote:
>Bill Pursell writes:
chandanlinster wrote:
> exit(EXIT_SUCCESS);
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().

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 30 '07 #9

P: n/a
On 30 Apr, 06:39, Richard Heathfield <r...@see.sig.invalidwrote:
Bill Pursell said: Keith Thompson wrote:
Bill Pursell writes:
chandanlinster wrote:
exit(EXIT_SUCCESS);
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.
<snip>
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().
terminating the program somewhere other then main().

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

--
Nick Keighley

"The Dinosaurs have come and gone,
we Theriodonts remain"

Apr 30 '07 #10

P: n/a
Nick Keighley said:
Richard Heathfield wrote:
>In fact, I can't think of any use whatsoever for exit().

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. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 30 '07 #11

P: n/a
In article <8I******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>In fact, I can't think of any use whatsoever for exit().
>terminating the program somewhere other then main().
>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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 30 '07 #12

P: n/a
chandanlinster wrote:
/* 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!

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Apr 30 '07 #13

P: n/a
chandanlinster <ch************@gmail.comwrites:
/* 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".
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Apr 30 '07 #14

P: n/a
Richard Heathfield wrote:
Bill Pursell said:
.... snip ...
>
>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().
So what do you call outside of main? :-)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 30 '07 #15

P: n/a
CBFalconer said:
Richard Heathfield wrote:
>Bill Pursell said:
... snip ...
>>
>>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().

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 30 '07 #16

P: n/a
CBFalconer wrote:
Richard Heathfield wrote:
>Bill Pursell said:
... snip ...
>>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().

So what do you call outside of main? :-)
The `return(EXIT_FAILURE);' function, of course!

--
Eric Sosman
es*****@acm-dot-org.invalid

May 1 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.