By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

struct in C99

P: n/a
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
....

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.

Cheers,

Paulo Matos

Jun 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
po******@gmail.com schrieb:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
Note: gcc still is not entirely a C99 compiler, see
http://gcc.gnu.org/C99status.html
$ gcc -Wall -std=c99 timespec.c
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;
Note: Neither <stdio.h> nor <time.h> is guaranteed to define the
type struct timespec. This type _probably_ comes from your
<sys/types.h> and seems to be not excluded properly for other
modes invoked by the std option.
In short: The compiler gets it right for -std=c99 but should
get it right for -std=c89 as well...
It may be necessary to
#include <sys/types.h>
or to
#define FOO
where FOO governs whether certain parts or the whole of
sys/types.h are processed/included. This is implementation
specific and thus off-topic round here.

It depends on the gcc and glib versions in use, so you have
to ask in a newsgroup dealing with either gcc or your OS to get
a complete/good answer.


printf("Doh!\n");

return 0;

}

Can someone please explain why? This doesn't happen in previous
versions of C.


Note that gcc by default compiles with -std=gnu89 which is C90
plus GNU extensions -- this language and its library contain
stuff plain standard C (in the C90, C95, or C99 variety) does
not have.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 12 '06 #2

P: n/a


po******@gmail.com wrote On 06/12/06 15:29,:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.


There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.

--
Er*********@sun.com

Jun 12 '06 #3

P: n/a
po******@gmail.com writes:
Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.


The type "struct timespec" is defined by POSIX, not by the C standard.
In (non-POSIX) standard C, a user program is allowed to declare its
own "struct timespec", so it's not allowed to declare it in <time.h>.
You need to tell your compiler to compile in some sort of
POSIX-specific mode. I don't know how to do that. Try a Google
search, or gnu.gcc.help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #4

P: n/a

po******@gmail.com wrote:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}


It simply means that "struct timespec" is an incomplete type... Which
simply means that there has not been any definition of this structure.
Since it is a non-standard structure, this result was expectable.

Jun 12 '06 #5

P: n/a

Michael Mair escreveu:>
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.


Why is the -O?

Jun 12 '06 #6

P: n/a

Eric Sosman escreveu:

There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.

Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.
--
Er*********@sun.com


Jun 12 '06 #7

P: n/a
po******@gmail.com writes:
Michael Mair escreveu:>
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.


Why is the -O?


I presume you know, or can easily find out from the documentation,
that "-O" enables optimization.

A compiler typically has to collect more information about the program
it's compiling in order to perform certain optimizations. For
example, given:

{
int x, y;
...
x = 10;
...
y = x;
}

If the compiler can prove that x is not modified before its value is
assigned to y, it can replace "y = x;" with the equivalent of "y = 10;".
This is called "dataflow analysis".

In the course of collecting this information, it can detect certain
kinds of programming errors, such as using a variable that hasn't been
initialized. So enabling optimization *and* warnings can often give
you more information about errors in your code than just enabling
warnings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #8

P: n/a
On 12 Jun 2006 16:45:30 -0700
po******@gmail.com wrote:
Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.

If you use functions that are not from c99 standard, then your code is
not c99 compilant. You can, of course, create the function you want, and
that includes functions that are not part of c99 standard, and you have
to have a definition for such functions.
Jun 13 '06 #9

P: n/a
po******@gmail.com schrieb:
Eric Sosman escreveu:
There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.


Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!


<OT>
You are probably looking for "-std=gnu99".
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 13 '06 #10

P: n/a

po******@gmail.com wrote:
Eric Sosman escreveu:

There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.


Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

No, it is a flag which allows the GNU compiler to be fully C99
compliant, with extensions.
It allows any piece of strictly conforming code to compile (well, at
this time, the GNU compiler doesn't fully implement C99, but in a near
future, it will).
But these extensions mustn't interact badly with strictly conforming
code.
And a strictly conforming code can write its own "struct timespec".
If this non-standard extension was put in the language, a strictly
conforming program, such as:
#include <time.h>
#include <stdio.h>

struct timespec {int a,b;};
int main() {
struct timespec a, b;
a.a=0;a.b=8;
printf("Doh!\n");
return 0;
}

Would not compile, saying that timespec is defined twice.

It doesn't mean that this extension is inexistant; Read the
documentation.

There is perhaps a __timespec (names starting with double underscores
are reserved for compiler extensions and this kind of stuff).
Or there is perhaps a header to include : Perhaps <timespec.h>

The gnu99 mode is "almost" ISO compliant but contains a few core or
library extensions which doesn't permit the compilation of a few
strictly conforming well-formed programs.

Anyway, -std=c99 doesn't require your program to be conforming... You
can still use a lot of non-standard extensions, but not extensions
which change the meaning of strictly-conforming programs.
With -std=c99, the compiler itself is ISO-compliant (as far as
possible).

If you want to reduce the number of non-standard extensions used in
your program, then you should use the "--pedantic-errors" compiler
flag... Otherwise your program might contain several non-standard
extensions.

For instance, this piece of code:

int main() {
int i=42;
int u[i];
}

Compiles with "-xc -std=c89"
While it doesn't compile (complaining that VLA are forbidden) with "-xc
-std=c89 --pedantic-errors".

Jun 13 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.