473,320 Members | 1,940 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,320 software developers and data experts.

Problem in using unsigned variable as a counter

Consider the code below:

unsigned int i=0;
for(j=1;j>-2; j--)
printf("Hello");

The printf statement is not executed even once. Why?

When the conditional statement in for loop is changed to j>=0 the
program goes into an infinite loop. Why?
Oct 24 '08 #1
2 2014


Sumit Nathany wrote:
Consider the code below:

unsigned int i=0;
for(j=1;j>-2; j--)
printf("Hello");
Your code sample contains a declaration for 'i', but never uses it. It
uses 'j', but never declares it.
I'll presume that you intended 'i' and 'j' to be the same.
The printf statement is not executed even once. Why?
When j==1, the comparison expression 'j>-2' is evaluated by first
converting '-2' to an unsigned integer, so that it can match the type
of j. The value of (unsigned int)(-2) is UINT_MAX-1, a number
guaranteed to be greater than 1. As a result, the condition for your
loop is never met, so the body of the loop is never executed.
When the conditional statement in for loop is changed to j>=0 the
program goes into an infinite loop. Why?
When j==0, what is the value of j-1? If your answer to that question
doesn't make it clear why your loop is failing, then you're answer to
that question is incorrect.
Oct 24 '08 #2
Thanks James! I will look for more co-operation from you in future.
Oct 24 '08 #3

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

Similar topics

6
by: Marcin Kalicinski | last post by:
Hi all, I have a problem with 'volatile' use in C++. The function get_clocks() below tries to use a 16 bit hardware counter to count time. The counter overflows very often. But an interrupt is...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
52
by: mavic | last post by:
hi to everybody,i need help for my project....how to make a program that accept any integer and convert it binary,octal and hexadecimal.pls help me...thanks
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
15
by: sathyashrayan | last post by:
Dear group, The below given link which I want to implement in C. http://online-judge.uva.es/p/v1/100.html The code which I wrote does not even reaches the near to the given problem.(I am...
17
by: Jonathan Pritchard | last post by:
I know this is a simple problem, but I've just included this in the title because it explains what my program tries to do. The following does not work, it for someone reason does not want to...
2
by: zl2k | last post by:
hi, I got a strange problem when running my problem. Basically, if I do int temp = someXYZ->someArray.size(); std::cout<<temp<<std::endl; //this temp is all right, say, temp is 12...
2
by: r3d dra6on | last post by:
I am not sure where I am going wrong with this. I get a lot of LINK errors, and I am unsure of how to solve the problem with them. Can someone please tell me where I went wrong? #include...
3
by: crater | last post by:
Today I ran into a problem that had me completely baffled. A function that I have just written, processes data in any one of 4 different data grids. Within a for() loop I need to call another...
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...
1
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.