Thread: output....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    output....

    I came across this question. But still i am not able to understand why the output tends to come to 64.

    [insert]
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    void main()
    {
    
    	int a=320;
    	char *ptr;
    	ptr=(char *)&a;
    	printf("%d ",*ptr);
    	getch();
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't be too concerned about where a cast of your computers first variable's memory location, might be. The latter will obviously vary from system to system.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Adak View Post
    I wouldn't be too concerned about where a cast of your computers first variable's memory location, might be. The latter will obviously vary from system to system.
    Sorry , but i didnt get what you are trying to convey?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So in binary a = 101000000. You set your ptr to point at a, and since you are presumably on some little-endian machine, you get the least significant byte of a, which is 0100 0000 = 64.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by roaan View Post
    I came across this question. But still i am not able to understand why the output tends to come to 64.

    [insert]
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    void main()
    {
    
        int a=320;
        char *ptr;
        ptr=(char *)&a;
        printf("%d ",*ptr);
        getch();
    
    }
    You're just printing the value of the first byte of the integer - had you been running the program on a big-endian machine the output would have been 0. In other words, (assuming 32 bit ints on your system) the value of 'a' in hex is 00 00 01 40, and since it's stored in little-endian format the first byte is 0x40 (64 decimal).

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    thanks i understood in now but then this is os specific as in some other machine it might as well be 0 (if it were to be big endian)

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    thanks i understood in now but then this is os specific as in some other machine it might as well be 0 (if it were to be big endian)
    Endianness is not OS specific, it's architecture specific.

    I think Adak made that comment about the OS because he mistakenly thought you were looking at the pointer's memory address.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM