473,378 Members | 1,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

program output?

bnp
Hi,

I took a test on C.
there was an objective question for program output type.
following is the program:

main()
{
char ch;
int i =2;
for (ch=0;ch<128;ch++)
i+=2;
printf("%d",i);
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.

Regards,

Bhavik
Nov 14 '05
54 4012
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Joe Wright wrote:

... snip ...

Well, even if the requirement for trailing new-line is documented
by the implementation, who exactly is the requirement binding
upon? The programmer or the implementation? For example:

FILE *t = fopen("text.txt", "w");
fprintf(t, "Hello world");
fclose(t);

I suppose I have written eleven characters to text.txt and
nothing else. And so it would not be a line because the required
'\n' is not there. And perhaps not even a stream for the same
reason. Again, who is this requirement incumbent upon?


Now precede the fclose(t); line with:

fflush(t);

and I believe the line needs to be written without the \n.


Considering that fclose(t) will flush the stream, it is downright foolish
to believe that preceding it with a fflush(t) is going to make any
difference.

The only reason for calling fflush on a stream before fclose is to be
able to check its return value and take whatever actions are appropriate
in case of failure.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #51
CBFalconer wrote:
Joe Wright wrote:

... snip ...
Well, even if the requirement for trailing new-line is documented
by the implementation, who exactly is the requirement binding
upon? The programmer or the implementation? For example:

FILE *t = fopen("text.txt", "w");
fprintf(t, "Hello world");
fclose(t);

I suppose I have written eleven characters to text.txt and
nothing else. And so it would not be a line because the required
'\n' is not there. And perhaps not even a stream for the same
reason. Again, who is this requirement incumbent upon?

Now precede the fclose(t); line with:

fflush(t);

and I believe the line needs to be written without the \n.

In actual practice I think the standards provision allows for the
fact that, without a final \n (or fflush), the final data may be
left in an internal buffer, and may simply get discarded.

From N869..

[#2] The fclose function causes the stream pointed to by
stream to be flushed and the associated file to be closed.
Any unwritten buffered data for the stream are delivered to
the host environment to be written to the file; any unread
buffered data are discarded. The stream is disassociated
from the file. If the associated buffer was automatically
allocated, it is deallocated.

No need for fflush() before fclose().
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #52
In <hi********************************@4ax.com> Leor Zolman <le**@bdsoft.com> writes:
On 26 May 2004 15:48:32 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <to********************************@4ax.com> Leor Zolman <le**@bdsoft.com> writes:
On 26 May 2004 12:40:05 GMT, Da*****@cern.ch (Dan Pop) wrote:

I thought it was supposed to be a question about a C program, not about
an unspecified implementation...

If it's a question about a C program, your answer is downright wrong,
period.

The OP asked about "program output type". What does the Standard have to
say about /that/? ;-)
Few things are lamer on Usenet than invoking someone's poor command of the
English language...


It never even occurred to me that he had poor command of the English
language.


OK, let's have a look again:

I took a test on C.
there was an objective question for program output type.
following is the program:
<snip>
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.

If this reflects a good command of the English language, then I don't
even want to see one that, in your opinion, doesn't. In particular, the
sentence:

there was an objective question for program output type.

doesn't make any sense alone. It the rest of the post that clarifies that
the OP wanted to know what is the intended output of a certain program.
And, since he posted the question in c.l.c, it is more than reasonable to
assume that he wanted the answer from the language point of view;
otherwise a newsgroup dedicated to programming on his platform
(whatever that was) was the natural choice.
His post looks better in that regard than some...
I agree that I have seen worse, yet this doesn't make this one look good.
So here's where I think we stand. I'm saying I cannot tell whether the OP
was asking "What does the Standard say about what the output should be", as
you interpret the question (and it is certainly a reasonable
interpretation, especially since that's the core flavor of question this
group deals with day-to-day), or if he's really asking, "How was I
supposed to predict what the output of this program would be on our class
platform?" which, given the quality of the question, strikes me as more
likely to reflect actual intent of the instructor. But of course, I don't
know.


How do you explain the fact that all the other regulars interpreted it in
the context of the newsgroup and only you managed to find a different
interpretation?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #53
Dan Pop wrote:
Leor Zolman <le**@bdsoft.com> writes:
... snip various ...
How do you explain the fact that all the other regulars
interpreted it in the context of the newsgroup and only you
managed to find a different interpretation?


Obviously he is a free spirit, thinking "outside the box" :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #54
Douglas A. Gwyn wrote:
Eric Sosman wrote:
Arthur J. O'Dwyer wrote:
On Wed, 26 May 2004, Douglas A. Gwyn wrote:

As I understand the Standard's intent: An implementation must document
the behavior of a program whose output does not end in a newline, in
one of two ways: either that the output is printed, or that the behavior
is undefined. [...]


...

I didn't say that, and in fact I disagree with it.
Please be more careful in your attributions.


Actually, my error was one of incomplete snippage
rather than of misattribution (count the >'s). Sorry
for the confusion.

--
Er*********@sun.com

Nov 14 '05 #55

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

Similar topics

11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
13
by: Liang Chen | last post by:
Hope you all had a nice weekend. I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.