# C: Printing unsigned char data type

## Homework Statement

Given a matrix n x m with unsigned char data type entries (entries are of size 1 byte, so data type of an entry should be unsigned or signed char, not int or char *). Entries are read in hexadecimal format (0x00,0x11,0xFF,...). Matrix should be allocated dynamically.

Example input:
C:
n x m = 2 2
mat[0][1]=0x00
mat[0][2]=0x22
mat[1][0]=0x33
mat[1][1]=0x44
Output:
C:
0x00 0x22
0x33 0x44
2. The attempt at a solution
Lets look at the following example:
C:
#include<stdio.h>
int main()
{
unsigned char x,y;
printf("x = ");
scanf("%x",&x);
y=x;
printf("0x%.2X",y);
return 0;
}

We read the value x as hexadecimal integer, assign the value of x to y, and print the value of y.
In this case, it will work.

Lets look at the another example, with statically allocated matrix:
C:
#include <stdio.h>
int main()
{
int n,m,i,j;
unsigned char mat[101][101];
unsigned char info;
do
{
printf("n x m = ");
scanf("%d %d",&n, &m);
}
while(n<1 || m<1);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("mat[%d][%d]=",i,j);
scanf("%x",&info);
mat[i][j]=info;
}
}
printf("\n");
for(i=0;i<n;i++,printf("\n"))
for(j=0;j<m;j++)
printf(" 0x%.2x", mat[i][j]);
return 0;
}

In this case, the program works when the matrix is statically allocated.

But, when the matrix is dynamically allocated, the program is not working:
C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,m,i,j;
unsigned char **mat;
unsigned char info;
do
{
printf("n x m = ");
scanf("%d %d",&n, &m);
}
while(n<1 || m<1);

mat=(unsigned char **)calloc(n,sizeof(unsigned char *));
for(i=0;i<n;i++)
{
mat[i]=(unsigned char *)calloc(m,sizeof(unsigned char));
for(j=0;j<m;j++)
{
printf("mat[%d][%d]=",i,j);
scanf("%x",&info);
mat[i][j]=info;
}
}
printf("\n");
for(i=0;i<n;i++,printf("\n"))
for(j=0;j<m;j++)
printf(" 0x%.2X", mat[i][j]);
for(i=0;i<n;i++){
free(mat[i]);
}
free(mat);
return 0;
}

Question: Why won't the program work when matrix is dynamically allocated?
Note: Data type of matrix entry should be of size 1 byte (not char *).

Last edited by a moderator:

Mark44
Mentor

## Homework Statement

Given a matrix n x m with unsigned char data type entries (entries are of size 1 byte, so data type of an entry should be unsigned or signed char, not int or char *). Entries are read in hexadecimal format (0x00,0x11,0xFF,...). Matrix should be allocated dynamically.

Example input:
C:
n x m = 2 2
mat[0][1]=0x00
mat[0][2]=0x22
mat[1][0]=0x33
mat[1][1]=0x44
Output:
C:
0x00 0x22
0x33 0x44
2. The attempt at a solution
Lets look at the following example:
C:
#include<stdio.h>
int main()
{
unsigned char x,y;
printf("x = ");
scanf("%x",&x);
y=x;
printf("0x%.2X",y);
return 0;
}

We read the value x as hexadecimal integer, assign the value of x to y, and print the value of y.
In this case, it will work.

Lets look at the another example, with statically allocated matrix:
C:
#include <stdio.h>
int main()
{
int n,m,i,j;
unsigned char mat[101][101];
unsigned char info;
do
{
printf("n x m = ");
scanf("%d %d",&n, &m);
}
while(n<1 || m<1);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("mat[%d][%d]=",i,j);
scanf("%x",&info);
mat[i][j]=info;
}
}
printf("\n");
for(i=0;i<n;i++,printf("\n"))
for(j=0;j<m;j++)
printf(" 0x%.2x", mat[i][j]);
return 0;
}

In this case, the program works when the matrix is statically allocated.

But, when the matrix is dynamically allocated, the program is not working:
By "not working" what do you mean? Please be more specific. Does the program compile? If not, what errors does it show? Does the program compile but give warnings? If so what are they and where are the (line number)?
gruba said:
C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,m,i,j;
unsigned char **mat;
unsigned char info;
do
{
printf("n x m = ");
scanf("%d %d",&n, &m);
}
while(n<1 || m<1);
mat=(unsigned char **)calloc(n,sizeof(unsigned char *));
for(i=0;i<n;i++)
{
mat[i]=(unsigned char *)calloc(m,sizeof(unsigned char));
for(j=0;j<m;j++)
{
printf("mat[%d][%d]=",i,j);
scanf("%x",&info);
mat[i][j]=info;
}
You are using the wrong conversion specifier here. %x is for hexadecimal integers (four bytes), not unsigned char (one byte). The conversion specifier for unsigned char in hexadecimal is %hhx, I believe.
gruba said:
C:
  }
printf("\n");
for(i=0;i<n;i++,printf("\n"))
for(j=0;j<m;j++)
printf(" 0x%.2X", mat[i][j]);
for(i=0;i<n;i++){
free(mat[i]);
}
free(mat);
return 0;
}

Question: Why won't the program work when matrix is dynamically allocated?
Note: Data type of matrix entry should be of size 1 byte (not char *).

Last edited: