User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,594 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,479 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 479 | Replies: 2
Reply
Join Date: Apr 2007
Posts: 9
Reputation: sInFuL69er is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sInFuL69er sInFuL69er is offline Offline
Newbie Poster

array, unexpected output

  #1  
Sep 7th, 2007
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]);
      }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: array, unexpected output

  #2  
Sep 7th, 2007
Use %d
Not %c
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: array, unexpected output

  #3  
Sep 7th, 2007
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.
Last edited by TkTkorrovi : Sep 7th, 2007 at 1:04 pm.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC