473,396 Members | 2,011 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,396 software developers and data experts.

What is printed?

DJ
What is printed to the screen?

#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}
Nov 15 '05 #1
10 1431
DJ wrote:
What is printed to the screen?

#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}


"Hello!"

However, where "Goodbye!" goes? Is this default behaviour?

--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #2
"DJ" <An********@comcast.net> wrote in message
news:SM********************@comcast.com...
What is printed to the screen?

#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);


I suppose just hi. What a pervert code anyway.

Alex
Nov 15 '05 #3
"Giannis Papadopoulos" <ip******@inf.uth.gr> wrote in message
news:dc**********@volcano1.grnet.gr...
....
fclose(stdout);
printf("Goodbye!\n");

.... However, where "Goodbye!" goes? Is this default behaviour?


I'd expect default to be undefined. And the app may crash at either of those
two function calls.

Alex
Nov 15 '05 #4
In article <dc**********@volcano1.grnet.gr>,
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
DJ wrote:
What is printed to the screen?
#include <stdio.h>
int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}
"Hello!" However, where "Goodbye!" goes? Is this default behaviour?


It doesn't go anywhere. The printf() will return an error
because the output file is closed.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 15 '05 #5
Walter Roberson wrote on 31/07/05 :
In article <dc**********@volcano1.grnet.gr>,
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
DJ wrote:
What is printed to the screen?
#include <stdio.h>
int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}

"Hello!"

However, where "Goodbye!" goes? Is this default behaviour?


It doesn't go anywhere. The printf() will return an error
because the output file is closed.


Well done

#include <stdio.h>

int main(void)
{
int ret = printf("Hello!\n");
fprintf(stderr, "ret = %d\n", ret);

fclose(stdout);

ret = printf("Goodbye!\n");

fprintf(stderr, "ret = %d\n", ret);
return 0;
}

gives :

Hello!
ret = 7
ret = -1

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #6
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
#include <stdio.h>

int main(void)
{
int ret = printf("Hello!\n");
fprintf(stderr, "ret = %d\n", ret);

fclose(stdout);

ret = printf("Goodbye!\n");

fprintf(stderr, "ret = %d\n", ret);
return 0;
}

gives :

Hello!
ret = 7
ret = -1


Not necessarily. The result of using a FILE pointer after it has been
closed is undefined. Fprintf() may return -1, but equally the program
may crash. Consider an implementation that malloc()s all FILEs and
free()s them when they are closed.

-- Richard
Nov 15 '05 #7
In article <SM********************@comcast.com>
DJ <An********@comcast.net> wrote:
#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}


Your code has undefined behavior. You might as well use this:

% cat v.c
short main[] = {
0x4800,0x0005,0x7fe8,0x02a6,0x3860,0x0001,0x3c5f,0 x0000,
0x3882,0x002c,0x38a0,0x0006,0x3800,0x0004,0x4400,0 x0002,
0x6000,0x0000,0x3860,0x0000,0x3800,0x0001,0x4400,0 x0002,
0x6865,0x6c6c,0x6f0a
};
% cc -o v v.c
% ./v
hello
%

Yes, the above really works. No, not on the x86. Can you figure
out which machine(s) it works on?

What the heck, here is a big hint as to how I generated the above:

% cat u.s
.globl _main
_main:
bl 1f
1:
mflr r31
li r3,1
addis r2,r31,ha16(hello-1b)
la r4,lo16(hello-1b)(r2)
li r5,6
li r0,4
sc
nop
li r3,0
li r0,1
sc
hello:
.ascii "hello\12"
% as -o u.o u.s
% hexdump u.o
[snippage]

