473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

the mystery of <ctrl-d>

/* 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
16 3085
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mark_galeck_spam_magnet | last post by:
Hello, K&R say about the constant expression after #if: "The resulting constant expression is restricted: it must be integral (...)" Clearly, the expression 1 < FOO if FOO is undefined, is...
2
by: Mystery Man | last post by:
I have developed a series of user defined controls. These have been placed on a variety of different forms. From within one of these controls, I want to show another dialog directly to the right...
11
by: Mad Joe | last post by:
I'm using a richTextBox for editing a source code (with Syntax Highlighting) of my own programming language... How come that myRichTextBox doesn't respond to Undo/Redo functions by using default...
9
by: wscrsurfdude | last post by:
f = open('myfile,'r') a = f.read(5000) When I do this I get the first 634 bytes. I tried using the: f = open('myfile,'rb') option, but now there are a few 0x0D bytes extra in myfile. 0x0D =...
3
by: AnalogKid17 | last post by:
I have the following line of code in my start/login page: document.getElementById("txtLogin").focus(); When I run the website app through F5 (Debug, Start Debugging) it works fine (ie, focus...
2
by: K B | last post by:
I'm sorry to post OT here but I was hoping that someone could help me. I use Visual Studio 2005 and suddenly my Find feature has completely stopped working. When I select it, the screen flickers...
1
by: Edward K Ream | last post by:
Hello all. I'm tracking down memory leaks in my app. To do this I wrote a script to show the numbers of each different type of object. But it doesn't show strings! Here is the script: import...
6
by: John Dann | last post by:
Don't know whether anyone might be willing to take a look at a little CSS problem that's stumping me. There's a page fragment at: http://www.weatherstations.co.uk/default_test.htm and the item...
9
Frinavale
by: Frinavale | last post by:
This has to be one of the most frustrating controls I've ever designed. First I couldn't stop the __doPostback method from executing for DropDownLists (ie select elements). Now the __doPostback...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.