The binary to decimal numbering conversion C program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Converting the binary read from standard input to decimal number in C programming

To show: The binary to decimal computer numbering system conversion C program example



#include <stdio.h>

/* for strlen()/wcslen() */

#include <string.h>


/* convert binary to decimal function prototype*/

void bintodec()


char buffbin[100];

char *bin;

int i=0;

int dec = 0;

int bcount;


printf("Please enter the binary digits, 0 or/and 1.\n");

printf("Your binary digits: ");

/* bin = gets(buffbin); */

/* secure version of gets() */

bin = gets_s(buffbin, 100);

/* read the binary string length */

i = strlen(bin);


for (bcount=0; bcount<i; ++bcount)

/*if bin[bcount] is equal to 1, then 1 else 0 */

dec=dec*2+(bin[bcount]=='1'? 1:0);


printf("The decimal value of %s is %d\n", bin, dec);



int main(void)


/* just call the function without argument */


return 0;



Output example:


Please enter the binary digits, 0 or/and 1.

Your binary digits: 1100110000011100010101010101

The decimal value of 1100110000011100010101010101 is 214025557

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index