i wrote a program that was supposed to run a function that makes a conversion of inter digits to binary
when i try to compile the program, the following errors come up:
int_to_bin.c:33: error: conflicting types for 'counv'
int_to_bin.c:28: error: previous implicit declaration of 'counv' was here
int_to_bin.c: In function 'counv':
int_to_bin.c:87: warning: return makes integer from pointer without a cast
int_to_bin.c:87: warning: function returns address of local variable
here is the code:
Code:
/********************************************************
* *
* FILE: ch10_ex4.c *
* CREATED: 8/15/07 *
* BY: bpf *
* *
* this program will run a function that converts an *
* integer to the binary code for each of the *
* integer's digits *
* *
********************************************************/
#include<stdio.h>
#include<string.h>
main()
{
char line[100];
char itgr[100]; /* the integer value of the input number */
char conv(); /* function prototype */
/* input number */
(void)printf("enter an integer: ");
(void)fgets(line, sizeof(line), stdin);
(void)sscanf(line, "%s", &itgr);
/* print new number */
(void)printf("%c\n", counv(itgr));
return(0);
}
char counv(char itgr[]) {
int index; /* index into integer string */
int length = strlen(itgr); /* length of string */
char bin[100]; /* string for binary number */
/* starting from the last digit, concatonize the binary equivalent to the binary string */
for (index = length - 1; index < 0; index--) {
switch(itgr[index]) {
case '1':
(void)strcat(bin, "0001");
break;
case '2':
(void)strcat(bin, "0010");
break;
case '3':
(void)strcat(bin, "0011");
break;
case '4':
(void)strcat(bin, "0100");
break;
case '5':
(void)strcat(bin, "0101");
break;
case '6':
(void)strcat(bin, "0110");
break;
case '7':
(void)strcat(bin, "0111");
break;
case '8':
(void)strcat(bin, "1000");
break;
case '9':
(void)strcat(bin, "1001");
break;
default:
/* do nothing */
break;
} /* closes switch statement */
} /* closes for loop */
return(bin);
}