C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Programming Tips & Tricks » C - Tips & Tricks

How to initialize array elements with hexadecimal values in C?

By: IncludeHelp, on 07 FEB 2017

Here, we will initialize array elements with one byte Hexadecimal values, remember these points while initialising:

  • Declare an array of unsigned char (it will refer Byte Array or unsigned char array).
  • One Byte value range is 0x00 to 0xFF.

Here is the syntax:

unsigned char array_name[]={value1, value2,...};

Consider the below statement, here I am initialising array with 5 Hexadecimal values...

unsigned char arr[]={0x00,0x05,0xFE,0xFF,0xA5};





Here is the complete program

#include <stdio.h>

int main()
{
	unsigned char arr[]={0x00,0x05,0xFE,0xFF,0xA5};
	int loop;
	
	printf("Array elements are:\n");
	for(loop=0;loop<5;loop++)
		printf("%02X (Decimal: %03d)\n",arr[loop],arr[loop]);
	
	return 0;	
}

Output

Array elements are: 
00 (Decimal: 000) 
05 (Decimal: 005) 
FE (Decimal: 254) 
FF (Decimal: 255) 
A5 (Decimal: 165)

COMMENTS