473,804 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1784
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******@myrapi dsys.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
1537
by: vee_kay | last post by:
Is there any simpler forum for C++. I feel this forum is advanced for a begineer
3
4892
by: ibm_97 | last post by:
Session 1: $db2 +c db2 => set current isolation = UR db2 => select * from t T1 ------ ABC
3
3096
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 before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
12
1288
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 like to do is eliminate the possibility of someone entering in bad data. To do this, I want to compare the same field on two different records to see if the result is possitive or negative. If negative, a message box. Each record has a date...
4
1583
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 specific PECL extension..? (like extension_loaded() for php libraries). Any help would be appreciated.
1
1557
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 yes or else it should say no. I am not getting how to do this as i am just a beginer in perl. Can anybody help me in writting the code. the files r toooo.. big to be attached , so i have just shown a tiny portion of it. thanks in advance, rimjim...
5
3235
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" ) cout << "First character is s" << endl; OR: string str("string"); string::iterator it = str.begin();
1
1249
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 of variable (character and double). think you.......
7
1664
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 the open statement. I wish to use the same script at various places in my web and hence wish to pass to the javascript function the URL location and the width and height of the new page. I am having no luck trying to get the open statement to...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.