1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: Printing unsigned char data type

  1. May 25, 2016 #1
    1. The problem statement, all variables and given/known data
    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:
    Code (C):
    n x m = 2 2
    mat[0][1]=0x00
    mat[0][2]=0x22
    mat[1][0]=0x33
    mat[1][1]=0x44
     
    Output:
    Code (C):
    0x00 0x22
    0x33 0x44
     
    2. The attempt at a solution
    Lets look at the following example:
    Code (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:
    Code (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:
    Code (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: May 25, 2016
  2. jcsd
  3. May 25, 2016 #2

    Mark44

    Staff: Mentor

    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)?
    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.
     
    Last edited: May 25, 2016
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C: Printing unsigned char data type
Loading...