questions about constants

K

Kevin Bracey

In message <[email protected]>
Martin Johansen said:
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.
 
M

Martin Dickopp

Martin Johansen said:
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
 
M

Martin Johansen

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top