8

I would like to initialize a 16-byte array of hexadecimal values, particularly the 0x20 (space character) value.

What is the correct way?

unsigned char a[16] = {0x20};

or

unsigned char a[16] = {"0x20"};

Thanks

3
  • 0x2 is not the space character, but an integer. Commented Oct 31, 2015 at 18:51
  • I am talking about the ASCII space character in hex, which is 0x20 Commented Oct 31, 2015 at 19:07
  • The C standard does not use ASCII. (Just noted I missed a trailing 0; I meant 0x20, of course). If you mean space, you should use a space character constant: ' '. Commented Oct 31, 2015 at 19:57

6 Answers 6

8

Defining this, for example

unsigned char a[16] = {0x20, 0x41, 0x42, };

will initialise the first three elements as shown, and the remaining elements to 0.

Your second way

unsigned char a[16] = {"0x20"};

won't do what you want: it just defines a nul-terminated string with the four characters 0x20, the compiler won't treat it as a hexadecimal value.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh ok I see. And what if I want the ASCII value of 0x20? Like I want the keyboard space that that hex value gives.
@Kingamere the ASCII value is 0x20 (hex) or 32 (decimal). If you print that as an int you'll get 32 and if you print as char you'll get ` ` (space).
7

There is a GNU extension called designated initializers. This is enabled by default with gcc

With this you can initialize your array in the form

unsigned char a[16] = {[0 ... 15] = 0x20};

1 Comment

Designated initialisers are not a gcc extension, but standard C. Allowing a range is an extension, however is..
5
unsigned char a[16] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};

or

unsigned char a[16] = "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20";

1 Comment

This violates DRY and adds adds unneccasary bytes, no sane programmer should do this.
5

I don't know if this is what you were looking for, but if the size of the given array is going to be changed (frequently or not), for easier maintenance you may consider the following method based on memset()

#include <string.h>

#define NUM_OF_CHARS 16

int main()
{
    unsigned char a[NUM_OF_CHARS];

    // initialization of the array a with 0x20 for any value of NUM_OF_CHARS
    memset(a, 0x20, sizeof(a));

    ....
}

Comments

3

The first way is correct, but you need to repeat the 0x20, sixteen times. You can also do this:

unsigned char a[16] = "                ";

5 Comments

This solution will lead to undefined behavior! "<16 spaces>" will actually occupy 17 bytes (the last one for '\0').
@AlexLop. Negative, the C specification allows a character array to be initialized using a string literal that is the same size as the array. In that case the NUL terminator is dropped.
Could you please point me to the documentation which will support your statement?
Section 6.7.9 paragraph 14, which says (emphasis added): "Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array."
You are right, my mistake. Could you please make a small edit in your answer so I could "undo" the downvote?
0

I would use memset.

No need to type out anything and the size of the array is only provided once at initialisation.

#include <string.h>

int main(void)
{
  unsigned char b[16];
  memset(b, 0x20, sizeof(b));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.