(There probably should be a second nop after the second sc, but it
seems to work without it. I am not as familiar with this architecture
as I am with the 386 and sparc; the code above is rather crude.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #8
DJ wrote:

What is printed to the screen?

#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}


Once the nasal demons smash the monitor, it doesn't really matter, does it?

--
+-------------------------+--------------------+-----------------------------+
| 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>
Nov 15 '05 #9
HI Chris,

I have no clue why you mentioned that (your solution)
for the OPs question !
Are you demonstrating an UB ?

Please elaborate on your program

- Ravi

Chris Torek wrote:
In article <SM********************@comcast.com>
DJ <An********@comcast.net> wrote:
#include <stdio.h>

int main(void)
{
printf("Hello!\n");
fclose(stdout);
printf("Goodbye!\n");
return 0;
}

Your code has undefined behavior. You might as well use this:

% cat v.c
short main[] = {
0x4800,0x0005,0x7fe8,0x02a6,0x3860,0x0001,0x3c5f,0 x0000,
0x3882,0x002c,0x38a0,0x0006,0x3800,0x0004,0x4400,0 x0002,
0x6000,0x0000,0x3860,0x0000,0x3800,0x0001,0x4400,0 x0002,
0x6865,0x6c6c,0x6f0a
};
% cc -o v v.c
% ./v
hello
%

Yes, the above really works. No, not on the x86. Can you figure
out which machine(s) it works on?

What the heck, here is a big hint as to how I generated the above:

% cat u.s
.globl _main
_main:
bl 1f
1:
mflr r31
li r3,1
addis r2,r31,ha16(hello-1b)
la r4,lo16(hello-1b)(r2)
li r5,6
li r0,4
sc
nop
li r3,0
li r0,1
sc
hello:
.ascii "hello\12"
% as -o u.o u.s
% hexdump u.o
[snippage]

(There probably should be a second nop after the second sc, but it
seems to work without it. I am not as familiar with this architecture
as I am with the 386 and sparc; the code above is rather crude.)


Nov 15 '05 #10
[top-posting fixed]
Chris Torek wrote:
Your code has undefined behavior. You might as well use [a
program with "short main[] = { ... };"].

In article <1123139608.999384@sj-nntpcache-3>
Ravi Uday <ra******@gmail.com> wrote:I have no clue why you mentioned that (your solution)
for the OPs question !
Are you demonstrating an UB ?
Yes. The effect of undefined behavior is, well, undefined behavior.
The code I posted really does work ... on *some* systems. Similarly,
C code with undefined behavior -- like calling fclose(stdout) and
then calling printf() -- works on *some* systems, and fails on some.
Please elaborate on your program


The data inside the main[] array constitute machine instructions:
load up some registers, and make two system calls (SYS_write and
SYS_exit respectively). This particular operating system and
compiler pair do not place too great a distinction between code
and data, so as long as the main[] array is correctly aligned, it
works. (Making it "int main[]", and changing the data values,
would help avoid potential alignment problems.)

Again, the point is that "undefined behavior" does *not* mean "code
does not work". It also does *not* mean "code works OK". What
it means is "the C standard no longer tells you what the code
does." You must therefore look outside the standard.

Strictly conforming "standard C" code *always* works on *every*
"standard C" implementation (subject of course to things like
running out of memory in malloc(), for instance). Other code
*sometimes* works on *some* implementations. Which is better?
Well, that is a bit like asking whether a sailboat is better than
a motorboat -- it depends on whether you have fuel available and
need to get somewhere fast, too. There are tradeoffs in everything:
nonstandard code may achieve something that *cannot* be done with
standard code -- in which case, you have to use it -- or it might
run much faster, or be much easier to write. Or it may simply fail
to work in the places where it is needed, so that the standard code
would have been better, despite being longer or slower or harder.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #11

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

Similar topics

4
by: Derek Wickersham | last post by:
Easy newbie question. I've searched all the PHP documentation I can find, but can't find an answer. What does the "@" mean, in this context: @mysql_select_db( "scripts");
4
by: Christian Otteneuer | last post by:
Hello Newsgroup, I have the following problem: I want to write a python script, that writes text into files. The text is being "collected" in a for-loop, the code looks like this: for i in...
46
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar...
0
by: hsifelbmur | last post by:
We are writing an app that automates printing of documents of different types. These documents are printed to PostScript files, not to a printer. The app uses shellExecute with the "printto" verb...
1
by: Guhanath | last post by:
Hi is there any way to post the values from asp.net page to a pre printed form,eg:like a commercial billing form where space will be already present to set a value. -- Guhan
6
by: tingjun.li | last post by:
There is a demo program: ------------------------------------------------- const char* WS = " \t\n"; const int n_WS = strlen(WS); char* s1 = "This sentence contains five words."; char* s2 =...
28
by: federico_bertola | last post by:
Hi everybody! I have this function: int Scan(char String) { printf("%s", String); } it works but when I try to pass a dotted string o separetad with (" ", "_" , "-" ...ecc)
3
by: Bart Van der Donck | last post by:
Hello, I'm having a hard time trying to configure printed output that consists of multiple pages. The idea is the following: <div style=" border: 1px solid blue; position: absolute; top:...
5
by: SEEMO | last post by:
I am making a database for our factory, for managing production. I have a form for printing job orders(Job Prep) for each item to be produced, which user selects from a combo box, and the form...
6
by: prakashwadhwani | last post by:
While making Vouchers in a form, I have a POST button which validates & posts the transaction to the main Accounts File. I also have a PRINT button which provides the user with a preview of the...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.