473,467 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

K&R2, 1.5.1 example

/* K&R@, 1.5.1 - File Copying */

#include<stdio.h>

int main()
{
int c;

while((c = getchar())!= EOF)
putchar(c);

return 0;
}

--------- OUTPUT -----------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra 1.5.1_file-
copying.c
[arch@voodo kr2]$ ./a.out
like this
like this
and thia
and thia
[arch@voodo kr2]$

you can see that "putchar" prints something only after hitting the
newline. this is exactly in agreement with what K&R2 says at the
beginning of section 1.5:

"Text input or output, regardless of where it originates or where it
goes to, is dealt with as streams of characters. A text stream is a
sequence of characters divided into lines; each line consists of zero
or more characters followed by a newline character"

whereas at the beginning of section 1.5.1 K&R2 says:

"The simplest example is a program that copies its input to its output
one character at a time"

these statements are exactly opposite to each other. i am confused
about this. looking at the code conforms that we are printing only one
character at a time whereas running the code shows that it is printing
a line at a time, rather than one character at a time. how does it
work ?

Apr 4 '07 #1
4 1448
arnuld said:

<snip>
looking at the code conforms that we are printing only one
character at a time whereas running the code shows that it is printing
a line at a time, rather than one character at a time. how does it
work ?
C is doing the actual copying of characters, and it is indeed doing them
one character at a time.

Your system is doing the display of data on the output device, and it is
indeed doing them one line at a time (i.e. it's 'buffering' the data).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 4 '07 #2
On Apr 4, 11:25 am, Richard Heathfield <r...@see.sig.invalidwrote:
C is doing the actual copying of characters, and it is indeed doing them
one character at a time.

Your system is doing the display of data on the output device, and it is
indeed doing them one line at a time (i.e. it's 'buffering' the data).
you want to say that:

1.) C programme is taking input 1 character at a time but this is the
compiler that storing the input into, what you call, a "buffer".

2.) when compiler finds the next character as '\n', it puts that whole
buffer onto the standard output, my terminal.

3.) so compiler is the cause of confusion not the code.

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

Apr 4 '07 #3
arnuld said:
>On Apr 4, 11:25 am, Richard Heathfield <r...@see.sig.invalidwrote:
>C is doing the actual copying of characters, and it is indeed doing
them one character at a time.

Your system is doing the display of data on the output device, and it
is indeed doing them one line at a time (i.e. it's 'buffering' the
data).

you want to say that:

1.) C programme is taking input 1 character at a time but this is the
compiler that storing the input into, what you call, a "buffer".
No, the compiler isn't even *running* at the stage where the data is
being handled. This is nothing to do with your compiler. It's the
system - your bash shell or Windows console or Mac colouring-box or
whatever you're using - that is dealing with input on a line-by-line
level.

When your program requests a character, the shell is collecting an
entire line from the user, and then giving the first character of that
line to your program, which is then free to go and process it.

When your program requests the next character, the shell can hand it
over straight away because it's still got a whole bunch of stuff ready.

Only when it runs out of data (i.e. has handed over the whole line, some
calls later) will it go back to the user for more.

Same in reverse when displaying. Your program hands over a character for
printing, and the shell grabs the character, says "thank you very
much", and does ***nothing at all*** with it - until it is either given
a newline or explicitly told to stop messing about and get on with it,
at which point it will write out everything it has saved up so far.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 4 '07 #4
arnuld wrote:
On Apr 4, 11:25 am, Richard Heathfield <r...@see.sig.invalidwrote:
C is doing the actual copying of characters, and it is indeed doing them
one character at a time.

Your system is doing the display of data on the output device, and it is
indeed doing them one line at a time (i.e. it's 'buffering' the data).

you want to say that:

1.) C programme is taking input 1 character at a time but this is the
compiler that storing the input into, what you call, a "buffer".

2.) when compiler finds the next character as '\n', it puts that whole
buffer onto the standard output, my terminal.

3.) so compiler is the cause of confusion not the code.
No. The compiler has compiled whatever you've asked it to compile and
the code is doing exactly what it appears to be doing. However your
program has a host environment and this host, or operating system is
buffering input and output to and from your program.

When you type in characters at the keyboard, they're typically
collected in a system level buffer and sent to the program only when
you signal an end-of-line, i.e. by pressing the Enter key. This is the
default behaviour of most operating systems, since it gives you a
chance to correct your typing errors and otherwise edit your command
line. All this occurs beyond the reach of your C program and all the
latter can do is block until the system decides to deliver the input.

There're implementation and system specific ways to "turn off" this
behaviour and grab input character by character as soon as the they
appear from the associated device. But because these details vary from
system to system and some systems lack such ability, the C Standard
library was based around a buffered model.

There's also another level of buffering going on - one by the C
Standard library. This is typically "line buffered" for stdin.
There're also options for fully turning on or turning off buffering
with the setvbuf function, but this is not necessary in most cases.
The defaults are fine.

Similarly stdout is line buffered by default, i.e. the stream's
buffers are flushed and the content sent to the associated output
device when a complete line has been recieved. That's why there's the
constant advice to terminate the strings to printf with a newline.
Otherwise output may not appear immediatly. Of course calling
fflush(stdout) causes a immediate write. Likewise streams connected to
disk files are fully buffered by default, i.e., content is written
when the buffer becomes full. Again, you can control this buffering
with setvbuf though in most cases the defaults are good enough.

The operating system may or may not have further buffers of it's own
for output from your program. All that's guaranteed in a Standard C
program is what's spelt out in the Standard.

Apr 4 '07 #5

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

Similar topics

14
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
6
by: arnuld | last post by:
this is the example programme from that section. it does not print the longest line otherwise it works fine, my version has only 1 extra line of "printf". i want to know why it is not printing the...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
7
by: arnuld | last post by:
this is the programme which converts a string of digits into its numeric equivalent, given in section 2.7: /* atoi: convert s to integer */ int atoi(char s) { int i, n; n = 0; for (i = 0;...
1
by: arnuld | last post by:
this is "word counting" example programme from K&R2 section 1.5.4. it runs without any error/warning but does not work properly, i.e. the OUTPUT 1 should be: NEWLINES: 1 WORDS: 1 CHARs: 5
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
2
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 &&...
15
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
1
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...
0
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...
0
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 ...

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.