comp.lang.ada
 help / color / mirror / Atom feed
* YACT (Yet Another C Trap)
@ 2010-08-21  9:32 mockturtle
  2010-08-27  4:13 ` Shark8
  2010-08-29 20:55 ` Maciej Sobczak
  0 siblings, 2 replies; 7+ messages in thread
From: mockturtle @ 2010-08-21  9:32 UTC (permalink / raw)


Found in a comment by a Swedish guy (they use "," in float, not ".")
in
http://embeddedgurus.com/stack-overflow/2010/08/a-foreign-perspective-on-variable-names/


int x = 10,000; // oops, it will compile, perhaps even without
warnings. Happy debugging!



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-21  9:32 YACT (Yet Another C Trap) mockturtle
@ 2010-08-27  4:13 ` Shark8
  2010-08-29 20:55 ` Maciej Sobczak
  1 sibling, 0 replies; 7+ messages in thread
From: Shark8 @ 2010-08-27  4:13 UTC (permalink / raw)


On Aug 21, 3:32 am, mockturtle <framefri...@gmail.com> wrote:
> Found in a comment by a Swedish guy (they use "," in float, not ".")
> inhttp://embeddedgurus.com/stack-overflow/2010/08/a-foreign-perspective...
>
> int x = 10,000; // oops, it will compile, perhaps even without
> warnings. Happy debugging!

Ouch... just ouch.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-21  9:32 YACT (Yet Another C Trap) mockturtle
  2010-08-27  4:13 ` Shark8
@ 2010-08-29 20:55 ` Maciej Sobczak
  2010-08-30  8:21   ` stefan-lucks
  1 sibling, 1 reply; 7+ messages in thread
From: Maciej Sobczak @ 2010-08-29 20:55 UTC (permalink / raw)


On 21 Sie, 11:32, mockturtle <framefri...@gmail.com> wrote:

> int x = 10,000; // oops, it will compile, perhaps even without
> warnings. Happy debugging!

Did you actually tried or are just repeating unchecked nonsense?
The above example is explicitly handled (forbidden) by both C and C++
standards - comma has special meaning in declarators.

--
Maciej Sobczak * http://www.inspirel.com

YAMI4 - Messaging Solution for Distributed Systems
http://www.inspirel.com/yami4



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-30  8:21   ` stefan-lucks
@ 2010-08-30  7:52     ` Maciej Sobczak
  2010-08-30  8:28     ` stefan-lucks
  1 sibling, 0 replies; 7+ messages in thread
From: Maciej Sobczak @ 2010-08-30  7:52 UTC (permalink / raw)


On 30 Sie, 10:21, stefan-lu...@see-the.signature wrote:

> I tried it out. The above example gives an error. But the following
> program compiles without warnings with gcc:
>
> #include <stdio.h>
> int main (int argc, char **argv)
> { float x;
>
>     x = 12,3;

> Is this a language flaw of C, or a bug in gcc?

It is a language flaw for two reasons: one is that the language should
not allow you to initialize floating-point values with integers and
the other is that the comma operator for fundamental types is nonsense
[*]. :-)

[*] But there are quite nice ways to use it for custom types.

In any case, the above code violates the coding standard that
discourages the use of unnamed numeric constants. Constants should be
named (with notable exception of 0 and 1), which would bring you back
to compile-time error. You might still argue that the need for such a
coding standard indicates a flaw in the language anyway.

Note that the above problem has many more interesting implications,
especially when combined with other flaws:

float f = 10.0f;

f = f * 1,5;

Go figure what it does (and I can bet that your guess will be
wrong). ;-)

--
Maciej Sobczak * http://www.inspirel.com

YAMI4 - Messaging Solution for Distributed Systems
http://www.inspirel.com/yami4



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-29 20:55 ` Maciej Sobczak
@ 2010-08-30  8:21   ` stefan-lucks
  2010-08-30  7:52     ` Maciej Sobczak
  2010-08-30  8:28     ` stefan-lucks
  0 siblings, 2 replies; 7+ messages in thread
