Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   #define ARR_SIZE sizeof(arr)/sizeof(arr[0]) (http://www.velocityreviews.com/forums/t437985-define-arr_size-sizeof-arr-sizeof-arr-0-a.html)

Vinu 05-11-2005 06:21 AM

#define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
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


Richard Bos 05-11-2005 06:56 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
"Vinu" <vinuwarrier@yahoo.com> 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

S.Tobias 05-11-2005 07:21 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
Vinu <vinuwarrier@yahoo.com> wrote:

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


Hint: Usual Arithmetic Conversions.

--
Stan Tobias
mailx `echo siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

baumann@pan 05-11-2005 07:48 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
see the Expert C ProgramIng , deep C secretes for the answer.


Jaspreet 05-11-2005 07:48 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
> > 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.


Christian Kandeler 05-11-2005 08:05 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
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

Richard Bos 05-11-2005 08:26 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
"baumann@pan" <baumann.Pan@gmail.com> 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

shafique069@gmail.com 05-11-2005 08:29 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
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.


baumann@pan 05-11-2005 09:00 AM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
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.48) 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).


CBFalconer 05-11-2005 01:01 PM

Re: #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
 
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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!




All times are GMT. The time now is 09:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.