2

In the code: ...

 #define MAXELEMNTS 100

 struct book
 {
     int number;
     char name[31];
     char author[31];
     int year;
     char publisher[31];
 };

 struct book bkStruct[MAXELEMENTS];

...

Are the integer fields (number and year) initialized to 0 by default when the other char fields are initialized but not these two? Or do they have god-knows-what value? In my experience they do have value = 0, but I am not sure is this general rule so I must be sure!

Best regards, Papo

admdrew
  • 3,790
  • 4
  • 27
  • 39
  • 2
    It depends, you did not provide enough information for an answer. – this May 05 '14 at 17:58
  • 4
    It depends on whether `bkStruct` has automatic or static storage duration. – Kerrek SB May 05 '14 at 18:00
  • Have a look at initialization of objects when using `new`. They also apply to initialization of objects on the stack. http://stackoverflow.com/a/620402/434551 – R Sahu May 05 '14 at 18:02
  • 1
    @RSahu: OP tagged `c` language, not `c++`. There is no `new` keyword in `c` language. – gangadhars May 05 '14 at 18:05

3 Answers3

3

In C there is no such thing as "partial initialization". If a struct is initialized by specifying a value for a single member, all the other members are automagically initialized (to 0 of the proper kind).

struct book
{
    int number;
    char name[31];
    char author[31];
    int year;
    char publisher[31];
};

struct book book1; // no initialization
struct book book2 = { .author = "pmg" }; // initialization of ALL of book2

struct book bkStruct1[MAXELEMENTS]; // uninitialized array
struct book bkStruct2[MAXELEMENTS] = {0}; // initialized array
                                          // (every member of every element)

Note: some implementations may complain about missing braces on the perfectly legal array initialization. That is a problem with those implementations.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • I agree that complaining about `{0}` is a problem. It's not a bug, though, or at least not a failure to conform to the standard -- as long as it's a warning and not a fatal error. – Keith Thompson May 05 '14 at 18:32
  • If `struct book book1;` occurs outside a function, is it not initialized with all bits zero? – chux - Reinstate Monica May 05 '14 at 19:02
  • 1
    @chux: It's initialized to *zero* (defined recursively down to `0` for integers, `0.0` for floating-point, and `NULL` for pointers), which may or may not be represented as all bits zero (though it very probably is). – Keith Thompson May 05 '14 at 19:05
3

If you define an object with an initializer, it's initialized to the specified value. If you only specify values for some members, the others are initialized to zero (meaning 0 for integers (including characters), 0.0 for floating-point values, and NULL for pointers).

If you define an object without an initializer, all members are implicitly initialized to zero if the object has static storage duration, i.e., if it's defined outside any function or if it's defined with the static keyword.

An object defined inside a function without the static keyword has automatic storage duration. Such objects, if not explicitly initialized, start off with garbage values. (If you happen to see such objects seemingly initialized to zero, remember that zero can be just as much a garbage value as anything else.)

You asked:

Are the integer fields (number and year) initialized to 0 by default when the other char fields are initialized but not these two?

but the code in your question doesn't initialize the char[] fields. See pmg's answer, which shows some good examples, but doesn't currently mention the static vs. automatic distinction.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
-1

to be on safe side you should intialize your varibales to 0 yourself depending upon compiler it might pick garbage value as well

Elijah
  • 306
  • 1
  • 12