473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

#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

Nov 14 '05 #1
13 7614
"Vinu" <vi*********@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
Nov 14 '05 #2
Vinu <vi*********@yahoo.com> wrote:
for(int i= -1; i < (ARR_SIZE-1); )


Hint: Usual Arithmetic Conversions.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #3
see the Expert C ProgramIng , deep C secretes for the answer.

Nov 14 '05 #4
> > 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.

Nov 14 '05 #5
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
Nov 14 '05 #6
"baumann@pan" <ba*********@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
Nov 14 '05 #7
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.

Nov 14 '05 #8
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).

Nov 14 '05 #9
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10
CBFalconer <cb********@yahoo.com> writes:
Vinu wrote:

[...]
#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.

[...]

No, it invokes undefined behavior by declaring void main(). If that's
corrected to int main() or int main(void), failing to return a value
doesn't invoke undefined behavior. In C90, it returns an unspecified
status to the environment. In C99, unfortunately, it does an implicit
"return 0;".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #11
On Wed, 11 May 2005 00:48:31 -0700, Jaspreet wrote:
> 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; )
It would be more natural to put the i++ here then you have a normal
iteration loop.
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.


The portable values are 0, EXIT_SUCCESS and EXIT_FAILURE, the last 2 being
defined in <stdlib.h>

Lawrence

Nov 14 '05 #12
Vinu,

sizeof operator returns you size_t, basically an unsigned int, you can
assume. When you compare an int with an unsigned int ( that is , i <
(ARR_SIZE - 1), it may give unexpected results. (Probably because, the
16th bit is the sign bit in int datatype)

If you typecast , it will work .. like this.
for(int i= -1; i < (int)(ARR_SIZE-1); )

Thanks.

Nov 14 '05 #13
On Thu, 12 May 2005 07:20:42 -0700, HalcyonWild wrote:
Vinu,

sizeof operator returns you size_t, basically an unsigned int, you can
assume.
size_t can be any unsigned integer type. As long as the integer promotions
leave it as an unsigned int or unsigned long the problem encountered here
will happen.
When you compare an int with an unsigned int ( that is , i <
(ARR_SIZE - 1), it may give unexpected results. (Probably because, the
16th bit is the sign bit in int datatype)
It is not to do with the sign bit specifically, it is to do with the value
you get when you convert an int to an unsigned type. The rule is that you
add or subtract 1 more than the maximum value that the unsigned type can
represent until you get a value that the unsigned type can represent. So
to convert -1 to unsigned int (assuming that type for size_t) you would
add (UINT_MAX+1) to -1 producing the result UINT_MAX which will be greater
than ARR_SIZE-1.
If you typecast , it will work .. like this.
for(int i= -1; i < (int)(ARR_SIZE-1); )


Or rearrange things to avoid negative values.

Lawrence
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
2
by: hudaiqian | last post by:
hi,all I encounter a problem with sizeof template <typename type> void display(type& a) { cout<<sizeof(type)<<":"<<a<<endl; } int main() {
11
by: chandanlinster | last post by:
There is a sentence in the book "The C programming language" that says - "C provides a compile-time unary operator called sizeof that can be used to compute the size of any object." What does the...
7
by: miaohua1982 | last post by:
the code is as follows: #include<stdio.h> int arr={1,2,3,4,5,6,7}; #define size (sizeof(arr)/sizeof(arr)) int main() { int index = -1; int x = 0; int t;
5
by: maker.rain1 | last post by:
Hello All, I have come across a problem as explained below in a sample. Please help me if anyone has any ideas to solve this. I have a #define as defined below. #define MAX 200 int...
3
by: Andreas Eibach | last post by:
Hi, when I had in mind to turn this code (which worked fine in main()) to a separate function, I stumbled upon this...(of course, it's way bigger than that, but I hope this short snippet can...
4
by: sedaw | last post by:
if i want to let the user to define the array size, how can i do it ? because if im define arr theres compilation error . cause n!=const . (the user enter n ) TNX .....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.