Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As shown below, I am running a small diagnostic on how the value of dec_value changed.
If I declare dce_value together with the rest of my variables as shown, if i input 12 for the first scanf, the output post while-loop actually shows that dec_value changed to 0.

 int dec_value, bin_value, i[15], j=0, quo, rem;
//int dec_value;

 printf("Please enter the decimal number: ");
 scanf("%d", &dec_value);

 quo = dec_value;

 printf("%d \n", dec_value);

 while(j < 16)
 {
     i[j] = quo % 2;
     quo = quo / 2;
     j++;
 }

 printf("%d \n", dec_value);


What I have tried:

The only way I got around the problem is if i declared dec_value on a separate line, or if..
i[j] = quo % 2;
was nulled. And I am very puzzled as to why this is happening.
Posted
Updated 24-Mar-16 23:38pm
Comments
Richard MacCutchan 25-Mar-16 8:15am    
You have an error in your while loop. Your array is only 15 elements wide, so it should be while(j < 15). You should also add some code to break the loop when your input value reaches zero.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

In your case, there is no difference, both declaration are exactly the same.
Use the debugger to see what the code is doing.
Your program have a bug:
I is an array of size 15 which is from i[0] to i[14]
but your progran write in i[15] which write in another variable, which one is compiler dependent.
 
Share this answer
 
It makes no difference (unless you have a buggy compiler).
In any case, I would always check the scanf return value (it should be 1, in your scenario).
 
Share this answer
 
Comments
Philippe Mori 25-Mar-16 10:49am    
In this case... unless the program is buggy and lead to undefined behavior. See solution 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900