Thread: urgent help needed with itoa

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,739
    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.

  2. #17
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by leo2008 View Post
    If I remove stdbool.h, I get the below errors.

    Code:
    main.c:28:1: error: unknown type name ‘bool’
       28 | bool isnegative = false;
          | ^~~~
    main.c:37:1: error: unknown type name ‘bool’
       37 | bool isnegative = true;
          | ^~~~
    Code:
    #include <stdio.h>
    #include <stdbool.h>
     
    // NOT
    
    #include <stdio.h>
    #define true 1
    #define false 0
    true and false are already defined in stdbool.h!!!

  3. #18
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    This is what Salem is saying!

    Compare my changes to your previous code:
    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;
    }
    You need to read this article on C code indentation style!

    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; 2 Weeks Ago at 08:13 AM.

  4. #19
    Registered User
    Join Date
    Apr 2021
    Posts
    54
    Your print statement is commented out.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    214
    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; 2 Weeks Ago at 07:40 AM.

  6. #21
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by leo2008 View Post
    If I test below code, it throws errors. I am not sure why these errors are coming here?
    Show us your compile command. The code compiles on the command line and executes fine for me.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    214
    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

  8. #23
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by leo2008 View Post
    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:
    Code:
    #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;
    }
    Input: -12 2
    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; 1 Week Ago at 08:46 AM.

  9. #24
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    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"

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    214
    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

  11. #26
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by leo2008 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent, help needed.
    By sinnclaus in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 06:08 AM
  2. Urgent Help needed!
    By Superstar90 in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2009, 04:21 PM
  3. urgent help needed!!!
    By yosef_yaniv in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2007, 12:36 PM
  4. Urgent! Help Needed
    By DarkManiac in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2004, 07:16 PM
  5. urgent help needed
    By david in forum C Programming
    Replies: 0
    Last Post: 11-27-2001, 08:27 AM

Tags for this Thread