473,326 Members | 2,104 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,326 software developers and data experts.

code not working properly

Hi all,
I recently came across this small piece of code:

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

it does not print anything. the reson i think its not printing
anything because of the comparison returning false. Why is the
condition not true?
please help me with this problem.

Nov 15 '05 #1
4 1112


Madhav wrote:
Hi all,
I recently came across this small piece of code:

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

it does not print anything. the reson i think its not printing
anything because of the comparison returning false. Why is the
condition not true?
please help me with this problem.


sizeof returns size_t which is unsigned. So (TOTAL_ELEMENTS-2) is
unsigned.
C converts d to unsigned before comparing.
-1 becomes a large number and condition became false.

Nov 15 '05 #2
"Madhav" <ma***********@gmail.com> writes:
I recently came across this small piece of code:

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

it does not print anything. the reson i think its not printing
anything because of the comparison returning false. Why is the
condition not true?
please help me with this problem.


Here's a simplified example:

This program prints nothing:

#include<stdio.h>
int main(void)
{
int d;
size_t max = 5;
for(d = -1; d <= max; d ++) {
printf("d = %d\n", d);
}
return 0;
}

This one works:

#include<stdio.h>
int main(void)
{
int d;
int max = 5;
for(d = -1; d <= max; d ++) {
printf("d = %d\n", d);
}
return 0;
}

The problem is that TOTAL_ELEMENTS expands to an expression of type
size_t. When you apply an operator ("<=" in this case) to an int and
a size_t (an unsigned type), the int value is promoted to size_t.
Converting the value -1 to size_t yields a large positive value,
typically 2147483647 if size_t is 32 bits.

Casting TOTAL_ELEMENTS to int is one workaround (one of the few cases
where a cast isn't a bad idea).

--
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 15 '05 #3
Madhav wrote:
Hi all,
I recently came across this small piece of code:

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

it does not print anything. the reson i think its not printing
anything because of the comparison returning false. Why is the
condition not true?
please help me with this problem.
sizeof(x) returns an UNSIGNED int. Then, the comparison with a signed int d
will be done in "unsigned" mode. The minus one will be transformed into
0xffffffff, in a 32 bit platform, yielding a huge number.

This huge number will be compared to 5, and the condition does not hold,
so nothing is printed and the program exits.

FIX: for(d=-1;d <= (int)(TOTAL_ELEMENTS-2);d++)


Note the (int) cast.

jacob
Nov 15 '05 #4
Madhav wrote:
#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;
}

it does not print anything. the reson i think its not printing
anything because of the comparison returning false. Why is the
condition not true?


Hint: d is signed, TOTAL_ELEMENTS-2 is unsigned.

#include <stdio.h>
int main(void)
{
printf("%u\n", (unsigned)(-1));
return 0;
}
Nov 15 '05 #5

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

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
6
by: Brian Miller | last post by:
I've been constructing an ASP.Net application using the 1.1 framework, and have been using Web Matrix for development purposes. Now that my application is near completion, I wanted to see if I can...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
4
by: qbproger | last post by:
I'm developing a plugin for some software. The previous version of the software didn't require a start in directory to be set. This allowed me to leave the working directory to the default in the...
4
by: candexis | last post by:
Hi!!!!!!! could you please tell me why this code in not working properly, the thing is that when I read one string after another, the second one is not read, and I don't know why, I am using...
9
by: javakid | last post by:
Hi Following form validatioin code is working fine on IE but not working on Mozilla Firefox V2. Can any body suggest? Regards <script ="JAVASCRIPT" type="text/javascript">...
3
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
7
by: robin1983 | last post by:
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.