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

  • Home
  • SEARCH
  • 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 6699053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:00+00:00 2026-05-26T06:39:00+00:00

Please let us consider following code: #include <iostream> using namespace std; union{ int i;

  • 0

Please let us consider following code:

#include <iostream>
using namespace std;

union{
 int i;
}u;

int main(){

     int k=5;
     cout<<k+u.i<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

This code shows me output 5,what means to me is that,variable i in union structure has default value=0, but the same code on ideone.com shows warning like this

prog.cpp:6: warning: non-local variable ‘<anonymous union> u’ uses anonymous type and then prints  5 as well, and last one  core of this problem comes  from algorithm calculate  

Reciprocal of the square root and here is code

#include<iostream>
#include<math.h>
using namespace std;

float invsqrt(float x){

    float xhalf=0.5f*x;

    union{
         float x;
         int i;
    }u;

   u.x=x;
   u.i=0x5f3759df-(u.i>>1);
   x=u.x*(1.5f-xhalf*u.x*u.x);

   return x;
}

int main(){

    float  x=234;
    cout<<invsqrt(x)<<endl;

    return 0;
}

It shows me output also,but my question is that is it a this code good?i meant that because int i is not initailized ,can any compiler consider it’s value as zero?i am curious and please tell me something about this,also if something is not clear from my question say me,i am not English native speaker.

  • 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-26T06:39:01+00:00Added an answer on May 26, 2026 at 6:39 am

    Does a union always have default value of zero?

    The language standard says this:

    If an object that has static or thread storage duration is not initialized
    explicitly, then:

    • if it has pointer type, it is initialized to a null pointer;
    • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • if it is an aggregate, every member is initialized (recursively) according to these rules,
      and any padding is initialized to zero bits;
    • if it is a union, the first named member is initialized (recursively) according to these
      rules, and any padding is initialized to zero bits;

    So, in your first code sample, u.i will be initialised to zero.

    I’m not sure about the second code sample at all. I cannot see the point of the union there. I rather suspect that you meant to use a struct rather than a union. But note that the two code examples are very different because the union in this first has static storage duration and in the second the union has automatic storage duration. This results in completely different semantics for uninitialized variables.

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

Sidebar

Related Questions

Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Consider the following table: People FirstName nvarchar(50) LastName nvarchar(50) Let's assume for the moment
Please consider the following function body: var isValidated = true; $(selector1).each(function(){ //do validation with
i am using vsto, to work with excel-2007... let's consider i have a matrix
Let's consider the following table models for sqlalchemy in python. class Worker(Base): id Column(Integer,
Consider, for example, the following code: class ViewHelpersTest < ActionView::TestCase should 'generate tag with
Please let me know which one is good Consider I have a class like
Please consider the following sample, consisting of a definition of a nested XElement and
Consider the following comma separated file. For simplicity let it contain one line: 'I

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.