C language-print byte representations of different objects (int* cast to unsigned char* understanding)

Source: Internet
Author: User

This article refers to "in-depth understanding of computer systems" P31.

First look at the following code: 12345 hexadecimal representation: 0x00003039

1#include <stdio.h>2 3 intMain ()4 {5     intA =12345;6     Char*q = (Char*) (&a);7      for(inti =0; I <sizeof (a); ++i)8printf"%.2x", Q[i]);9     return 0;Ten}

The output is:

The address of A is the int* type, whose object is 4 bytes of type int, 12345, when it is cast to the unsigned char* type q, since char is 1 bytes, its object is split by one byte byte, because the memory is small end storage (low address, High address), so q[0] is 4 bytes of 12345 in memory of the first byte, and so on (12345 of the 4 bytes are forcibly split into 4 single-byte storage), so the final output is 39 30 00 00.

C language-print byte representations of different objects (int* cast to unsigned char* understanding)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.