Your indentation is STILL a mess.
You don't call your function from main, so - really - nothing happens.
Your indentation is STILL a mess.
You don't call your function from main, so - really - nothing happens.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
This is what Salem is saying!
Compare my changes to your previous code:
You need to read this article on C code indentation style!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[13]; printf("Enter a number and base\n"); scanf("%d %d", &i, &b); itoa(i, array, b); printf("String : %s\n", array); return 0; }
The remaining issue is that you might overflow the array with a large number and base 2! 13 chars may be too short.
Last edited by rstanley; 10-09-2021 at 08:13 AM.
Your print statement is commented out.
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
Last edited by leo2008; 10-11-2021 at 07:40 AM.
I identified problem was due to 2 files with the same name, after renaming it, i was able to resolve compile issues.
Tested and it shows below output. if I provide negative number it gives strange results.
Enter a number and base
1234 2
String : 10011010010
Enter a number and base
-1234 2
String : /00//0/00/0
Enter a number and base
80 2
String : 1010000
The following change corrects the display of the '1', but you still have issues:Input: -12 2Code:#include <stdio.h> #include <stdlib.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){ if(input < 0){ 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; }
displays:
"String : -1100"
However, and int is 4 bytes long, and negative numbers are in twos complement, so the binary display should correctly display:
"11111111 11111111 11111111 11110100" (Endian corrected)
The MSB (Most Significant Bit) indicates a negative number.
I'll leave it to you to make the corrections to this code, and the correct conversion to other bases.
Last edited by rstanley; 10-13-2021 at 08:46 AM.
leo2008:
Even if the value -12 is being processed as a char, the output matches the LSB (Least Significant Byte) of the full integer, -12.
Using a function I wrote to process:
char ch = 12;
Output: "11110100"
Hi rstanley
Did you write any new function to handle negative values?
If I provide positive numbers there are no issues seen, with the existing logic.
Enter a number and base
12 2
String : 1100
My functions were written a couple of years ago to display the actual bit pattern of various data types, irregardless of signed or unsigned variables. Primarily for checking my code using bit masks, and other debugging uses. printf() has no format specifier for binary display.
You are processing int values. ints are 4 bytes long.
Positive numbers are in normal format.
value: 12, rad: 2
Output: [00000000 00000000 00000000 00001100] (Endian corrected)
value: -12, rad: 2
Negative numbers are stored in 2s complement format:
Output: [11111111 11111111 11111111 11110100] (Endian Corrected)
In a signed int, if the MSB (Most significant bit) is set to 1, the remainder of the bits are in 2s compliment format.
The reason for the (Endian Corrected) message to Big Endian is that data on Intel architecture is normally stored in Little Endian format:
value: 12, rad: 2
Output: [00001100 00000000 00000000 00000000] (Little Endian)
I know you are calculating an integer to ascii. But you need to handle the difference between positive, and negative values, especially for binary. My functions do no calculations, just display the actual bit pattern in memory.