Union -Need Ur Help

This is a discussion on Union -Need Ur Help within the C Programming forums, part of the General Programming Boards category; Can anyone explain how output from below program is 0.000000 2 somechar ? <Eventhough they are not initialised>> Code: #include<stdio.h> ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up Union -Need Ur Help

    Can anyone explain how output from below program is

    0.000000 2 somechar

    ?

    <Eventhough they are not initialised>>

    Code:
    #include<stdio.h>
    
    union uu{
    char aa;
    int a;
    float b;
    };
    
    int main()
    {
    union uu u1;
    
    printf("val:%f %d %c",u1.b ,u1.a,u1.aa);
    
    getchar();
    return 0;
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you get from an uninitialized variable is undefined, so you may well get what you describe, but it could also come out with just about any other value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    I checked in Dev-c ..

    I am getting Same Answer everytime i am executing...

    I think it wont be garbage Value... if it is so, my Output have to change everytime...

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by ganesh bala View Post
    I checked in Dev-c ..

    I am getting Same Answer everytime i am executing...

    I think it wont be garbage Value... if it is so, my Output have to change everytime...
    Who said that?

    If I every day in the morning take one cup of tee - my garbage collection will store every day the same - one empty tee bag and two used sugar bags...

    To see some other garbage - I need to do something else - compile your application with another compiler for example, or link it with another run-time library
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ganesh bala View Post
    I checked in Dev-c ..

    I am getting Same Answer everytime i am executing...

    I think it wont be garbage Value... if it is so, my Output have to change everytime...
    Yes, it's going to stay the same each time you run it, if nothing else changes. Change something in the environment of the program (pass arguments on the command line, compile with different optioons, add another variable, etc, etc) and it may well change.

    It is predictable for a particular build, under the same circumstances, because the storage on the stack will contain the same content, as the code that executes before main will have taken the same path [unless some function is doing something that depends on time-of-day, date, random numbers or something else that changes between one run and another].

    It is after all undefined behaviour - it does not mean that it won't do the same thing every time - it just doesn't GUARANTEE that it will behave in any particular way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks for ur Explanation...

    I got it ...

    i just added few variables and i run it again...

    Output was changed this time......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 08:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21