1,105,229 Community Members

difficulty in displaying array contents

Member Avatar
saurabh.mehta.33234
Junior Poster in Training
58 posts since Nov 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Why this piece of code doesnot display array contents

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}
Member Avatar
deceptikon
Eternally Awesome
4,669 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

You've stumbled on one of the tricky points of mixing signed and unsigned values. The result of the sizeof operator is unsigned, but d is signed and negative. You can just pretend the comparison looks like this, because that's what actually happens:

for (d = -1; (unsigned)d <= (TOTAL_ELEMENTS - 2); d++)

Given that little tidbit, it's easy to see why the loop doesn't execute, because -1 when converted to unsigned will be a huge value, far greater than TOTAL_ELEMENTS - 2. This is due to the rule of unsigned wrapping. When you go negative, the value wraps around from the largest value.

One possible fix is to force both parts of the comparison to be signed:

for (d = -1; d <= (int)(TOTAL_ELEMENTS - 2); d++)

Another is to avoid starting at -1 in the first place. The following is a conventional counted loop setup that starts at 0 and goes until N-1. It's highly recommended:

for (d = 0; d < TOTAL_ELEMENTS; d++)
    printf("%d\n", array[d]);
Member Avatar
saurabh.mehta.33234
Junior Poster in Training
58 posts since Nov 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thankyou Sir I am a Beginner in C.So can you suggest any links from where I can download the C99 standard e-book.It would be really helpful.

Member Avatar
deceptikon
Eternally Awesome
4,669 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

But be forewarned: if you're a beginner to C then you'll likely find the standard obtuse and unhelpful. It's written for compiler writers, not every day users of the language.

Member Avatar
saurabh.mehta.33234
Junior Poster in Training
58 posts since Nov 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thankyou Sir So according to you which reference book will be suitable for beginner.

Member Avatar
deceptikon
Eternally Awesome
4,669 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

C: A Reference Manual and The C Programming Language both have reasonably accessible reference parts. But you can get the same result for the most part with a Google search.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article