Jump to content
xisto Community
Ahsaniqbalkmc

C -- Strange Behavior With Numbers

Recommended Posts

While following my path to learn C, I have created another simple program (from the scenario given in programming project section of the exercise). What this program does is it takes a number (upto 4 digits) from the user and tells him how many digits the number has. The program looks like this:

#include <stdio.h>int main(void) {printf("Program: Calculate how many digits in a number\n");int number;printf("Enter a number with maximum 4 digits: ");scanf("%d", &number);int digits;int error = 0;if (number >= 0 && number <=9) {digits = 1;}else if (number >= 10 && number <= 99) {digits = 2;}else if (number >= 100 && number <= 999) {digits = 3;}else if (number >= 1000 & number <= 9999) {digits = 4;}else {error = 1;}if (error != 0) {printf("Digits in number: %d\n", digits);} else {printf("Number has more than 4 digits....\n");}return 0;}
For most of the digits the program behaves as it is expected to behave. However strange results come when a number is preceded by 0. For example if a user enters 02 as the number, instead of telling that the program has 2 digits (or 1 digit which is more expected result), it tells that the number has more than 4 digits.

 

ahsan@ahsan-virtual-machine:~/Documents/chapter5$ ./q1Program: Calculate how many digits in a numberEnter a number with maximum 4 digits: 02Number has more than 4 digits....

Doing some analysis, the main questions becomes that:

Isn't 02 greater than 0 in C's preferences?

 

If yes, then why is the program behaving strangely and if not then why is it not made greater than 0 while in the real world it is ??

Edited by Ahsaniqbal111 (see edit history)

Share this post


Link to post
Share on other sites

This was just en axeample. In order to go more indepth, have a look at the "integer" syntax. Clearly, when entered as &number, 02 is not seen as the integer "2"

Yordan, Don't you think that if "02" is not taken as an integer code must give an error and execute else part of code (i.e. error=1 ).

 

Second thing is that when I run this code on paper (Win 8 not support c++ compiler) and enter different value 20, 200, 2000 gives same result (i.e. Number has more than 4 digits....).

 

When I further looked at code deeply find something wrong in conditional statement of If function at line # 24 (i.e. If (error !=0).Oh!

 

Yes exactly it is wrong, statement is error !=0 that means condition is true when value of variable error is not equal to zero (i.e. error =1), will go to line # 25 (first printf function). But since we have no error that means value of variable error is equal to zero and hence program always go to line # 26 (i. e. second printf function).

 

So, Ahsan problem is not related to numbers but it is related to wrong statement used in If function. There is two you can change your code first one is that by interchanging value of variable error at line # 8 and # 21 but it is not seems like a good coder and second one is that you can replace If ( error != 0) with If (error =0) rerun program and you will get result that you expected.

 

Hope I cleared your doubt somewhat.

Edited by agyat (see edit history)

Share this post


Link to post
Share on other sites

The problem is that there is no real error, with the syntax we use. We are casting values, and print pointers.
We say


int number;

and further, we read a decimal, and affect it to a pointer.


scanf("%d", &number);


So we read a decimal value, we say that it's an integer, and we use it as an array of variable length. And we input a value which is not a decimal, so the cast to integer does not give the result we want, so all our "if" statements are based on false assumptions.
In a perfect world we should investigate the default assignment for "02", is it translated to "0x2" ? Then it's value is 2 hexadecimal in a 64-bit machine? What is it worth? 128?
I guess that the correct way for such a problem is not overriding the natural values with artificial forced casts.
We should read the integer as an integer value.
Then, if we want to see the digits, we should explicitly find the number of digits, as we learned at school when we were five-years old : we divide by 10, and then by ten, until nothing remains.
And if we want to put the thing inside an array, we should declare the array, with it's real length.
If we could do that way, we would have a standard error processing at each step of the program : input an integer, error because it's not integer, et caetera.

Share this post


Link to post
Share on other sites

This was just en axeample. In order to go more indepth, have a look at the "integer" syntax. Clearly, when entered as &number, 02 is not seen as the integer "2"

 


So what you are trying to say is that 02 and 2 are the same for C but it is the manner of input which has the problem. Well, I certainly agree with the first part because after I made the first post, I created a simple program to check whether 2 and 02 are equal or not (when stored in variables as well as directly) and the answer was yes. But because of my very limited knowledge, I can't say anything about the second part. I guess I will have to give more more time studying the language and probably at some time I would be able to understand why they behave differently when entered in this way...

 

Or if the concept is simple, you can throw some light on it @yordan and help me understand it......

 

(Sorry for responding so late.... I was tremendously caught in my real life)

 

Second thing is that when I run this code on paper (Win 8 not support c++ compiler) and enter different value 20, 200, 2000 gives same result (i.e. Number has more than 4 digits....).

 


oh!.... I can confirm that the program gives wrong answer when the value entered is 200..... Thought I really don't understand what "run this code on paper" means.... (please throw some light on this @agyat)....

 

When I further looked at code deeply find something wrong in conditional statement of If function at line # 24 (i.e. If (error !=0).Oh!

 

Yes exactly it is wrong, statement is error !=0 that means condition is true when value of variable error is not equal to zero (i.e. error =1), will go to line # 25 (first printf function). But since we have no error that means value of variable error is equal to zero and hence program always go to line # 26 (i. e. second printf function).

 

So, Ahsan problem is not related to numbers but it is related to wrong statement used in If function. There is two you can change your code first one is that by interchanging value of variable error at line # 8 and # 21 but it is not seems like a good coder and second one is that you can replace If ( error != 0) with If (error =0) rerun program and you will get result that you expected.

 

Hope I cleared your doubt somewhat.

 


First... thanks @agyat for pointing towards the error... But correcting it as per your instruction has created another dilemma.... The program still doesn't display the correct answer... Its a complete mess.....

can you please modify the program so that it uses the same approach as I have used but displays accurate results..

Share this post


Link to post
Share on other sites

"run this code on paper" means.... (please throw some light on this @agyat)....

Oh! It is just nothing, actually I do not have any compiler installed on any of my machine. I did write your code on a piece of paper and checked it out line by line for results. So, that was just indication for you that I might be wrong.

First... thanks @agyat for pointing towards the error... But correcting it as per your instruction has created another dilemma.... The program still doesn't display the correct answer... Its a complete mess.....can you please modify the program so that it uses the same approach as I have used but displays accurate results..


Sorry for making you confused. Here is corrected code ..


#include <stdio.h>int main(void) {printf("Program: Calculate how many digits in a number\n");int number;printf("Enter a number with maximum 4 digits: ");scanf("%d", &number);int digits;int error = 0; // We are Assuming here that code do no have any error.if (number >= 0 && number <=9) {digits = 1;}else if (number >= 10 && number <= 99) {digits = 2;}else if (number >= 100 && number <= 999) {digits = 3;}else if (number >= 1000 & number <= 9999) {digits = 4;}else {error = 1; // There is some error in code or number is having more than 4 digits.}// Display number of Digits if there is no error in code OR Display "Number has more than 4 digits" if there is error in code or input value is of more than 4 digits.if (error = 0) { printf("Digits in number: %d\n", digits);} else {printf("Number has more than 4 digits....\n");}return 0;}

Edited by agyat (see edit history)

Share this post


Link to post
Share on other sites

Thought I really don't understand what "run this code on paper" means.... (please throw some light on this @agyat)....

Ha-ha! lucky young rich generation, which even does not know what "no computer available, use your brain, a paper and a pencil" means! :lol:

Share this post


Link to post
Share on other sites

Oh! It is just nothing, actually I do not have any compiler installed on any of my machine. I did write your code on a piece of paper and checked it out line by line for results. So, that was just indication for you that I might be wrong.

Wow!... Its quite funny how some simple things can become hard to understand some times. And I have to say that "running on paper" does actually work... at least for simple programs...

By the way.... thanks for the corrected version of the program. by running the program and looking deep into it, I found why I wasn't able to correct the program following your guideline. Actually there was a syntax error in the program as shown below...

else if (number >= 1000 & number <= 9999) {if (error = 0) {

As you can see, there is only one ampersand sine in the top line... This line was in the same form in my original program and I guess it was the culprit...

The second line wasn't present in my program. It is your modification that corrects the programs' output (if the syntax error is corrected in it)..

So thanks and finally the program now works correctly.... And I have to say that it concludes that 2 and 02 are the same in C no matter if they are used directly or with pointers....


Ha-ha! lucky young rich generation, which even does not know what "no computer available, use your brain, a paper and a pencil" means! :lol:


Lol... as I mentioned above... its quite funny actually... :lol:

Share this post


Link to post
Share on other sites

I ran the program with eclipse c/c++ IDE but unfortunately I had some issues... Please see the topic : http://forums.xisto.com/topic/55347-c-development-with-eclipse-ide-problem-with-scanf/
'>C Development with eclipse -- problem with scanf
. and please guide me if you can.....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.