473,471 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Please Explian

Hi,

int main(int argc, char **argv)
{
int j;
printf("Word id %s",argv[1]);

}
This never used to happen.

I have always seen people saying that use \n in printf statement ie
printf("Hello\n");
as it works out as fflush but i never paid attention.

But this time my prog was not printing what it was suppose to print .
But i used '\n' and it printed.
Can anybody explain why we should use this in *detail* . As i want to
know the *detail* reason for this behaviour.

Thanx in advance.

Mar 14 '06 #1
4 1506
In article <11**********************@z34g2000cwc.googlegroups .com>,
ma***********@gmail.com <ma***********@gmail.com> wrote:
int main(int argc, char **argv)
{
int j;
printf("Word id %s",argv[1]);
} This never used to happen.
Wot, you never used to write code that relies upon the value of
argv[1] without first checking to see that argc >= 2 ?

I have always seen people saying that use \n in printf statement ie
printf("Hello\n");
as it works out as fflush but i never paid attention. But this time my prog was not printing what it was suppose to print .
But i used '\n' and it printed.
Can anybody explain why we should use this in *detail* . As i want to
know the *detail* reason for this behaviour.


The C standard *allows* implementations to not produce the last
line of a text stream when the last line is not terminated with \n .
This is the case whether or not the stream is fflush()'d, and
is the case whether or not the stream is explicitly close()'d.

Whether a particular implementation has this property or not is
implementation-specific, and the implementation could have any
reason for it. An implementation could even handle things this
way just to be perverse, or might do it deliberately in order
to encourage students to learn this detail of C.

You did not mention which platform you are using or which software
version, so we cannot tell you why -your- platform does things
the way it does. And since you asked for "*detail*", if you
had specified the platform, we would have told you to go ask
in a newsgroup that specializes in that platform.

So, no detail here. But I will say that sometimes it only *looks*
like the last line not being produced, when actually the last
line -is- produced but the command prompt then visually
overwrites the last line.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Mar 14 '06 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
The C standard *allows* implementations to not produce the last
line of a text stream when the last line is not terminated with \n .
This is the case whether or not the stream is fflush()'d, and
is the case whether or not the stream is explicitly close()'d.


I don't think the C standard is even that specific.

C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

As far as I can tell, the standard gives no hint about what happens if
the last line "requires" a terminating new-line and the program
doesn't provide one. The worst thing that's *likely* happen on most
implementations is that the last line doesn't appear, but I think it's
actually undefined behavior. I can imagine scenarios where you'd get
only part of the last line, or even where the resulting text file is
malformed. (Think about systems where a text file is represented as a
sequence of records rather than as a stream of characters.)

The standard *could* have nailed this down more precisely. For
example, it could have said something like this:

... Whether the last line requires a terminating new-line
character is implementation-defined. If it does, closing the file
writes a new-line to the stream if the last character written to
the stream was not a new-line.

This would allow a text file with no terminating new-line to be
created on systems that don't require it, while guaranteeing (barring
errors) a properly terminated file on systems that do require it.

In fact, I'd be (mildly) surprised if there were any real-world
implementations that don't already follow this suggested requirement.
But since it's not in the standard, we can't depend on it if we want
maximally portable code.

--
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.
Mar 14 '06 #3
Keith Thompson wrote:
[...]
The standard *could* have nailed this down more precisely. For
example, it could have said something like this:

... Whether the last line requires a terminating new-line
character is implementation-defined. If it does, closing the file
writes a new-line to the stream if the last character written to
the stream was not a new-line.

This would allow a text file with no terminating new-line to be
created on systems that don't require it, while guaranteeing (barring
errors) a properly terminated file on systems that do require it.

[...]

What if the terminating-newline-requirement was device dependent? What
if the "terminal" where the stream ends up works just fine without the
terminating newline, but the "printer" attched to some other output
stream requires it in order to print the line? How is the C library to
know whether the output device needs it or not?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Mar 15 '06 #4
Kenneth Brody <ke******@spamcop.net> writes:
Keith Thompson wrote:
[...]
The standard *could* have nailed this down more precisely. For
example, it could have said something like this:

... Whether the last line requires a terminating new-line
character is implementation-defined. If it does, closing the file
writes a new-line to the stream if the last character written to
the stream was not a new-line.

This would allow a text file with no terminating new-line to be
created on systems that don't require it, while guaranteeing (barring
errors) a properly terminated file on systems that do require it.

[...]

What if the terminating-newline-requirement was device dependent? What
if the "terminal" where the stream ends up works just fine without the
terminating newline, but the "printer" attched to some other output
stream requires it in order to print the line? How is the C library to
know whether the output device needs it or not?


The C library, or whatever lower-level code it invokes, obviously has
to know how to write to whatever device it's talking to. Knowing
whether that device requires a trailing new-line shouldn't be a
significant addtional burden.

If that's not possible for some reason, I suppose the standard could
allow fclose() to write an extra new-line even if it's not strictly
required.

--
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.
Mar 15 '06 #5

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

Similar topics

1
by: Numberwhun | last post by:
Hello everyone! I am trying to learn java and have run into kind of a snag. Here is the code that I have so far: ------ <begin_code> ---------- import javax.swing.*; import...
1
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my...
0
by: s_erez | last post by:
Hi, This is a realy tricky one. I have an ASP.NET application where some pages are reading data from a DB and presenting reports. In order for the user to wait while the page is reading data from...
4
by: dave | last post by:
Hi guys I display one page in popup window...that fetches some data from sql and perfom some calculation (tht approx 10 secs) and display result.... I am trying to display "Please wait ..."message...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: pshindle | last post by:
DB2 Team - I just downloaded and unzipped the new Fixpack 9 for DB2 ESE V8 for Windows (FP9_WR21350_ESE.exe). I then burned the unzipped Fixpack files to a CD. I proceded to install this...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
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
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...
1
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...
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: 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: 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.