473,382 Members | 1,387 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,382 software developers and data experts.

beginer C question

Hi everybody

here is my code to print all nonblank character on Input.. This program
according to my knowledge should run till i press Ctrl+D but,,, this is
parsing the input untill i press RETURN ( ENTER).. Why is it so..

thx in advance
prem mallappa

#include <stdio.h>

int main(void)
{
int c;
while ( ( c = getchar() ) != EOF)
{
if ( c == ' ' || c == '\t' || c == '\n')
;
else
putchar (c);
}
return 0;
}

Nov 14 '05 #1
5 1764
Prem Mallappa <pr***********@hotpop.com> wrote in
news:bv************@ID-203908.news.uni-berlin.de:
Hi everybody

here is my code to print all nonblank character on Input.. This program
according to my knowledge should run till i press Ctrl+D but,,, this is
parsing the input untill i press RETURN ( ENTER).. Why is it so..
Because you must tell your program when you are done inputting characters
by issuing a newline. If you want to snarf each character as it is typed
you will need to use some non-standard C method off-topic here.
thx in advance
prem mallappa

#include <stdio.h>

int main(void)
{
int c;
while ( ( c = getchar() ) != EOF)
{
if ( c == ' ' || c == '\t' || c == '\n')


look up the isprint() function.
--
- Mark ->
--
Nov 14 '05 #2
On Thu, 05 Feb 2004 18:42:01 +0530, Prem Mallappa
<pr***********@hotpop.com> wrote:
Hi everybody

here is my code to print all nonblank character on Input.. This program
according to my knowledge should run till i press Ctrl+D but,,, this is
parsing the input untill i press RETURN ( ENTER).. Why is it so..

thx in advance
prem mallappa

#include <stdio.h>

int main(void)
{
int c;
while ( ( c = getchar() ) != EOF)
{
if ( c == ' ' || c == '\t' || c == '\n')
;
else
putchar (c);
}
return 0;
}


It may help to understand the rationale for _why_ you need to press
return before it begins to respond:

Under Unix, where the model for this behavior was forged, the I/O
subsystems (often complete computers, or the equivalent thereof, in
their own right) are often separate from the CPU. To off-load the
burden of dealing with I/O from the CPU, your app (within the
machinations of the first getchar() call) will internally place a
system call that directs the I/O subsystem to "get a line". While this
is happening, perhaps reading from a disk, the CPU is free to work on
other tasks it would rather be doing (like giving time to other
computational processes).

Since the I/O processor doesn't understand the logic of your program,
it goes by simple rules: buffer up until we see a newline or EOF. BTW,
the reason typing ^D doesn't act as an EOF in your case is that it
appears in the middle of a line...it doesn't _count_ as an EOF that
way [don't shoot me, I'm just the messenger].

Once the I/O processor has buffered up its line, only _then_ will
getchar() return with the first character...and then each subsequent
call to getchar() until the line is exhausted won't require I/O,
because the data has already been cached up in memory.

Make sense?
-leor



Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3


Prem Mallappa wrote:
Hi everybody

here is my code to print all nonblank character on Input.. This program
according to my knowledge should run till i press Ctrl+D but,,, this is
parsing the input untill i press RETURN ( ENTER).. Why is it so..

Your knowledge is probably based on the Ctrl+D key combination may
generate EOF on a or some implementations. The Stardard does not
define this therefore their are implementations that do not
have this combination being EOF.

Read fag question 19.1 at:
http://www.eskimo.com/~scs/C-faq/q19.1.html

On your implementation you are getting line-at-a-time processing.

#include <stdio.h>

int main(void)
{
int c;
while ( ( c = getchar() ) != EOF)
{
if ( c == ' ' || c == '\t' || c == '\n')
;
else
putchar (c);
}
return 0;
}


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
Prem Mallappa wrote:
Hi everybody

here is my code to print all nonblank character on Input.. This program
according to my knowledge should run till i press Ctrl+D but,,, this is
parsing the input untill i press RETURN ( ENTER).. Why is it so..

thx in advance
prem mallappa

#include <stdio.h>

int main(void)
{
int c;
while ( ( c = getchar() ) != EOF)
{
if ( c == ' ' || c == '\t' || c == '\n')
;
else
putchar (c);
}
return 0;
}

This code works fine. I've tested it a bit. Maybe it is confusing that
no output is produced when stdin is terminal until you press ENTER, but
program still works and waits for the next line.
Output from terminal is often delivered to program line by line(not by
char), but this is offtopic in current group.

Nov 14 '05 #5
Prem Mallappa <pr***********@hotpop.com> wrote:
# Hi everybody
#
# here is my code to print all nonblank character on Input.. This program
# according to my knowledge should run till i press Ctrl+D but,,, this is
# parsing the input untill i press RETURN ( ENTER).. Why is it so..

Terminal I/O is under special rules. In debugging you want to simplify
the matters under consideration; one possibility here is to use disk
input instead of a terminal. Depending on your operating system,
you should be able to create a file, say test.txt, with sample input
of blank and nonblank text, and then run your program
program <test.txt >syo.txt
and then examine the output syo.txt if it looks correct. Then you can try
program <test.txt
and see if the terminal output looks correct. If all that is fine, then if
program
doesn't seem to work, it is because of terminal I/O set up is not in accord
with your model of terminal I/O, and not because your program is wrong.

So simplify to discover whether the problem is your program itself, or how
you think the terminal is supposed to run.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Wow. A sailboat.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: vee_kay | last post by:
Is there any simpler forum for C++. I feel this forum is advanced for a begineer
3
by: ibm_97 | last post by:
Session 1: $db2 +c db2 => set current isolation = UR db2 => select * from t T1 ------ ABC
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
12
by: GoalieGW | last post by:
Hi, In work I have created a database for meter readings in access. I am not a programmer, but would like to try something. Please respond with detail as I will be lost otherwise. What I would...
4
by: pek | last post by:
Two questions: 1. Is there a way I can find a list of all available php libraries and with which version they can run..? 2. Using php code, is they a way I can find out if I have loaded a...
1
by: itsjyotika | last post by:
Hello Everyone, I need to read data from a CVS file(i created it from micosoft excel) and then need to match it with the one of the date from the command line.If the date is there then it should say...
5
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" )...
1
by: hamed steph | last post by:
i'm a beginer in programing and i need very much your help .i need explanation about while statement (loop initialization and structure of while ) also qualifier (long,short,unsigned ,signed,) type...
7
by: Helpful person | last post by:
I am new to Javascript and have a fairly straightforward question. I am trying to use an image as a link to open a new page with the onmouseclick event. In general this seems to work fine with...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.