Thread: Crazy question

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    6

    Crazy question

    I really don't understand why it prints out
    0x55 when running these two files. Can someone please explain??
    Thank you.

    Code:
    /*bar6.c*/
    void p2(void);
    
    int main(){
        p2();
        return 0;
    }
    
    /*foo6.c*/
    #include <stdio.h>
    char main;
    void p2(){
        printf ("0x%x\n", main);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    main is an uninitialized variable. Were you to change it to any other name, you'd still get unexpected results. (Unexpected by you, not me.) Since you haven't assigned any value to main, it contains some random value.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    Quote Originally Posted by quzah
    main is an uninitialized variable. Were you to change it to any other name, you'd still get unexpected results. (Unexpected by you, not me.) Since you haven't assigned any value to main, it contains some random value.

    Quzah.

    No matter how many time you run, you will get 0x55
    It's not random.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't generate a random number (as per your calling rand). We say it's random because it contains some arbitrary value of which you have not specified, and is out of your control.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    I think you call it a garbage value.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In this case main is a global variable and would therefore(if it was legal) be initialized to zero at program startup. Your "program" appears to have "confused" the linker and your output is likely the address (or part thereof) of the main() function. In any case, don't use a global variable with the name main (or any other function name).

    [edit]It is also possible that it is the value of the first byte of the main function.[/edit]
    Last edited by anonytmouse; 10-24-2004 at 11:48 PM.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    Oh actually, I just needed to find out where it gets the garbage information unsigned 85 or 55 hex value from.
    The question is to find out why it would print out 0x55 not where the problem is.. Sorry if I confused the issue.

    Thank you.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The 55H value is garbage. There's no rhyme or reason to it. If you ran the same program on a different computer or even after a reboot or running some other applications you'd probably get a different number.

    When you declare a variable the compiler just sets some space aside for you to store in it. It doesn't clear that memory so whatever just happened to be in that memory location when you started the program is what's going to be printed.

    Are you trying to print the address of the main() function? Try this instead:
    Code:
    itsme@dreams:~/C$ cat file1.c
    #include <stdio.h>
    
    extern void p2(void);
    
    int main(void)
    {
      p2();
      return 0;
    }
    itsme@dreams:~/C$ cat file2.c
    #include <stdio.h>
    
    extern int main(void);
    
    void p2(void)
    {
      printf("%p\n", main);
    }
    itsme@dreams:~/C$ gcc -Wall file1.c file2.c -o file
    itsme@dreams:~/C$ ./file
    0x80483f0
    itsme@dreams:~/C$
    If you understand what you're doing, you're not learning anything.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anonytmouse
    In this case main is a global variable and would therefore(if it was legal) be initialized to zero at program startup.
    You're wrong there. Only static variables are initialized (to zero). Automatic variables are not initialized.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    Quote Originally Posted by itsme86
    The 55H value is garbage. There's no rhyme or reason to it. If you ran the same program on a different computer or even after a reboot or running some other applications you'd probably get a different number.

    When you declare a variable the compiler just sets some space aside for you to store in it. It doesn't clear that memory so whatever just happened to be in that memory location when you started the program is what's going to be printed.

    Are you trying to print the address of the main() function? Try this instead:
    Code:
    itsme@dreams:~/C$ cat file1.c
    #include <stdio.h>
    
    extern void p2(void);
    
    int main(void)
    {
      p2();
      return 0;
    }
    itsme@dreams:~/C$ cat file2.c
    #include <stdio.h>
    
    extern int main(void);
    
    void p2(void)
    {
      printf("%p\n", main);
    }
    itsme@dreams:~/C$ gcc -Wall file1.c file2.c -o file
    itsme@dreams:~/C$ ./file
    0x80483f0
    itsme@dreams:~/C$

    No I am not trying to print out the address. I need to know WHY how it gets its value 55H. I've tried on several machines already including Windows, Linux, Solaris and they all seem to produce the same 0x55. There has to be a reason why it does.
    Thanks.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you ever listen? It's a garbage value. It doesn't matter what it gives you, because it's your fault that it gives you some crap.

    Try initializing the variable. Then what does it give you?
    Code:
    char main = 0;
    Try changing the name of the variable, and initializing it:
    Code:
    char foo = 0;
    There, now you've debugged your own program. Congradulations. No, wait, that was me.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    We've told you. It gives you that value because that's whatever used that memory location last put there. It's garbage.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Look at all the garbage you can make!

    Code:
    itsme@dreams:~/C$ cat garbage.c
    #include <stdio.h>
    
    int main(void)
    {
      unsigned char a, b, c, d, e, f;
    
      printf("%x %x %x %x %x %x\n", a, b, c, d, e, f);
      return 0;
    }
    itsme@dreams:~/C$ ./garbage
    bf ff f9 58 40 0
    It's amazing really.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    Quote Originally Posted by quzah
    Do you ever listen? It's a garbage value. It doesn't matter what it gives you, because it's your fault that it gives you some crap.

    Try initializing the variable. Then what does it give you?
    Code:
    char main = 0;
    Try changing the name of the variable, and initializing it:
    Code:
    char foo = 0;
    There, now you've debugged your own program. Congradulations. No, wait, that was me.

    Quzah.
    Okay hold on a second here. I am not asking you guys to find the bug here. I am ASKING you guy where in the code grabs the garbage value of 0x55. The question is it must not be initialized and the code is perfectly legal. My problem is HOW it gets 0x55.
    I really didn't mean to cause any confusion here.
    Thank you.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My problem is HOW it gets 0x55.
    Asked and answered.

    But one more time, the answer is: "Because it feels like it."

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM