what exactlly these values returns?

This is a discussion on what exactlly these values returns? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> struct s { }; struct s gs; int main() { struct s s1; printf("%u \n",gs); printf("%u \n",s1); printf("%u ...

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    what exactlly these values returns?

    Code:
    #include<stdio.h>
    struct s
    {
    
    };
    
    struct s gs; 
    
    int main()
    {
    struct s s1; 
    printf("%u \n",gs);
    printf("%u \n",s1);
    printf("%u \n",&gs);
    printf("%u \n",&s1);
    return 0;
    }
    output :3214811820
    3214811820
    134518176
    3214811652

    can any one give exactlly s1,gs,&s1,&gs returns?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What's wrong with the answer you got here:

    wt is output of s1, &s1

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    What are you doing? The first two printf's are meaningless garbage.
    The second two are the addresses that your empty structures are at.

    Who comes up with these questions?
    You can't just use non-existant data in an empty structure as if it were a standard type.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The first two are undefined because print expects an unsigned integer, and not a struct s. Whatever nonsensical output you get (if not a crash) is meaningless, because it isn't supposed to be "anything".
    &s1 and &gs returns the addresses in memory where the two instances of the struct are located. They should be printed using %p, not %u, however, since the later results in undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2008, 01:36 PM
  2. IRC-type logger with files based on three values
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 08-27-2008, 04:42 PM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. sizeof(array) returns differnt values for same array?
    By Diamonds in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2003, 03:27 PM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

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