beginer C question

P

Prem Mallappa

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;
}
 
M

Mark A. Odell

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.
 
L

Leor Zolman

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
(e-mail address removed)
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
 
A

Al Bowers

Prem said:
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.
 
V

Victor Nazarov

Prem said:
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.
 
D

Derk Gwen

# 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.
 
M

Mark McIntyre

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

I presume you mean that its /waiting/ till you press input before it
parses.
Why is it so..

This is a FAQ - 19.1
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top