Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is this prog stdc?

Reply
Thread Tools

is this prog stdc?

 
 
Heinrich Pumpernickel
Guest
Posts: n/a
 
      09-10-2007
is this prog stdc?


no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
but how does it wrk???


#include <stdio.h>
int main(void)
{
return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
0;
}



--
mfg, heinrich

 
Reply With Quote
 
 
 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-10-2007
"Heinrich Pumpernickel" <(E-Mail Removed)> a écrit dans le message de
news: (E-Mail Removed) m...
> is this prog stdc?
>
>
> no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
> but how does it wrk???
>
>
> #include <stdio.h>
> int main(void)
> {
> return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
> 0;
> }


Well on those vulgar architectures, it should print unix and an end of line.
But It is not strictly portable: when run on a DS9000 in EBCDIC mode, it
causes it to grow hair and hop around the room purring, can you spot why?
Indeed switching this wonderful piece of machinery back to ASCII doesn't
seem to fix the problem: it just stopped the purring. Should I take it to
the vet or is there a genuine flaw in this program?

--
Chqrlie.


 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      09-10-2007
On Sun, 09 Sep 2007 19:43:06 -0700, Heinrich Pumpernickel wrote:

> is this prog stdc?
>
>
> no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
> but how does it wrk???
>
>
> #include <stdio.h>
> int main(void)
> {
> return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
> 0;
> }

__STDC__ is 1.
a[i] is defined as *(a + i).
Arrays, including string literals, evaluate to the address of
their first member.
In ASCII, which gcc and lcc-win32 use, 'a' is 97, in hexadecimal
that's 0x61.

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      09-10-2007
"Charlie Gordon" <(E-Mail Removed)> wrote:

Why are you cross-posting _all_ your replies to comp.std.c? Even when
they don't belong here?

Richard
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-10-2007
"Richard Bos" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)4all.nl...
> "Charlie Gordon" <(E-Mail Removed)> wrote:
>
> Why are you cross-posting _all_ your replies to comp.std.c? Even when
> they don't belong here?


3 cross posts does not make for _all_ replies.

I did post this code sniplet to comp.std.c because it raises a question that
belongs here more than in comp.lang.c: the obfuscated expression contains
pointer arithmetic that computes an address beyond the end of a character
string constant. most optimizing compilers will turn (0x61 + "fun" - 0x60)
into ("fun" + 1), but is it strictly correct or is UB invoked ?

I apologize if this has already been discussed here before, but I think it
is very much on topic.

--
Chqrlie.


 
Reply With Quote
 
André Gillibert
Guest
Posts: n/a
 
      09-10-2007

Charlie Gordon wrote:

> pointer arithmetic that computes an address beyond the end of a character
> string constant. most optimizing compilers will turn (0x61 + "fun" -
> 0x60)
> into ("fun" + 1), but is it strictly correct or is UB invoked ?
>
> I apologize if this has already been discussed here before, but I think
> it
> is very much on topic.


No, it's not. If you asked something like "is it intentional that the
current wording of the standard forbids that?", then, it would be on topic.
But you ask: "With the current wording of the standard, is it UB?".

It's off-topic.I set followup to comp.lang.c.

The answer is yes, it's UB, because it does arithmetic outside of array
boundaries, which is UB by 6.5.6p8. The fact that optimizing compilers
*might* make the program behave correctly is irrelevant.

comp.lang.c is about standard C.
comp.std.c is about the C standard.

--
You can contact me at <(E-Mail Removed)>
 
Reply With Quote
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      09-10-2007
Army1987 wrote:
> In ASCII, which gcc and lcc-win32 use,


This is a bit misleading, and not necessary to the rest of your message. gcc
will default to the character encoding of the host operating system, and
allows for this default to be overridden using command-line options.
 
Reply With Quote
 
Douglas A. Gwyn
Guest
Posts: n/a
 
      09-10-2007
Charlie Gordon wrote:
> ... most optimizing compilers will turn (0x61 + "fun" - 0x60)
> into ("fun" + 1), but is it strictly correct or is UB invoked ?


It violates a semantic requirement of the C standard.
 
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
Starting and stopping a prog. from another prog. andoni.oconchubhair@ie.fid-intl.com Java 1 10-22-2006 10:43 PM
Piping ping into perl-prog bernd wegener Perl 3 09-22-2004 06:32 PM
NetSaver : Cisco Router Configuration Archiver for windows (alpha stage prog.) NetSaver Cisco 0 08-06-2004 02:49 AM
Prog ID not found on MSSOAP John Bailo ASP .Net 1 04-15-2004 09:08 PM
beans and C code in java prog. Nishi Bhonsle Java 1 01-06-2004 07:50 PM



Advertisments