-2

Below is my code to convert the binary to decimal but my doubt is that when I enter my data as 100000 it's giving exact answer. How is it giving the correct answer even the range of integer is exceeded?

int main()
{int rem,deci=0,a=1,b;
int bin;

printf("size of int :- %d",sizeof(int));

printf("enter the binary value");
scanf("%d",&bin);
while(bin!=0)
{
    rem=bin%10;
    deci=deci+rem*a;
    a=a*2;
    bin=bin/10;
}
printf("%d \n",deci);
}
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Ravi S
  • 15
  • 3
  • 1
    What is the expected and actual output of this program? – Mohit Jain Jul 20 '15 at 07:34
  • 2
    100000 isn't outside the range of `int` on virtually all architectures/implementations these days. Unless you see the line `size of int :- 2` printed, it isn't the case for you either. – Phylogenesis Jul 20 '15 at 07:39
  • It works here. Where is the problem ? – Jabberwocky Jul 20 '15 at 07:42
  • [`sizeof` returns a `size_t` and must be printed with `%zu`](http://stackoverflow.com/q/5943840/995714). Printing with the wrong format invokes undefined behavior. – phuclv Jul 20 '15 at 08:29
  • Refer http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes for clarification on size of 'int'. – aks Jul 20 '15 at 08:58
  • 2
    It is not really a binary to decimal conversion.... You should read a string (e.g. with `fgets`), not some `int` variable `bin`, if you wanted such a conversion, – Basile Starynkevitch Jul 20 '15 at 08:59
  • sorry i was calculating only for 2^16 rather than 2^31.... this is only my mistake here – Ravi S Jul 20 '15 at 18:11

3 Answers3

0

The main problem with your code is that you're reading the binary value as an integer and then manipulating it as such.

scanf("%d",&bin);

This reads in the decimal representation of a integer and stores it in bin. So if you enter in "100000", then bin holds the decimal value 100000.

Since scanf doesn't have an option to read binary, what you need to do is read in the user's input as a string, then check each character to see if it's '0' or '1' and based on that add the proper value to deci.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

To know the maximum value in the range of int in your implementation,
please check the value of the constant INT_MAX defined in the standard header limits.h.

<limits.h>
<stdio.h>
int main(void)
{
     printf("Max. value: %d\n", INT_MAX);
}  

Also, you can obtain information about limits of other integer types. Examples:

printf("%ld", LONG_MAX); // For the maximum value of signed long      
printf("%lu", ULONG_MAX); // For the maximum value of unsigned long
pablo1977
  • 4,281
  • 1
  • 15
  • 41
-1

Updated thank's Lưu Vĩnh Phúc

The maximum accepted range for int is 4 bytes, which goes up to 2147483647. You are confusing the short type (–32,768 to 32,767), with int type (–2,147,483,648 to 2,147,483,647)

Interger Type

There is a variety of types that goes under the integer, you can know more from this article: Data Type Ranges

You can declare different int types using these statement:

 __int16 x = 16;
 __int32 x = 32;
 __int64 x = 64;

Check that int64 type whoof!

Garsallah mohamed
  • 156
  • 1
  • 1
  • 10
  • wrong. MSVC and all the compilers I know never change the size of int. The sizes must be consistent throughout the compilation – phuclv Jul 20 '15 at 08:30
  • I guess I had wrong the hole way. thank you Lu "If a value exceeds the largest integer representation, the Microsoft compiler generates an error." – Garsallah mohamed Jul 20 '15 at 08:36