Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:21:29+00:00 2026-05-27T22:21:29+00:00

If struct a a1 = {0}; initializes all the elements (of different types) of

  • 0

If struct a a1 = {0}; initializes all the elements (of different types) of a structure to zero, then struct a a2 = {5}; should initialize it to 5.. no?

#include <stdio.h>

typedef struct _a {
    int i;
    int j;
    int k;
}a;

int main(void)
{
    a a0;
    a a1 = {0};
    a a2 = {5};

    printf("a0.i = %d \n", a0.i);
    printf("a0.j = %d \n", a0.j);
    printf("a0.k = %d \n", a0.k);

    printf("a1.i = %d \n", a1.i);
    printf("a1.j = %d \n", a1.j);
    printf("a1.k = %d \n", a1.k);

    printf("a2.i = %d \n", a2.i);
    printf("a2.j = %d \n", a2.j);
    printf("a2.k = %d \n", a2.k);

    return 0;
}

The uninitialized struct contains garbage values

a0.i = 134513937
a0.j = 134513456
a0.k = 0 

The initialized to 0 struct contains all elements initialized to 0

a1.i = 0 
a1.j = 0 
a1.k = 0 

The initialized to 5 struct contains only the first element initialized to 5 and the rest of the elements initialized to 0.

a2.i = 5 
a2.j = 0 
a2.k = 0

Would a2.j and a2.k always guaranteed to initialize to 0 during a a2 = {5}; (or) is it an undefined behavior

OTOH, why am I not seeing all the elements of s2 initialized to 5. How is the struct initialization is done during {0} and how is it different when {5} is used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:21:30+00:00Added an answer on May 27, 2026 at 10:21 pm

    Reference:

    C99 Standard 6.7.8.21

    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

    [EDIT]

    Static objects and implicit initialization:

    The storage duration of an object determines the lifetime of an object.
    There are 3 storage durations:
    static, automatic, and allocated

    variables declared outside of all blocks and those explicitly declared with the static storage class specifier have static storage duration. Static variables are initialized to zero by default by the compiler.

    Consider the following program:

    #include<stdio.h>
    
    int main()
    {
        int i;
        static int j;
        printf("i = [%d]",i);
        printf("j = [%d]",j);
    
        return 0;
    }
    

    In the above program, i has automatic storage and since it is not explicitly initialized its value is Undefined.
    While j has static storage duration and it is guaranteed to be initialized to 0 by the compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Duplicate: Why Must I Initialize All Fields in my C# struct with a Non-Default
I have a struct that I initialize like this: typedef struct { word w;
Code: struct IRenderingEngine { virtual void Initialize(int width, int height) = 0; virtual void
I've got a structure as follows: typedef struct { std::wstring DevAgentVersion; std::wstring SerialNumber; }
I have a class: class systemcall { typedef struct { int pid; int fptrCntr;
I want to initialize a struct element, split in declaration and initialization. This is
struct elem { int i; char k; }; elem user; // compile error! struct
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
struct { char a; int b; } x; Why would one define a struct
struct some_struct{ int a; }; some_struct n = {}; n.a will be 0 after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.