C语言:将数组的元素全部打印

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

void printByteArrayElements(char* inputArray, int size)
{
  long i;
  char StrBuff[200] = {0};

  if (size * 5 > 200)
  {
    printf("print input data buffer in hex form impossible because of too many data (%d)\n", size);
  }
 
  for(i=0; i < size; i++)
  {
   char tmp[5];

   strncat(StrBuff, "0x", 2);
   snprintf(tmp, sizeof(tmp), "%02X", inputArray[i]);

   printf("tmp = '%s'\n", tmp);

   strncat(StrBuff, tmp, sizeof(tmp));

   if(i < size - 1)
   {
     strncat(StrBuff, " ", 1);
   }
  }

  printf("Data Size = %d, Data Element = '%s'\n", size, StrBuff);
}

int main()
{
	char dataElement = 0;
	char inputArray[20] = {0, 0, 0};

	printf("dataElement = 0x%02X\n", dataElement);	
	printByteArrayElements(inputArray, 20);

	return 0;
}

dataElement = 0x00
tmp = ’00’
Data Size = 20, Data Element = ‘0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00’

    原文作者:AllenSun-1990
    原文地址: https://blog.csdn.net/xikangsoon/article/details/114900916
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