If I test below code, it throws errors. I am not sure why these errors are coming here?
Where are these multiple definitions for itoa, main and reverse?
Code:
#include <stdio.h>
#include <stdbool.h>
void reverse(char output[], int len)
{
int start, end;
char temp;
for(start=0, end=len-1; start < end; start++, end--) {
temp = *(output+start);
*(output+start) = *(output+end);
*(output+end) = temp;
}
}
void itoa (int input, char* output, int rad){
int i = 0;
bool isnegative = false;
if(input == 0) {
output[i] = '0';
output[i + 1] = '\0';
}
if(input < 0 && rad == 10){
isnegative = true; // NOT bool isnegative = true;
input = -input;
}
while (input != 0){
int rem = input%rad;
output[i++] = (rem > 9) ? (rem-10) + 'A' : rem + '0';
input = input/rad;
}
if(isnegative){
output[i++] = '-';
}
output[i] = '\0';
reverse(output, i);
}
int main (){
int i, b;
char array[148];
printf("Enter a number and base\n");
scanf("%d %d", &i, &b);
itoa(i, array, b);
printf("String : %s\n", array);
return 0;
}
OUTPUT:
main.c:(.text+0x0): multiple definition of `reverse'; /tmp/ccPlW4oc.o:main.c:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccAtpqk9.o: in function `itoa':
main.c:(.text+0x7a): multiple definition of `itoa'; /tmp/ccPlW4oc.o:main.c:(.text+0x7a): first defined here
/usr/bin/ld: /tmp/ccAtpqk9.o: in function `main':
main.c:(.text+0x160) : multiple definition of `main'; /tmp/ccPlW4oc.o:main.c:(.text+0x160): first defined here
collect2: error: ld returned 1 exit status