Thread: adding a char at the end of a number

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    50

    adding a char at the end of a number

    so, ive managed to add a number at the end of a char

    Code:
    char chara[30] = "";
    char charb[30] = "test ";
    
    
    //charb = "testcom";
    
    
    itoa(numa,chara,30);
    
    
    strcat(charb,chara);
    
    
    printf("%s\n",charb);
    now, I want to add a char at the end of a number, any help?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    There is no such function itoa() in the Standard Library, there is a non-standard function in some compilers as an extension.

    numa is not defined, or initialized.

    You should look st the IBM web page for the Non-Standard atoi() function to see how it is used.

    Also look at sprintf().

    You should stick to Standard Library functions and avoid most of the extensions.

    Work this out first.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    There is no such function itoa() in the Standard Library, there is a non-standard function in some compilers as an extension.

    numa is not defined, or initialized.

    You should look st the IBM web page for the Non-Standard atoi() function to see how it is used.

    Also look at sprintf().

    You should stick to Standard Library functions and avoid most of the extensions.

    Work this out first.
    Code:
    itoa(numa,chara,30);
    
    


    actually worked for me, it changed:

    Code:
    numa = 1;
    into
    Code:
     chara[30] = "1";

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    From the IBM Website for the itoa() function:
    The itoa() function coverts the integer n into a character string. The string is placed in the buffer passed, which must be large enough to hold the output. The radix values can be OCTAL, DECIMAL, or HEX. When the radix is DECIMAL, itoa() produces the same result as the following statement:

    (void) sprintf(buffer, "%d", n);
    Try a value of numa for values other than '1', and see the results. I would think it would not be what you expect.

    Please use the much simpler to use, sprintf() Standard Library function.

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    in fact, I've figured it out

    Code:
        char charx[30] = " xx";
        char chary[30] = ""; //for intx
        char charz; //not needed
    
    
    
    
        int intx;
        intx = 5;
    
    
        int inty; //not needed
    
    
        itoa(intx,chary,30);
    
    
        strcat(chary,charx);
    
    
    
    
    
    
        printf("%s", chary);
    Last edited by WaterSerpentM; 10-15-2023 at 10:30 AM.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Compare the versatility of sprintf() to multiple functions in your code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int ival = 1234;
      double dval = 1.2345;
      char ary[64] = "";
    
    // Build a string with an int, int in Hex, address of the int, and the the double
    
      sprintf(ary, "int=%d, hex=%x, addr=%p, double=%lf\n", ival, ival, (void *) &ival, dval);
    
      printf("ary = %s\n", ary);
    
      return 0;
    }
    Code:
    Output:
    ary = int=1234, hex=4d2, addr=0x7ffd3ba48224, double=1.234500
    Last edited by rstanley; 10-15-2023 at 11:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number at the end of a char
    By WaterSerpentM in forum C Programming
    Replies: 6
    Last Post: 10-15-2023, 07:54 AM
  2. Adding one to every digit of number
    By culater in forum C Programming
    Replies: 3
    Last Post: 06-04-2015, 06:36 AM
  3. Replies: 6
    Last Post: 07-15-2008, 02:27 PM
  4. What's problem of adding two binary number?
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 01-12-2007, 06:12 AM
  5. Adding .txt to a char
    By ErionD in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2002, 04:01 PM

Tags for this Thread