comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Embedded systems programmers worldwide earn failing grades in C
Date: Mon, 25 Jan 2010 15:22:24 -0800
Date: 2010-01-25T15:22:24-08:00	[thread overview]
Message-ID: <lnr5pdzs5r.fsf@nuthaus.mib.org> (raw)
In-Reply-To: 5pppl5lm5h1eerr6aesru2n9poois8vesc@4ax.com

Brian Drummond <brian_drummond@btconnect.com> writes:
[...]
> Q: Which of the following is the most portable way to declare an Ada
> (which has no preprocessor) constant for the number of seconds in a
> (non-leap) calendar year?
>
> (a) Seconds_Per_Year : constant natural := 60 * 60 * 24 * 365;
> (b) no preprocessor traps from textual substitution, so no ( ) subtleties
> (c) literals of type Universal Integer, so no explicit size qualifiers
> (d) all of the above are true, why make it more complex than (a)?

(b), (c), and (d) aren't really choices.  But the correct answer
is (e) none of the above.  Since Natural'Last isn't required
to be bigger than 32767, and 60 * 60 * 24 * 365 is 31536000,
your declaration of Seconds_Per_Year is illegal on some systems.
(Where it's legal, you probably won't get a warning from the
compiler, which is a pity.)

A better answer IMHO is:

    Seconds_Per_Year : constant := 60 * 60 * 24 * 365;

Why specify subtype Natural if you don't need to?

As I recall, the corresponding C question presented a similar issue.
Ada does have a better solution, but only if you use it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <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	other threads:[~2010-01-25 23:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-20 10:50 Embedded systems programmers worldwide earn failing grades in C alexandru.chircu
2010-01-20 16:46 ` Alex R. Mosteo
2010-01-20 20:48   ` Keith Thompson
2010-01-21 10:09     ` Alex R. Mosteo
2010-01-21 21:54       ` Keith Thompson
2010-01-25  1:32   ` Brian Drummond
2010-01-25 23:22     ` Keith Thompson [this message]
2010-01-26  0:38       ` Brian Drummond
2010-01-26 14:09         ` stefan-lucks
replies disabled

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