Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is it possible?

Reply
Thread Tools

is it possible?

 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-01-2008
On Mon, 01 Sep 2008 19:07:48 +0200, August Karlstrom
<(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>> On Sep 1, 1:45 pm, August Karlstrom <(E-Mail Removed)> wrote:
>>> #define ____ (printf("welcome ") && 0)

>>
>> Identifiers starting with two underscores are reserved for the
>> implementation,

>
>Then the problem may have no solution, depending on how you interpret
>the question from the original poster.
>
>> your code invokes undefined behavior.

>
>Which additional options do GCC need in order to tell me this?


The compiler is not required to diagnose undefined behavior. In this
case it might not be possible.

>
>Terminal session:
>
>$ cat test.c
>#include <stdio.h>
>
>#define ____ (printf("welcome ") && 0)


If this line of code was the last line in stdio.h instead of your
source file, the input to the compiler after the preprocessor was
finished would be the same. How is the compiler to distinguish
between your case which is undefined behavior and my case which is
not?

>
>int main(void)
>{
> if (____)
> printf("welcome");
> else
> printf("to C world");
> puts("");
>
> return 0;
>}
>
>$ gcc -ansi -pedantic -Wall test.c
>$ ./a.out
>welcome to C world
>
>
>August


--
Remove del for email
 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      09-01-2008
On Mon, 01 Sep 2008 12:14:44 -0700, Barry Schwarz wrote:
> On Mon, 01 Sep 2008 19:07:48 +0200, August Karlstrom
> <(E-Mail Removed)> wrote:
>>(E-Mail Removed) wrote:
>>> On Sep 1, 1:45 pm, August Karlstrom <(E-Mail Removed)> wrote:
>>>> #define ____ (printf("welcome ") && 0)
>>>
>>> Identifiers starting with two underscores are reserved for the
>>> implementation,

>>
>>Then the problem may have no solution, depending on how you interpret
>>the question from the original poster.
>>
>>> your code invokes undefined behavior.

>>
>>Which additional options do GCC need in order to tell me this?

>
> The compiler is not required to diagnose undefined behavior. In this
> case it might not be possible.
>
>>Terminal session:
>>
>>$ cat test.c
>>#include <stdio.h>
>>
>>#define ____ (printf("welcome ") && 0)

>
> If this line of code was the last line in stdio.h instead of your source
> file, the input to the compiler after the preprocessor was finished
> would be the same.


Not with GCC's preprocessor.

> How is the compiler to distinguish between your case
> which is undefined behavior and my case which is not?


Even without the preprocessor extensions that allow the compiler to
determine whether the preprocessed output comes from a system header, if
the compiler would give an error on ____, the system headers can be
written in a way that does not use ____.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-01-2008
August Karlstrom <(E-Mail Removed)> writes:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> On Sep 1, 1:45 pm, August Karlstrom <(E-Mail Removed)> wrote:
>>> #define ____ (printf("welcome ") && 0)

>> Identifiers starting with two underscores are reserved for the
>> implementation,

>
> Then the problem may have no solution, depending on how you interpret
> the question from the original poster.
>
>> your code invokes undefined behavior.

>
> Which additional options do GCC need in order to tell me this?

[...]

There may well not be any such options. If the behavior is undefined,
then silently accepting the code is perfectly acceptable; gcc is under
no obligation to do anything else. (If there is such an option, you
can find out about it in the gcc documentation or by asking in
gnu.gcc.help.)

In any case, I think the intent was that ___ was placeholder, not an
identifier to be defined as a macro.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
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
 
      09-01-2008
viza <(E-Mail Removed)> writes:
> On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
>> please forgive me if it is a stupid question because i dont understand
>> I saw a question in C -
>>
>> if (____)
>> printf("welcome");
>> else
>> printf("to C world");
>>
>> what should be the condition in if(___) for following output
>>
>> welcome to C world

>
> You have had several answers, all correct, but none very useful, as is to
> be expected in comp.lang.c.


No useful answer is possible.

> In answer to your question, it is not possible make both the if and the
> else happen, without using goto or something like that, which can get
> confusing.


The problem statement didn't ask for a way to make both the if and the
else happen; it asked for a way to make the program produce the output
"welcome to C world". The printf("welcome") call was a deliberate red
herring.

[...]

A sufficiently good and clever C programmer should be able to solve
the problem as stated. Note that "good" and "clever" are not
synonymous. A sufficiently clever programmer might use such a trick
in production code; a good programmer never will.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-02-2008
On 1 Sep, 07:27, Tinku <(E-Mail Removed)> wrote:
> please forgive me if it is a stupid question because i dont
> understand
> I saw a question in C -
>
> if (____)
> printf("welcome");
> else
> printf("to C world");
>
> what should be the condition in if(___) for following output
>
> * * * * * welcome to C world


> if (!printf("welcome "))
> printf("welcome");
> else
> printf("to C world");


--
Nick Keighley





 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-02-2008
August Karlstrom wrote:
> (E-Mail Removed) wrote:
>> On Sep 1, 1:45 pm, August Karlstrom <(E-Mail Removed)> wrote:
>>> #define ____ (printf("welcome ") && 0)

>>
>> Identifiers starting with two underscores are reserved for the
>> implementation,

>
> Then the problem may have no solution, depending on how you interpret
> the question from the original poster.


I don't see an interpretation that would make me think that use of
#define was called for, much less that it was mandatory. It seemed clear
to me that the underscores were meant to indicate a blank that was to be
filled in, not a C identifier which was to be #defined.

>> your code invokes undefined behavior.

>
> Which additional options do GCC need in order to tell me this?


As far as I know, there is no GCC option which identifies this problem.
Since the behavior is undefined, diagnosis is not required, so this does
not prevent gcc from conforming.

The reason why certain identifiers are reserved for the implementation
is so that that implementation can give those identifiers an
implementation-specific meaning, one that code written by knowledgeable
users might, in some cases, take advantage of. As a result, it might not
even make any sense for the compiler to diagnose it.

I think that a good compiler should diagnose any use of any reserved
identifier that it doesn't attach it's own special meaning to, but
that's a matter of QoI, and I'm not sure whether any real compiler does
this.
 
Reply With Quote
 
Mohamad Room
Guest
Posts: n/a
 
      09-02-2008

"Tinku" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> please forgive me if it is a stupid question because i dont
> understand
> I saw a question in C -
>
> if (____)
> printf("welcome");
> else
> printf("to C world");
>
> what should be the condition in if(___) for following output
>
> welcome to C world
>
>
>
>

if(printf("welcome ") = = 7)
printf("welcome");
else
printf("to C world");


function printf() returns the number of characters printed.
In this case the printf("welcome ") will evalute first and the result is
false ( it should return in order to evalute the statement in else part.



 
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




Advertisments