-1

So I made a custom type by using typedef unsigned char byte;, and then declared an array of it, like using byte mem[255];. I used mem[0] = 0x10100000; to init the first value, but when I print it using printf("%d", mem[0]); I get 0. Why?

sef sf
  • 117
  • 1
  • 8
  • 2
    `0x10100000` is a 32 bit value. You probably meant `0xa0` – William Pursell May 24 '21 at 16:55
  • `0x10100000` is grossly over the storage capacity of an `unsigned char` You realize that's not a binary number, right? That's hexadecimal unsigned literal. – WhozCraig May 24 '21 at 16:56
  • If your compiler is *very* new and supports the upcoming C23 standard (or have an extension that allows it) then you can use binary [integer constants](https://en.cppreference.com/w/c/language/integer_constant) using the `0b` prefix, as in `0b10100000`. – Some programmer dude May 24 '21 at 17:08
  • @Someprogrammerdude and if you're not that lucky, you can use some template trickery to get there : [C++ binary constant/literal](https://stackoverflow.com/q/699781/5987). – Mark Ransom May 24 '21 at 18:03

4 Answers4

2

An unsigned char can typically only hold values between 0 and 255. The hex value 0x10100000 is well out of range for that type, so (essentially) only the low-order byte of that value is used, which is 0.

Presumably you wanted to use a binary constant. Not all compilers support that, but those that do would specify it as 0b10100000. For those than don't you can use the hex value 0xA0.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

You're assigning it the hexidecimal number 0x10100000 which is far larger than a single character, and thus can't be stored in a byte. If you want to use a binary number, and your compiler supports this, you might try using 0b10100000 instead.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
0

unsigned char can only hold the value of ((1 << CHAR_BIT) - 1)

You can check what is the maximum value yourself

#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("%u\n", (1 << CHAR_BIT) - 1);
}

On most systems it is 255 or 0xff.

When you assign the unsigned char with 0x10100000 only the lowest two hex digits will be assigned (in your case 0x00).

If you wanted to copy all the bytes from the 0x10100000 to the byte array mem you defined, the assignment will not work. You need to copy then instead:

#include <stdio.h>
#include <limits.h>
#include <string.h>

typedef unsigned char byte;

int main(void)
{
    byte mem[100];

    memcpy(mem, &(unsigned){0x10100000}, sizeof(0x10100000));

    for(size_t index = 0; index < sizeof(0x10100000); index++)
    {
        printf("mem[%zu] = 0x%hhx\n", index, mem[index]);
    }
}

Output:

mem[0] = 0x0
mem[1] = 0x0
mem[2] = 0x10
mem[3] = 0x10

https://godbolt.org/z/cGYa8MTef

Why in this order? Because the machine, where godbolt is run, uses little endioan. https://en.wikipedia.org/wiki/Endianness

0x prefix means that number hexadecimal. If you wanted to use binary number then gcc supports 0b prefix which is not standard.

mem[0] = 0b10100000

You can also create .h file

#define b00000000   0
#define b00000001   1
#define b00000010   2
#define b00000011   3

/* .... */
#define b11111110   254
#define b11111110   255

and use those definitions portable way

mem[0] = b10100000;
0___________
  • 60,014
  • 4
  • 34
  • 74
-1

You can't fit a 32 bit value inside an 8 bit variable (mem[0]). Do you perhaps mean to do this?

*(int *)mem = 0x10100000;
Blindy
  • 65,249
  • 10
  • 91
  • 131