type cast from integer to char array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    type cast from integer to char array

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

    thanks in advance for any help.
  • Park Sung Jae

    #2
    Re: type cast from integer to char array

    sprintf, sscanf

    function will help you

    if your environment is linux(unix), man page will give about those functions
    to you.

    "Jay" <jay4050@hotmai l.com> wrote in message
    news:6ddc1590.0 308050704.1a2a6 e58@posting.goo gle.com...[color=blue]
    > How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
    program?[color=blue]
    >
    > thanks in advance for any help.[/color]


    Comment

    • Mike Wahler

      #3
      Re: type cast from integer to char array

      Park Sung Jae <darkpgm@kornet .net> wrote in message
      news:bgofjk$nq6 $1@news1.kornet .net...[color=blue]
      > sprintf, sscanf
      >
      > function will help you[/color]

      How is sscanf going to help?

      -Mike



      Comment

      • Allan Bruce

        #4
        Re: type cast from integer to char array


        "Jay" <jay4050@hotmai l.com> wrote in message
        news:6ddc1590.0 308050704.1a2a6 e58@posting.goo gle.com...[color=blue]
        > How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
        program?[color=blue]
        >
        > thanks in advance for any help.[/color]

        int number = 450;
        char string[10];

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

        HTH
        Allan


        Comment

        • Mike Wahler

          #5
          Re: type cast from integer to char array

          Jay <jay4050@hotmai l.com> wrote in message
          news:6ddc1590.0 308050704.1a2a6 e58@posting.goo gle.com...[color=blue]
          > How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
          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



          Comment

          • Dan Pop

            #6
            Re: type cast from integer to char array

            In <bgol3l$jut$1@s lb6.atl.mindspr ing.net> "Mike Wahler" <mkwahler@mkwah ler.net> writes:
            [color=blue]
            >Park Sung Jae <darkpgm@kornet .net> wrote in message
            >news:bgofjk$nq 6$1@news1.korne t.net...[color=green]
            >> sprintf, sscanf
            >>
            >> function will help you[/color]
            >
            >How is sscanf going to help?[/color]

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

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • Dan Pop

              #7
              Re: type cast from integer to char array

              In <6ddc1590.03080 50704.1a2a6e58@ posting.google. com> jay4050@hotmail .com (Jay) writes:
              [color=blue]
              >How can I cast Integer value 450 to a char[10] array "450 "? in a c program?[/color]

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

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Dan Pop

                #8
                Re: type cast from integer to char array

                In <bgolba$4b8$1@s lb5.atl.mindspr ing.net> "Mike Wahler" <mkwahler@mkwah ler.net> writes:
                [color=blue]
                >Jay <jay4050@hotmai l.com> wrote in message
                >news:6ddc1590. 0308050704.1a2a 6e58@posting.go ogle.com...[color=green]
                >> How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color][/color]
                ^^^^^^^^^^[color=blue]
                >program?
                >
                >#include <stdio.h>
                >
                >int main()
                >{
                > char array[10] = {0};
                > int i = 450;
                > sprintf(array, "%d", i);
                > printf("%s\n", array);
                > return 0;
                >}[/color]

                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
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: type cast from integer to char array

                  In <bgpo12$2qfe$1@ news.hgc.com.hk > "Jeff" <nothing@notexi st.com> writes:

                  [color=blue]
                  >"Jay" <jay4050@hotmai l.com> wrote in message
                  >news:6ddc1590. 0308050704.1a2a 6e58@posting.go ogle.com...[color=green]
                  >> How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
                  >program?[color=green]
                  >>
                  >> thanks in advance for any help.[/color]
                  >
                  >Do you mean a char[10] array *including* the tailing null character ?[/color]

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

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • Jay

                    #10
                    Re: type cast from integer to char array

                    "Jeff" <nothing@notexi st.com> wrote in message news:<bgpo12$2q fe$1@news.hgc.c om.hk>...[color=blue]
                    > "Jay" <jay4050@hotmai l.com> wrote in message
                    > news:6ddc1590.0 308050704.1a2a6 e58@posting.goo gle.com...[color=green]
                    > > How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
                    > program?[color=green]
                    > >
                    > > thanks in advance for any help.[/color]
                    >
                    > Do you mean a char[10] array *including* the tailing null character ?
                    >
                    > char array[10];
                    > sprintf(array, "%-9d", 450);
                    > array[9] = '\0';[/color]

                    This answers my question, thank you all for your help.

                    Comment

                    • Dan Pop

                      #11
                      Re: type cast from integer to char array

                      In <6ddc1590.03080 60653.24f8e2f0@ posting.google. com> jay4050@hotmail .com (Jay) writes:
                      [color=blue]
                      >"Jeff" <nothing@notexi st.com> wrote in message news:<bgpo12$2q fe$1@news.hgc.c om.hk>...[color=green]
                      >> "Jay" <jay4050@hotmai l.com> wrote in message
                      >> news:6ddc1590.0 308050704.1a2a6 e58@posting.goo gle.com...[color=darkred]
                      >> > How can I cast Integer value 450 to a char[10] array "450 "? in a c[/color]
                      >> program?[color=darkred]
                      >> >
                      >> > thanks in advance for any help.[/color]
                      >>
                      >> Do you mean a char[10] array *including* the tailing null character ?
                      >>
                      >> char array[10];
                      >> sprintf(array, "%-9d", 450);
                      >> array[9] = '\0';[/color]
                      >
                      >This answers my question, thank you all for your help.[/color]

                      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
                      DESY Zeuthen, RZ group
                      Email: Dan.Pop@ifh.de

                      Comment

                      Working...