problem with initiallisation

V

vim

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;
}
 
K

KOFKS

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!
 
E

Eric Sosman

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.
 
B

Bill Pursell

vim said:
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.
 
F

Flash Gordon

vim said:
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.
 
F

Flash Gordon

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.
 
K

KOFKS

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!
 
K

Keith Thompson

vim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top