Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with initiallisation

Reply
Thread Tools

problem with initiallisation

 
 
vim
Guest
Posts: n/a
 
      05-15-2006
The expected output of the following C program is to print the elements
in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#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;
}

 
Reply With Quote
 
 
 
 
KOFKS
Guest
Posts: n/a
 
      05-15-2006
It seems the problem of "d = -1".
If you use d = 0 instead, all is ok.(Of course, you should modify other
parts accordingly. )
I do not know why either.
Waiting for some expert to explain. TIA!

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-15-2006


vim wrote On 05/15/06 11:20,:
> The expected output of the following C program is to print the elements
> in the array. But when actually run, it doesn't do so.
> #include<stdio.h>
> #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;
> }


TOTAL_ELEMENTS is of type size_t, which is an unsigned
integer type, hence not the same type as the signed int d.
When the comparison operator <= has operands of different
types, it promotes one (or both) of them to a single common
type, then compares the promoted values. In your case, the
signed int d is being promoted to an unsigned type to match
the size_t, and by the rules of unsigned arithmetic the
promotion produces a very large number. The loop test fails
on the very first attempt, and the loop never executes.

Some compilers will warn about this sort of thing; read
your compiler's documentation to learn how to crank up the
warning levels. For gcc, I suggest "-W -Wall -ansi -pedantic",
possibly replacing "-ansi" with a different Standard version.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Bill Pursell
Guest
Posts: n/a
 
      05-15-2006

vim wrote:
> The expected output of the following C program is to print the elements
> in the array. But when actually run, it doesn't do so.
> #include<stdio.h>
> #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;
> }


Try:
#define TOTAL_ELEMENTS (int)(sizeof(array) / sizeof(array[0]))

In the test in the for loop, the first comparison fails,
probably because the RHS is being cast as
an unsigned int, so -1 is promoted to 0xffffffff which is greater than
5.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-15-2006
vim wrote:
> The expected output of the following C program is to print the elements
> in the array.


You may expect that but I don't.

> But when actually run, it doesn't do so.
> #include<stdio.h>
> #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;
> }


Try searching the Google archive of this group and you will find your
question has been answered many times in the past. Since you are using
Google anyway that should not be beyond your abilities.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-15-2006
KOFKS wrote:

Please provide Context. See the bit about Google at
http://clc-wiki.net/wiki/Intro_to_clc and the sites it links to.

> It seems the problem of "d = -1".
> If you use d = 0 instead, all is ok.(Of course, you should modify other
> parts accordingly. )
> I do not know why either.
> Waiting for some expert to explain. TIA!


The experts have explained it many times in the past. Try searching.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
KOFKS
Guest
Posts: n/a
 
      05-15-2006
I see. So, that is it!

I review the textbook to find out that it is int being promoted to
unsigned int in a mixed expression other than the opposite.
I used to take it for granted that unsigned integer will be promoted to
int in mixed expression. What a mistake!

Appreciate you for the help and kindness!
Have a good day!

 
Reply With Quote
 
vim
Guest
Posts: n/a
 
      05-16-2006
Yes U r correct .I got that output.
Thanks for giving good concept

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-17-2006
"vim" <(E-Mail Removed)> writes:
> Yes U r correct .I got that output.
> Thanks for giving good concept


If I were deliberately trying to compose a badly written followup, it
would look very much like what you just wrote.

It's "you", not "U". It's "are", not "r". This is not a chat room or
a cell phone; take the time to spell out words so people can read
them. Many people here don't have English as their first language,
and have a lot of difficulty reading abbreviated pseudo-English. Most
of the rest of us just find it annoying (not a good idea if you're
asking for help).

And I find it difficult to believe that you haven't already been told
to read <http://cfaj.freeshell.org/google/>. If you haven't read it,
do so now, and follow its advice.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments