printing array elements

C

crazy

Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",array[d+1]);
return 0;
}

crazy
 
M

Mike Wahler

crazy said:
Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

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

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

Read about 'signed', 'unsigned', and 'conversions'.

IMO it's poor practice to use negative offsets
with arrays like this. Or was this just a 'puzzle'
to be solved?

-Mike
 
M

Mike Wahler

Mike Wahler said:
crazy said:
Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

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

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

Read about 'signed', 'unsigned', and 'conversions'.

IMO it's poor practice to use negative offsets
with arrays like this. Or was this just a 'puzzle'
to be solved?

Additonal hint: 'sizeof' returns an (impelemenation-
defined) unsigned type.

-Mike
 
A

Andreas Kahari

Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",array[d+1]);
return 0;
}

Cast "(TOTAL_ELEMENTS - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.
 
C

crazy

Andreas Kahari said:
----snipped----


Cast "(TOTAL_ELEMENTS - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}

This example definitely helped understand what was wrong.

My mistake was printing d as %d and not as %u.
The moment I did that, it became very clear what was happening.

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."

What is meant by rank here? what is the rank precedence?

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.


-crazy
 
P

Peter Nilsson

crazy said:
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",array[d+1]);
return 0;
}

Because size_t has equal or higher rank than an int on your machine.
Try experimenting with (-1 < 1u).
 
J

Jack Klein

This example definitely helped understand what was wrong.

My mistake was printing d as %d and not as %u.
The moment I did that, it became very clear what was happening.



What is meant by rank here? what is the rank precedence?




-crazy

The integer types have a ranking in C, such that each higher-ranked
integer type must be able to contain at least the range of values of
lesser ranked types, if not more. This is also subject to the minimum
allowable absolute range for each type.

Signed or unsigned short int must be able to hold all the valuer of
signed and unsigned char, respectively.

(un)signed int must be able to hold all the values of (un)signed
short.

(un)signed long must be able to hold all the values of (un)signed int.

(un)signed long long must be able to hold all the values of (un)signed
long.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
D

Dan Pop

In said:
My mistake was printing d as %d and not as %u.

Nope, that was NOT your mistake: d has type int, therefore it CANNOT be
printed with %u, which expects an unsigned int!
The moment I did that, it became very clear what was happening.

To do that properly, you must use (unsigned)d.
What is meant by rank here? what is the rank precedence?

At your current level, you can safely ignore the C99 lingo. Simply
read about the arithmetic conversions in your favourite C book.

Dan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top