Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   questions about constants (http://www.velocityreviews.com/forums/t318323-questions-about-constants.html)

Kevin Bracey 05-20-2004 05:42 PM

Re: questions about constants
 
In message <nT5rc.2704$eH3.54819@news4.e.nsc.no>
Martin Johansen <mfag@online.no> wrote:

> Hello
>
> I was not able to find answers to there Qs on google or in the FAQ, so
> here goes
>
> Q1:
> I refer to the question 2.10 in the FAQ
> the following code compiled with gcc
> (struct {int a; char* b; int c;}){1, "1", 'A'}.c
> but is it valid ansi c?


It's valid in ISO C99, but not earlier versions.

> Q2:
> Are these equivalent?
> (long)1 contra 1L


Yes, for that specific example, barring syntax quirks (eg "sizeof (long)1"
will be parsed differently from "sizeof 1L").

This might not be:

(long)1000000000000 vs 1000000000000L

because the suffix only specifies a minimum size, and it will grow to
long long if necessary. The first form would truncate it back down to long,
even if it didn't fit.

> Q3:
> I know of the following "constant classifiers" i.e. 1L
> l and L, f and F, u and U
> but are there more?


C99 has "ll" or "LL" for long long.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/

Martin Dickopp 05-20-2004 06:01 PM

Re: questions about constants
 
Martin Johansen <mfag@online.no> writes:

> Q1:
> I refer to the question 2.10 in the FAQ
> the following code compiled with gcc
> (struct {int a; char* b; int c;}){1, "1", 'A'}.c
> but is it valid ansi c?


This is called a compound literal; it is valid according to the current
ISO C standard (C99). Note, however, that most compilers do not yet
implement C99, but C89, the previous C standard. (GCC implements C99
only partially.)

> Q2:
> Are these equivalent?
> (long)1 contra 1L


Yes.

> Q3:
> I know of the following "constant classifiers" i.e. 1L
> l and L, f and F, u and U
> but are there more?


For integers, there are the suffixes L (long), U (unsigned), and UL
(unsigned long), and in C99 LL (long long) and ULL (unsigned long long).
For floating-point numbers, there are F (float) and L (long double). All
suffixes can be spelled in uppercase or lowercase letters.

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/

Martin Johansen 05-21-2004 02:30 AM

questions about constants
 
Hello

I was not able to find answers to there Qs on google or in the FAQ, so
here goes

Q1:
I refer to the question 2.10 in the FAQ
the following code compiled with gcc
(struct {int a; char* b; int c;}){1, "1", 'A'}.c
but is it valid ansi c?

Q2:
Are these equivalent?
(long)1 contra 1L

Q3:
I know of the following "constant classifiers" i.e. 1L
l and L, f and F, u and U
but are there more?

Thank you!


All times are GMT. The time now is 10:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57