Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #define ARR_SIZE sizeof(arr)/sizeof(arr[0])

Reply
Thread Tools

#define ARR_SIZE sizeof(arr)/sizeof(arr[0])

 
 
Vinu
Guest
Posts: n/a
 
      05-11-2005
The following code doesn't prints anything why it is?
The code is correct. plz explain the logic

#include <stdio.h>

int arr[] = {10,20,30,40,50};

#define ARR_SIZE sizeof(arr)/sizeof(arr[0])

void main()
{
for(int i= -1; i < (ARR_SIZE-1); )
printf("Hello %d\n", arr[++i]);
}
Thanks & Regards
Vinu

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-11-2005
"Vinu" <(E-Mail Removed)> wrote:

> The following code doesn't prints anything why it is?
> The code is correct.


The code you showed is not correct, in at least two aspects; and neither
is asking for homework answers on Usenet.

HTH; HAND.

Richard
 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      05-11-2005
Vinu <(E-Mail Removed)> wrote:

> for(int i= -1; i < (ARR_SIZE-1); )


Hint: Usual Arithmetic Conversions.

--
Stan Tobias
mailx `echo http://www.velocityreviews.com/forums/(E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
baumann@pan
Guest
Posts: n/a
 
      05-11-2005
see the Expert C ProgramIng , deep C secretes for the answer.

 
Reply With Quote
 
Jaspreet
Guest
Posts: n/a
 
      05-11-2005
> > for(int i= -1; i < (ARR_SIZE-1); )
Just remember to always compare unsigned values with a macro value.
Basically you need to initialize i to 0. Soo, I would have main as:

int main()
{
int i;
for(i= 0; i < ARR_SIZE; )
printf("Hello %d\n", arr[i++]);
return(0);
}

Also ensure that main has a int return type. The return value should be
a constant SUCCESS defined in one of the standard headers.

 
Reply With Quote
 
Christian Kandeler
Guest
Posts: n/a
 
      05-11-2005
Vinu wrote:

> The following code doesn't prints anything why it is?
> The code is correct. plz explain the logic
>
> #include <stdio.h>
>
> int arr[] = {10,20,30,40,50};
>
> #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
>
> void main()


int main(void)

> {
> for(int i= -1; i < (ARR_SIZE-1); )


ARR_SIZE has type size_t, which is an unsigned integer type (presumably at
least unsigned int). According to the Usual Arithmetic Conversions, i is
converted to this unsigned type, whereby -1 becomes the maximum value for
that type, which is definitely bigger than 4. As a result, the loop body is
not entered at all.
Of course, whoever wrote this code knew all that and simply wanted to
present an example -- nobody in their right mind would write an actual for
loop in this way.

> printf("Hello %d\n", arr[++i]);
> }



Christian
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-11-2005
"baumann@pan" <(E-Mail Removed)> wrote:

> see the Expert C ProgramIng , deep C secretes for the answer.


The answer to _what_? For heavens' sake, man, if you must use Google
Groups Broken Beta, do learn to get it to at least quote a bit of what
you're replying to. This way, you're more or less talking in a vacuum.

BTW, for someone who cannot discover the OP's problem on his own, I'd
hardly recommend Deep C Secrets. It is an altogether too confusticating
book for someone who doesn't already know C reasonably well.

Richard
 
Reply With Quote
 
shafique069@gmail.com
Guest
Posts: n/a
 
      05-11-2005
The operator sizeof returns an unsigned value and the unsigned division
results in unsigned value for ARR_SIZE and that results in unsigned
comparison inside the for statement.

So for the int i = -1; the unsigned comparison for the statement i <
ARR_SIZE becomes as:
0xFFFFFFFF < (5-1)

which results in false and the printf statement inside for loop never
gets executed and that is why you are not seeing any output!

(I am assuming that int size is 32-bit on your system.)

Try
#define ARR_SIZE (int)(sizeof(arr)/sizeof(arr[0]))

and the result will be the same as you desired.

I hope it helps.

 
Reply With Quote
 
baumann@pan
Guest
Posts: n/a
 
      05-11-2005
i think below may help u understand why .
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
1 Every integer type has an integer conversion rank defined as follows:
- No two signed integer types shall have the same rank, even if they
hav e the same
representation.
- The rank of a signed integer type shall be greater than the rank of
any signed integer
type with less precision.
- The rank of long long int shall be greater than the rank of long
int, which
shall be greater than the rank of int, which shall be greater than the
rank of short
int, which shall be greater than the rank of signed char.
- The rank of any unsigned integer type shall equal the rank of the
corresponding
signed integer type, if any.
- The rank of any standard integer type shall be greater than the
rank of any extended
integer type with the same width.
- The rank of char shall equal the rank of signed char and unsigned
char.
- The rank of _Bool shall be less than the rank of all other standard
integer types.
- The rank of any enumerated type shall equal the rank of the
compatible integer type
(see 6.7.2.2).
- The rank of any extended signed integer type relative to another
extended signed
integer type with the same precision is implementation-defined, but
still subject to the
other rules for determining the integer conversion rank.
- For all integer types T1, T2, and T3, if T1 has greater rank than
T2 and T2 has
greater rank than T3, then T1 has greater rank than T3.
2 The following may be used in an expression wherever an int or
unsigned int may
be used:
42 Language §6.3.1.1
ŠISO/IEC ISO/IEC 9899:1999 (E)
- An object or expression with an integer type whose integer
conversion rank is less
than the rank of int and unsigned int.
- A bit-field of type _Bool, int, signed int, or unsigned int.
If an int can represent all values of the original type, the value is
converted to an int;
otherwise, it is converted to an unsigned int. These are called the
integer
promotions.4 All other types are unchanged by the integer promotions.
3 The integer promotions preserve value including sign. As discussed
earlier, whether a
''plain'' char is treated as signed is implementation-defined.
Forward references: enumeration specifiers (6.7.2.2), structure and
union specifiers
(6.7.2.1).

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-11-2005
Vinu wrote:
>
> The following code doesn't prints anything why it is?
> The code is correct. plz explain the logic
>
> #include <stdio.h>
>
> int arr[] = {10,20,30,40,50};
> #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
> void main()
> {
> for(int i= -1; i < (ARR_SIZE-1); )
> printf("Hello %d\n", arr[++i]);
> }


No, the code isn't correct. Besides problems with arithmetic
conversions it requires a C99 system to compile (declaration of i),
and invokes undefined behavior by failing to return a value. A
corrected version follows, which works:

#include <stdio.h>
int arr[] = {10,20,30,40,50};
#define ARR_SIZE sizeof(arr)/sizeof(arr[0])

int main(void)
{
int i;

for (i = 0; i < (ARR_SIZE); )
printf("Hello %d\n", arr[i++]);
return 0;
}

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
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




Advertisments