Hi,
This is a segmant of my code that iam working on, for some reason its not giving out the expected output :( which is 11010111. It gives me "elements: ☺ ☺ ☺ ☺ ☺ ☺" and its really bugging me cuz it looks right. Any help would be appriciated. Thanks

unsigned char ipans[8],ipb[4]={1,0,1,1},epans[8];
int           ep[8] = {4,1,2,3,2,3,4,1};


int main()
{
    int x;
      
      printf("elements: ");
      for(x=0;x<8;x++)
      {
         epans[x] = ipb[ep[x]-1];
         printf("%c " ,epans[x]);
      }

Use %d
Not %c

Yes, Salem is right, %c gives you a character with a corresponding code, not a number, 1 1 0 1 0 1 1 1 is exactly what your program outputs when you use %d. Also, when you initialize the arrays, you don't always have to provide the size, like in your program ipb [] = {1, 0, 1, 1} would work as well. One more thing, when you use the initialized arrays, it's often better to not use the array size anywhere, to avoid an unnecessary magic number. Like, in your array ep, the elements most likely cannot be 0, so, you can end this array with zero, and iterate until zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.