Does sizeof return the number of bytes or the number of octets of a type in C?

后端 未结 4 604
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 12:09

Simply put in C and variants (unlike that wuss java with its virtual machine) the size of primitive types on different targets can vary greatly, and there is really no guarantee

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 12:47

    From my own experience, working on embedded micro controllers with exotic 'C' compilers, I have seen:

    sizeof( uint8 )
    

    return 1

    sizeof( uint16 )
    

    return 1

    sizeof( uint32 )
    

    return 2

    Clearly, I was dealing with a machine were the smallest addressable entity was 16 bit, so the sizeof does not comply with C89 or C99.

    I would say that on mainstream C89 & C99 compliant systems, the accepted answer is correct. Unfortunately, a "C" compiler can still be called a "C" compiler, even if it does not comply to a 25 year old standard. I hope this answer help put things in perspective, given more exotic systems.

提交回复
热议问题