From: stefan-lucks @ 2010-08-30  8:21 UTC (permalink / raw)


On Sun, 29 Aug 2010, Maciej Sobczak wrote:

> > int x = 10,000; // oops, it will compile, perhaps even without
> > warnings. Happy debugging!
> 
> Did you actually tried or are just repeating unchecked nonsense?
> The above example is explicitly handled (forbidden) by both C and C++
> standards - comma has special meaning in declarators.

I tried it out. The above example gives an error. But the following 
program compiles without warnings with gcc:

#include <stdio.h>
int main (int argc, char **argv)
{ float x;

    x = 12,3;

    if (x > 12.1) printf("greater"); else printf("smaller or equal");
  printf(" than 12.1.\n");
}

Here is what it does:

> gcc flaw.c    
> ./a.out
smaller or equal than 12.1.

Is this a language flaw of C, or a bug in gcc?


-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-30  8:21   ` stefan-lucks
  2010-08-30  7:52     ` Maciej Sobczak
@ 2010-08-30  8:28     ` stefan-lucks
  2010-08-30 10:13       ` Peter C. Chapin
  1 sibling, 1 reply; 7+ messages in thread
From: stefan-lucks @ 2010-08-30  8:28 UTC (permalink / raw)


On Mon, 30 Aug 2010, stefan-lucks@see-the.signature wrote:

> #include <stdio.h>
> int main (int argc, char **argv)
> { float x;
> 
>     x = 12,3;
> 
>     if (x > 12.1) printf("greater"); else printf("smaller or equal");
>   printf(" than 12.1.\n");
> }
> 
> Here is what it does:
> 
> > gcc flaw.c    
> > ./a.out
> smaller or equal than 12.1.

P.S.: Even gcc -Wall doesn't warn about the flawed ",". It just tells me 
"control reaches end of non-void function" (my main function should return 
an integer, but it doesn't).

> gcc -Wall flaw.c
flaw.c: In function 'main':
flaw.c:9: warning: control reaches end of non-void function

> Is this a language flaw of C, or a bug in gcc?

-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: YACT (Yet Another C Trap)
  2010-08-30  8:28     ` stefan-lucks
@ 2010-08-30 10:13       ` Peter C. Chapin
  0 siblings, 0 replies; 7+ messages in thread
From: Peter C. Chapin @ 2010-08-30 10:13 UTC (permalink / raw)


On 2010-08-30 04:28, stefan-lucks@see-the.signature wrote:

> P.S.: Even gcc -Wall doesn't warn about the flawed ",". It just tells me 
> "control reaches end of non-void function" (my main function should return 
> an integer, but it doesn't).

In the expression statement

	x = 12,3;

first 12 is assigned to x and then the expression '3' is evaluated, the
result of which is ignored. It has a similar effect to

	x = 12;
	3;

except that it is one statement instead of two. The key point here is
that the comma operator has lower precedence than even assignment. In C
a statement can just be an expression followed by a semicolon. Since it
is legal to write expressions that have no side effects you can do
things like

	a + b;

Given your original code ('x = 12,3') the Open Watcom C compiler issues
the warning, "Expression is only useful for its side effects." It's a
strange warning in some ways since the problem here is that the
expression in question ('3') has no side effects.

Ada avoids these problems because in Ada you can't just put a semicolon
at the end of an expression to make a statement (assignment isn't an
operator in Ada).

Peter



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-08-30 10:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-21  9:32 YACT (Yet Another C Trap) mockturtle
2010-08-27  4:13 ` Shark8
2010-08-29 20:55 ` Maciej Sobczak
2010-08-30  8:21   ` stefan-lucks
2010-08-30  7:52     ` Maciej Sobczak
2010-08-30  8:28     ` stefan-lucks
2010-08-30 10:13       ` Peter C. Chapin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox