This Code convert a decimal number i to its binary ... Along with storing its value in a array ans.
In This below code i used ans size 100 . Now when i enter input as 0.5 it gives a weird output . But if i change the size of Array to 1000 the it's giving correct output can anyone explain this behaviour.
Code with ans Size 100
#include <stdio.h>
int main() {
float n;
printf ("Enter Decimal Number:");
scanf("%f", &n);
// Integer Part of Number
int ip = n;
// To store the Binary Number
char ans[100];
// Index of array
int i = 0;
// For integer part
while(ip != 1 && ip != 0) {
if ((ip % 2) == 0) {
ans[i] = '0';
}
else {
ans[i] = '1';
}
i ;
ip /= 2;
}
if (ip) {
ans[i ] = '1';
}
else{
ans[i ] = '0';
}
// Reverse the Integer Part
for (int j = 0; j < (i/2); j ) {
char temp = ans[j];
ans[j] = ans[i - j - 1];
ans[i - j - 1] = temp;
}
ans[i ] = '.';
ans[i] = '0';
// Flaoting Part of Number
float fp = n - (int) n;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j ) {
fp *= 2;
// Integer part of fp
int x = fp;
// floating part of new fp
fp = fp - x;
if (x) {
ans[i ] = '1';
}
else {
ans[i ] = '0';
}
if(!fp){
break;
}
}
}
printf("%f in Binary is %s", n, ans);
return 0;
}
OUTPUT
Enter Decimal Number:0.5
0.500000 in Binary is 0.1�
[Program finished]
Code with ans size 1000
#include <stdio.h>
int main() {
float n;
printf ("Enter Decimal Number:");
scanf("%f", &n);
// Integer Part of Number
int ip = n;
// To store the Binary Number
char ans[1000];
// Index of array
int i = 0;
// For integer part
while(ip != 1 && ip != 0) {
if ((ip % 2) == 0) {
ans[i] = '0';
}
else {
ans[i] = '1';
}
i ;
ip /= 2;
}
if (ip) {
ans[i ] = '1';
}
else{
ans[i ] = '0';
}
// Reverse the Integer Part
for (int j = 0; j < (i/2); j ) {
char temp = ans[j];
ans[j] = ans[i - j - 1];
ans[i - j - 1] = temp;
}
ans[i ] = '.';
ans[i] = '0';
// Flaoting Part of Number
float fp = n - (int) n;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j ) {
fp *= 2;
// Integer part of fp
int x = fp;
// floating part of new fp
fp = fp - x;
if (x) {
ans[i ] = '1';
}
else {
ans[i ] = '0';
}
if(!fp){
break;
}
}
}
printf("%f in Binary is %s", n, ans);
return 0;
}
OUTPUT
Enter Decimal Number:0.5
0.500000 in Binary is 0.1
[Program finished]
CodePudding user response:
Your ans
string in not null-terminated, so printing a string that is not null-terminated causes UB (undefined behavior). As to why it works when the size is 1000 vs. 100 I cannot say, but the bottom line is that undefined bahavior is undefined... anything could happen. So fix the bug as follows:
:
:
// Flaoting Part of Number
float fp = n - ip;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j ) {
fp *= 2;
// Integer part of fp
int x = (int)fp;
// floating part of new fp
fp -= x;
ans[i ] = x ? '1' : '0';
if(!fp)
break;
}
} else i ;
ans[i] = '\0';
printf("%f in Binary is %s", n, ans);
return 0;
}
CodePudding user response:
I am turning my comment into an answer.
Your ans
array is not null terminated, hence printf("%s", arr)
doesn't know where to stop reading and keeps printing whatever garbage follows. If you are going to print it with %s
append a null terminator at the end of your data in the array before printing it, or print it character by character in a loop.
You can also initialize your array as char ans[100] = { 0 }
, which assigns all elements 0
. Which also is the ASCII code for the null terminator, hence when you are done writing your characters, the next character will be a null terminator. This however, doesn't work if you re-write a smaller data to your array, previous characters that are not overwritten will still be printed. You can consider using memset
to clean you array with 0
before each time you re-write it.
When it comes to the difference between 100 and 1000 element array, I guess your compiler happens to allocate 100 element array in a part of the memory containing garbage, and 1000 element array in a part that happens to contain 0. This is unpredictable and as you can see, can mislead you. Never, ever use your variables uninitialized.