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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:27:27+00:00 2026-06-14T08:27:27+00:00

int i=512; char *c = (char *)&i; c[0] =1; printf(%d,i); this displays 513, it

  • 0
int i=512;
char *c = (char *)&i;
c[0] =1;
printf("%d",i);

this displays “513”, it adds 1 to i.

int i=512;
char *c = (char *)&i;
c[1] =1;
printf("%d",i);

whereas this displays 256. Divides it by 2.
Can someone please explain why? thanks a lot

  • 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-06-14T08:27:28+00:00Added an answer on June 14, 2026 at 8:27 am

    Binary

    The 32-bit number 512 expressed in binary, is just:

    00000000000000000000001000000000
    

    because 2 to the power of 9 is 512. Conventionally, you read the bits from right-to-left.

    Here are some other decimal numbers in binary:

    0001 = 1
    0010 = 2
    0011 = 3
    0100 = 4
    

    The Cast: Reinterpreting the Int as an Array of Bytes

    When you do this:

    int i = 512;
    char *c = (char *)&i;
    

    you are interpreting the 4-byte integer as an array of characters (8-bit bytes), as you probably know. If not, here’s what’s going on:

    &i
    

    takes the address of the variable i.

    (char *)&i
    

    reinterprets it (or casts it) to a pointer to char type. This means it can now be used like an array. Since you know an int is at least 32-bit on your machine, can access its bytes using c[0], c[1], c[2], c[3].

    Depending on the endianness of the system, the bytes of the number might be laid out: most significant byte first (big endian), or least significant byte first (little endian). x86 processors are little endian. This basically means the number 512 is laid out as in the example above, i.e.:

    00000000 00000000 00000010 00000000
        c[3]     c[2]     c[1]     c[0]
    

    I’ve grouped the bits into separate 8-bit chunks (bytes) corresponding to the way they are laid out in memory. Note, you also read them right-to-left here, so we can keep with conventions for the binary number system.

    Consequences

    Now setting c[0] = 1 has this effect:

    00000000 00000000 00000010 00000001
        c[3]     c[2]     c[1]     c[0]
    

    which is 2^9 + 2^0 == 513 in decimal.

    Setting c[1] = 1 has this effect:

    00000000 00000000 00000001 00000000
        c[3]     c[2]     c[1]     c[0]
    

    which is 2^8 == 256 in decimal, because you’ve overwritten the second byte 00000010 with 00000001

    Do note on a big endian system, the bytes would be stored in reverse order to a little endian system. This would mean you’d get totally different results to ones you got if you ran it on one of those machines.

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

Sidebar

Related Questions

In C, I have declared a memory area like this: int cells = 512;
int main() { char *p,c; for(p=Hello World;c=*p;++p) { printf(%c,c); } } In the above
Please look this code: #include <stdlib.h> #include <stdio.h> int N, L, I; float *
can someone explain why this isn't working? i am trying to put an unsigned
int a = 10; int* pA = &a; long long b = 200; long
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
This is a 1024 x 512 jpg. The size variable returns 84793. One thing
i have this function to get the content of file , #define BUFSIZE 512
int* p_bob = new int; *p_bob = 78; The above code makes sense to

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.