Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why this works???

Reply
Thread Tools

Why this works???

 
 
Harald van Dijk
Guest
Posts: n/a
 
      01-12-2008
On Sat, 12 Jan 2008 01:29:16 +0000, Mark McIntyre wrote:
> Harald van Dijk wrote:
>> On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
>>>>>>> http://www.velocityreviews.com/forums/(E-Mail Removed) (Kenny McCormack) wrote in
>>> current operations to fail. So, the above program would (under all
>>> the usual CLC rules) work, as far as printing "hello, world", even
>>> though it might start global thermonuclear war thereafter.

>
> printf() sends data to the stream, and the \n instructs the stream to
> flush.
>
> Neither of these forces the output to definitively appear [...]


Good point.

>> Indeed. I've asked a similar question in the past. If the compiler can
>> prove that at a given point, there is absolutely no way to finish
>> running the program without it invoking undefined behaviour, there are
>> already effectively no requirements on the implementation.

>
> UB is an aspect of /runtime/ not compile-time behaviour.


If you mean that compiling a program that has UB should not reformat your
hard drive if you don't actually try to run the program, then sure. If
you mean that UB (as opposed to syntax errors or constraint violations)
means a program must still be compiled by any conforming implementation,
then that is not entirely correct, because compilers are allowed to
reject programs when they can prove every execution path of the entire
program will invoke UB. But if you meant either of those, then it's not
relevant to my message, and if you meant something else, I didn't get it.
 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      01-12-2008
On Fri, 11 Jan 2008 17:28:27 -0800, user923005 wrote:
>> On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
>> > [...] And, let's assume for the sake of
>> > argument that the above program does cause UB. [...]

>
> /*
> OK, What am I missing? I do not see how undefined behavior comes into
> play here.


There was no undefined behaviour, just a "what if" question.
 
Reply With Quote
 
 
 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      01-12-2008
On Jan 12, 9:06*am, (E-Mail Removed) (Kenny McCormack)
wrote:
[...]
> >>>>> #include <stdio.h>
> >>>>> int main(void) { printf("hello, world\n"); }

>
> >>>>> I compile it with all warning turned on and it still works fine.

>

[...]
>
> Actually, every time I post this sort of thing (which is my stock sort
> of response to idiotic "Why this works?" type posts), I forget about the
> need for a "return 0;" at the end (actually testing my code with gcc
> with all warnings turned on would have shown this up).
>


I might say it isn't idiotic.

> P.S. *Of course, I lied in the original post, since if I had compiled it
> with all warning turned on, I would have noticed the lack of "return 0;".


The lack of a return statement means /*return (void) expr;*/; and the
function main() itself is type of non-void. The undefined behavior
definitively happens then. The whole program contains UB and may
cause demon fly out of your nose.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-12-2008
Harald van D?k wrote:
> Tomás Ó hÉilidhe wrote:
>

.... snip ...
>
>> we both already know why the C standard permits that
>> program to do whatever it likes.

>
> I don't. I don't see anything wrong with Kenny's program, and I
> suspect there was nothing meant to be wrong with it.
>
> And your message headers are still broken.


How? I see nothing wrong, except (possibly) the char set for the
senders name.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-12-2008
In article <(E-Mail Removed)>,
user923005 <(E-Mail Removed)> wrote:

>/*
>OK, What am I missing? I do not see how undefined behavior comes into
>play here.


>I see nothing wrong with the program:
>*/
>#include <stdio.h>
>int main(void) { printf("hello, world\n"); }


>/*
>5.1.2.2.3 Program termination
>1 If the return type of the main function is a type compatible with
>int, a return from the initial call to the main function is equivalent
>to calling the exit function with the value returned by the main
>function as its argument;10) reaching the } that terminates the main
>function returns a value of 0.
>*/


That's the case for C99, but not for C89. This subthread proceeded
from my pointing out the lack of return value being an issue for C89.
--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2008
Mark McIntyre <(E-Mail Removed)> writes:
[...]
> UB is an aspect of /runtime/ not compile-time behaviour.


