C: Printing unsigned char data type

In summary, the program is not working when the matrix is dynamically allocated because the wrong conversion specifier is used for the hexadecimal input, resulting in incorrect values being stored in the matrix. The correct conversion specifier for unsigned char in hexadecimal is %hhx.
  • #1
gruba
206
1

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.

Print the read matrix, which is also in hexadecimal format (0x00,0x11,0xFF,...).
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:
Physics news on Phys.org
  • #2
gruba said:

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.

Print the read matrix, which is also in hexadecimal format (0x00,0x11,0xFF,...).
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:

Related to C: Printing unsigned char data type

1. What is an unsigned char data type?

An unsigned char data type is a data type in the C programming language that can hold integer values from 0 to 255. It is used to represent characters or small integer values.

2. How is an unsigned char data type printed in C?

To print an unsigned char data type in C, the %c format specifier is used in the printf() function. This will print the ASCII value of the character that is stored in the unsigned char variable.

3. Can an unsigned char data type be negative?

No, an unsigned char data type can only hold positive integer values from 0 to 255. If a value outside of this range is assigned to an unsigned char variable, it will overflow and wrap around to the opposite end of the range.

4. What is the difference between a char and an unsigned char in C?

A char data type can hold both positive and negative integer values, while an unsigned char data type can only hold positive values. Additionally, a char data type typically has a range of -128 to 127, while an unsigned char has a range of 0 to 255.

5. When would you use an unsigned char data type?

An unsigned char data type is typically used when working with character data, such as strings, or when storing small non-negative integer values. It can also be used to save memory space when the range of values needed is within 0 to 255.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
913
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
689
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
960
Back
Top