473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is the difference between '\n' versus pressing return at the prompt

Given:

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 200

int main(void) {

char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");

}
How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la

Chad

Jan 26 '07 #1
10 2006
Chad wrote:
>
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 200
int main(void) {

char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");
if(ferror(stdin))
fprintf(stderr, "input error \n");
}

How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la
It's perfectly obvious to me, but I find it hard to explain :-(
For one thing, it is not needed in console input, because the input
system is quite capable of translating your press of 'return' into
a '\n' to transmit to the program. Notice that at the same time
that input system is probably echoing both a '\r' and a '\n' to
your terminal, which will be the ascii sequence <0x0d><0x0a(if
your system is using Ascii). Notice also that you (probably)
generated a '\r' with the enter key, but the program received a
'\n'.

However, within the program source, you can't just enter a <return>
willy nilly, because that would be the end of the source line, and
probably a syntax error. So C has escape characters, signalled by
the \, to represent various special characters IN THE SOURCE. Some
of these same escape sequences are used elsewhere in various
systems, but that is another matter entirely.

With any luck you are now totally confused. :-)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Jan 26 '07 #2
"Chad" <cd*****@gmail.comwrites:
Given:

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 200

int main(void) {

char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");

}
How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la
Do you mean that you typed
quotation mark
letter 'l'
letter 'a'
backslash
letter 'n'
(etc.)
on your keyboard?

The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.

--
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.
Jan 26 '07 #3
Chad wrote:
Given:

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 200

int main(void) {

char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");

}
How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la
The backslash based escape sequence is only valid within string and
character literals in C. If input is from the console, the operating
system and the C library translate the actual newline sequence, (i.e.
the Enter or Return keypress), into the C newline character. The
character sequence \n is treated literally.

Jan 26 '07 #4

Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
Given:

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 200

int main(void) {

char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");

}
How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la

Do you mean that you typed
quotation mark
letter 'l'
letter 'a'
backslash
letter 'n'
(etc.)
on your keyboard?
Yes.
>
The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.
I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell

Chad

Jan 26 '07 #5
Chad wrote:
Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
Given:
>
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 200
>
int main(void) {
char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");
>
if(ferror(stdin))
fprintf(stderr, "input error \n");
}
>
How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"
>
Why doesn't it print
la
la
la
<snip>
The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.

I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell
This has nothing to do with the shell or the operating system. Escape
sequences are a mechanism to include control characters, invisible
characters, or characters which are otherwise hard to type, _within_ C.
These're then "translated" to the actual character/s they represent
when the string is sent to an output stream. When input from a stream
is read into an array, these formatting character/s are again
translated to their corresponding C escape sequence.

For example under Unix, the end-of-line marker is a single
carriage-return character, which is generated when you press the Enter
or Return key. The same keypress under DOS/Windows generates two
characters, a carriage return and a linefeed character.

To abstract out these system specific details, yet provide a consistent
mechanism, the C library, translates a predefined set of character/s
into their C equivalents, which we syntactically denote in the \xx form
in strings.

If these had not been defined, you'd have to include the actual
character code into your strings, something that's awkward to do.

Jan 26 '07 #6
"santosh" <sa*********@gmail.comwrites:
Chad wrote:
>Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
<code snipped>
<snip>
The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.

I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell

This has nothing to do with the shell or the operating system.
<snip>
>
For example under Unix, the end-of-line marker is a single
carriage-return character,
*nix systems use LF, not CR.
which is generated when you press the Enter
or Return key. The same keypress under DOS/Windows generates two
characters, a carriage return and a linefeed character.
This is an example of why OT answers should not be offered since any
errors either have to be left uncorrected or the topic drifts. I may
have added errors, confusions and such like so my correction may need
correcting, and so on.

My apologies if ignoring OT corrections is considered the better
option.

--
Ben.
Jan 26 '07 #7
santosh wrote, On 26/01/07 06:45:
Chad wrote:
>Keith Thompson wrote:
>>"Chad" <cd*****@gmail.comwrites:
Given:

#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 200

int main(void) {
char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");
}

How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la
<snip>
>>The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.
I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell

This has nothing to do with the shell or the operating system. Escape
sequences are a mechanism to include control characters, invisible
characters, or characters which are otherwise hard to type, _within_ C.
Correct up to here.
These're then "translated" to the actual character/s they represent
when the string is sent to an output stream. When input from a stream
is read into an array, these formatting character/s are again
translated to their corresponding C escape sequence.
Whilst it *could* be done that way it normally isn't, and I don't think
that is the best way to think of it.

On compiling the program the '/n' if converted in to a newline
character. The libraries do whatever is needed to make pressing the
return key send a newline character to the program. It also does any
magic required for printing a newline character to "do the right thing"(tm).
For example under Unix, the end-of-line marker is a single
carriage-return character,
No, it is a newline character not a carriage return character.
which is generated when you press the Enter
or Return key. The same keypress under DOS/Windows generates two
characters, a carriage return and a linefeed character.

To abstract out these system specific details, yet provide a consistent
mechanism, the C library, translates a predefined set of character/s
into their C equivalents, which we syntactically denote in the \xx form
in strings.

If these had not been defined, you'd have to include the actual
character code into your strings, something that's awkward to do.
This is true although you are mixing up two issues.

To the OP, if you think of the escape sequences (things like /n) as
being translated during compilation it should become obvious that they
do not exist at run time so typing slash-n does not send a newline to
the program, just as typing 'printf("Hello");' as an input to the
program will not make the program print an extra "Hello".
--
Flash Gordon
Jan 26 '07 #8
Ben Bacarisse said:
"santosh" <sa*********@gmail.comwrites:
<snip>
>>
For example under Unix, the end-of-line marker is a single
carriage-return character,

*nix systems use LF, not CR.
>which is generated when you press the Enter
or Return key. The same keypress under DOS/Windows generates two
characters, a carriage return and a linefeed character.

This is an example of why OT answers should not be offered since any
errors either have to be left uncorrected or the topic drifts. I may
have added errors, confusions and such like so my correction may need
correcting, and so on.

My apologies if ignoring OT corrections is considered the better
option.

No, you're right that OT answers are a nuisance and you've correctly
identified one of the reasons. Your reply was well-formed and useful.

Nevertheless, in his defence I'd like to say that the reply "santosh" gave
to a clearly topical question was equally clearly an attempt to draw on
various machine differences to illustrate a point about portability, and in
that sense his remarks were not exactly off-topic. They were, however,
incorrect! :-)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 26 '07 #9
Flash Gordon wrote:
santosh wrote, On 26/01/07 06:45:
Chad wrote:
Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
Given:

#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 200

int main(void) {
char buff[MAXLINE];
while( fgets(buff, MAXLINE, stdin) != NULL)
if( fputs(buff, stdout) == EOF)
fprintf(stderr, "output error \n");

if(ferror(stdin))
fprintf(stderr, "input error \n");
}

How come when I go like:
cdalten@linux:~./ef3
"la\nla\nla\n"
"la\nla\nla\n"

Why doesn't it print
la
la
la
<snip>
>The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.

I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell
<snip>
For example under Unix, the end-of-line marker is a single
carriage-return character,

No, it is a newline character not a carriage return character.
Sorry about that. Got that mixed up.

<snip>

Jan 26 '07 #10
Ben Bacarisse wrote:
"santosh" <sa*********@gmail.comwrites:
Chad wrote:
Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
<code snipped>
<snip>
The sequence of a backslash followed by a letter 'n' is interpreted as
a newline only in a string or character literal in a C source file.
On input, it's just a backslash followed by a letter 'n', and has no
special meaning.

I think the confusion is stemming from the fact that I still have no
real grasp over how the Unix Shell works. I'm suspecting that I might
have to the archane Unix questions to either comp.unix.programmer or
comp.unix shell
This has nothing to do with the shell or the operating system.
<snip>

For example under Unix, the end-of-line marker is a single
carriage-return character,

*nix systems use LF, not CR.
Thanks and apologies to Chad. That was a thinko.
which is generated when you press the Enter
or Return key. The same keypress under DOS/Windows generates two
characters, a carriage return and a linefeed character.

This is an example of why OT answers should not be offered since any
errors either have to be left uncorrected or the topic drifts. I may
have added errors, confusions and such like so my correction may need
correcting, and so on.
You're probably right. My first reply to Chad should've been enough.
My apologies if ignoring OT corrections is considered the better option.
I think a quick correction, a you've done here, is okay, since
misleading information is subtly dangerous.

Jan 26 '07 #11

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
2
by: MLH | last post by:
Here is the code. I got it off the MicroSoft site (for Access 2.0) and modified a few lines per recommendation of some of you in this newsgroup. This code runs fine in Access 2.0, but not at all...
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: Camellia | last post by:
Hi all, I'll get straight into it. When I try to run the code: ..... while (scanf("%c", &c) == 1) printf("%c", c); ..... I input "abcd" follows by an EOF(Ctrl + d) instead of pressing...
5
by: Matthew Zhou | last post by:
I check the ISO 9899 standard, \r (carriage return) Movesthe active position to the initial position of the current line. What does it do? How to input? I tried to use Flex to check this...
0
by: petr | last post by:
Hello Forum, I work under linux kernel 2.6.20-1.2320.fc5 I wrote an awk script (named prim_fail) for generating a sequence of draws from a binomial distribution: BEGIN { srand() } {...
6
by: akk003 | last post by:
Hello I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code: int loop = 1; char...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
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: 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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.