C: Printing unsigned char data type

Click For Summary
SUMMARY

The discussion centers on the issue of printing an unsigned char data type from a dynamically allocated matrix in C. The primary problem arises from using the incorrect format specifier (%x) in the scanf function, which is intended for reading four-byte integers instead of one-byte unsigned chars. The correct format specifier for reading an unsigned char in hexadecimal format is %hhx. This mistake leads to unexpected behavior when the matrix is dynamically allocated, while the statically allocated version functions correctly.

PREREQUISITES
  • Understanding of C programming language syntax and semantics
  • Knowledge of dynamic memory allocation in C using malloc and calloc
  • Familiarity with data types in C, specifically unsigned char
  • Proficiency in using format specifiers in C's printf and scanf functions
NEXT STEPS
  • Research the correct usage of format specifiers in C, particularly for different data types
  • Learn about dynamic memory management in C, including best practices for using calloc and free
  • Explore debugging techniques for C programs to identify and resolve runtime issues
  • Study examples of reading and printing hexadecimal values in C to reinforce understanding
USEFUL FOR

C programmers, computer science students, and software developers who are working with memory management and data type handling in C.

gruba
Messages
203
Reaction score
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
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K