type cast from integer to char array

Discussion in 'C Programming' started by Jay, Aug 5, 2003.

  1. Jay

    Jay Guest

    How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

    thanks in advance for any help.
     
    Jay, Aug 5, 2003
    #1
    1. Advertisements

  2. sprintf, sscanf

    function will help you

    if your environment is linux(unix), man page will give about those functions
    to you.
     
    Park Sung Jae, Aug 5, 2003
    #2
    1. Advertisements

  3. Jay

    Allan Bruce Guest

    int number = 450;
    char string[10];

    sprintf(string, "%9d", number);

    HTH
    Allan
     
    Allan Bruce, Aug 5, 2003
    #3
  4. Jay

    Mike Wahler Guest

    How is sscanf going to help?

    -Mike
     
    Mike Wahler, Aug 5, 2003
    #4
  5. Jay

    Mike Wahler Guest

    program?

    You don't. Are you sure you understand what a cast is?
    It converts one type to another.

    What you're really asking is how to store the textual
    representation of a numeric type into an array of
    characters. Use the 'sprintf()' function, which
    works just like 'printf()' except its output goes
    to a char array instead of stdout.

    #include <stdio.h>

    int main()
    {
    char array[10] = {0};
    int i = 450;
    sprintf(array, "%d", i);
    printf("%s\n", array);
    return 0;
    }

    -Mike
     
    Mike Wahler, Aug 5, 2003
    #5
  6. Jay

    Dan Pop Guest

    Maybe he wants to check the result of the conversion ;-)

    Dan
     
    Dan Pop, Aug 5, 2003
    #6
  7. Jay

    Dan Pop Guest

    Please read the FAQ *before* posting questions to this newsgroup.

    Dan
     
    Dan Pop, Aug 5, 2003
    #7
  8. Jay

    Dan Pop Guest

    ^^^^^^^^^^
    You're not solving the OP's problem: he wants the digits 450 followed by
    7 spaces, i.e. not a string. The solution is a bit more complicated:

    char array[10], buff[sizeof array + 1];
    sprintf(buff, "%-10d", 450);
    memcpy(array, buff, sizeof array);

    Then again, maybe the OP actually wanted your solution, but didn't
    formulate his question properly.

    Dan
     
    Dan Pop, Aug 5, 2003
    #8
  9. Jay

    Dan Pop Guest

    What "tailing null character"? The word "string" does not appear in OP's
    request, does it?

    Dan
     
    Dan Pop, Aug 6, 2003
    #9
  10. Jay

    Jay Guest

    This answers my question, thank you all for your help.
     
    Jay, Aug 6, 2003
    #10
  11. Jay

    Dan Pop Guest

    Then, your question was extremely poorly phrased (your example shows 3
    digits followed by 7 spaces). BTW, there is no point in overwriting
    the null character appended by the sprintf call with array[9] = '\0'.
    sprintf is quite good at generating strings...

    Dan
     
    Dan Pop, Aug 6, 2003
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.