Yes, that's normal C behaviour. Only external and static storage is
guaranteed to be zero. In a modern environment it seems a little mean,
especially since you gave opt a partial initial value, but there are no
half-measures in C.

On Tue, 2 Apr 2019 at 01:27, Jeremy O'Brien <neut...@fastmail.com> wrote:

> On Mon, Apr 1, 2019, at 11:33, Kyohei Kadota wrote:
> > Hi, 9fans. I use 9legacy.
> >
> > About below program, I expected that flags field will initialize to
> > zero but the value of flags was a garbage, ex, "f8f7".
> > Is this expected?
> >
> > ```
> > #include <stdio.h>
> >
> > struct option {
> >     int n;
> >     char *s;
> >     int flags;
> > };
> >
> > int
> > main(void)
> > {
> >     struct option opt = {1, "test"};
> >     printf("%d %s %x\n", opt.n, opt.s, opt.flags);
> >     return 0;
> > }
> > ```
> >
> >
>
> According to C99: "If an object that has automatic storage duration is not
> initialized explicitly, its value is indeterminate."
>
> Stack variable == automatic storage duration. This appears to be correct
> behavior to me.
>
>

Reply via email to