Not entirely. C99 3.4.3p2:

NOTE Possible undefined behavior ranges from ignoring the
situation completely with unpredictable results, to behaving
during translation or program execution in a documented manner
characteristic of the environment (with or without the issuance of
a diagnostic message), to terminating a translation or execution
(with the issuance of a diagnostic message).

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:
[...]
> The lack of a return statement means /*return (void) expr;*/;


If you mean that a missing return statement is equivalent to a
comment, you're right. If you mean that it's equivalent to
return (void) expr;
then you're mistaken.

> and the
> function main() itself is type of non-void. The undefined behavior
> definitively happens then. The whole program contains UB and may
> cause demon fly out of your nose.


In C99, falling off the end of main is perfectly legal, and equivalent
to ``return 0;''. In C90, "the termination status returned to the
host environment is undefined". This is not undefined behavior; if
demons fly out of your nose, it was caused by the host environment,
not by the program.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      01-12-2008
On Fri, 11 Jan 2008 22:01:39 -0500, CBFalconer wrote:
> Harald van D?k wrote:
>> Tomás Ó hÉilidhe wrote:
>> And your message headers are still broken.

>
> How? I see nothing wrong, except (possibly) the char set for the
> senders name.


That's exactly it. Message headers must not contain any non-ASCII
characters. If his name contains non-ASCII characters, it should be
encoded. Even if it weren't invalid, there is no sane way for a
newsreader to interpret his name when no encoding is specified. Tomás has
been told this before.
 
Reply With Quote
 
Toms hilidhe
Guest
Posts: n/a
 
      01-12-2008
=?UTF-8?q?Harald_van_D=C4=B3k?= <(E-Mail Removed)> wrote in
news:db401$47888992$541dfcd3$(E-Mail Removed)1.n b.home.nl:

>> How? I see nothing wrong, except (possibly) the char set for the
>> senders name.

>
> That's exactly it. Message headers must not contain any non-ASCII
> characters. If his name contains non-ASCII characters, it should be
> encoded. Even if it weren't invalid, there is no sane way for a
> newsreader to interpret his name when no encoding is specified. Tomás
> has been told this before.



Harald, how do you write your own name for Usenet? It comes up as the
following on my newsreader:

=?UTF-8?q?Harald_van_D=C4=B3k?=

It's not that I'm too lazy to sort the problem out, it's just that I don't
know how to get my newsreader to use UTF-8 properly.

--
Toms hilidhe
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      01-12-2008
"Tomás Ó hÉilidhe" <(E-Mail Removed)> writes:

> (E-Mail Removed) (Kenny McCormack) wrote in news:fm8ufo$hfj$1
> @news.xmission.com:
>
>> #include <stdio.h>
>> int main(void) { printf("hello, world\n"); }
>>
>> I compile it with all warning turned on and it still works fine.

>
>
> Forgetting for the moment that you're a troll -- a troll who's actually
> not half-bad at C programming, albeit, it's a shame you don't contribute
> your expertise -- we both already know why the C standard permits that
> program to do whatever it likes.
>
> I've plenty of friends that get a kick out of winding people up... but
> to be honest they've got better, more entertaining ways than posting to a
> newsgroup. Which is why I'm led to believe that you actually do have more
> than a passing interest in C.
>
> I think you'll find that if you were to start posting something other
> than tripe, that the regulars would welcome you with open arms. And no,
> that's not an invitation to start a debate. In fact, as a precaution, I'm
> going to killfile this thread right now.
>
> Best of luck to you in life though, nobody can deny that you're
> creative and inventive, even if it's misplaced.


Laughing at the blown up ego maniacs and lunchtime C experts in this NG
is not a waste of time. It helps to remind newcomers that not every one
here is here for their own publicity and to prance around acting like
the big I am. I enjoy Kenny's posts. The likes of Heathfield and CBF
need a Kenny to laugh at their pompous posturings.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments