Typecasting in C

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

    Typecasting in C

    Hi,
    Whenever we type in this code
    int main()
    {
    printf("%f",10) ;
    }
    we get an error. We can remove that by using #pragma directive t
    direct that to the 8087. Even after that the output is 0.00000 and no
    10.0000. Can anybody tell me why it is like that and why typecasting i
    not done in this case?
    -
    andynai
    -----------------------------------------------------------------------
    Posted via http://www.codecomments.co
    -----------------------------------------------------------------------

  • Dario (drinking coffee in the office…)

    #2
    Re: Typecasting in C

    andynaik wrote:
    [color=blue]
    > Hi,
    > Whenever we type in this code
    > int main()
    > {
    > printf("%f",10) ;
    > }
    > we get an error. We can remove that by using #pragma directive to
    > direct that to the 8087. Even after that the output is 0.00000 and not
    > 10.0000. Can anybody tell me why it is like that and why typecasting is
    > not done in this case??[/color]

    %f is for double, but 10 is an int.

    Rewrite as:
    #include <stdio.h>
    int main(){printf(" %d",10);retur n 0;}
    or as:
    #include <stdio.h>
    int main(){printf(" %f",10.0);retur n 0;}

    - Dario

    Comment

    • Alex Fraser

      #3
      Re: Typecasting in C

      "andynaik" <andynaik.18ltt q@mail.codecomm ents.com> wrote in message
      news:2e08dd6cca e1bbebc6cdc92dc 381e629@news.th enewsgroups.com ...[color=blue]
      > Whenever we type in this code
      > int main()
      > {
      > printf("%f",10) ;
      > }
      > we get an error. We can remove that by using #pragma directive to
      > direct that to the 8087.[/color]

      Means nothing to me (compiler specifics are off-topic here).
      [color=blue]
      > Even after that the output is 0.00000 and not 10.0000. Can anybody tell
      > me why it is like that and why typecasting is not done in this case??[/color]

      Read section 15 in the FAQ. Post back if you still have questions.


      Alex


      Comment

      • Emmanuel Delahaye

        #4
        Re: Typecasting in C

        In 'comp.lang.c', andynaik <andynaik.18ltt q@mail.codecomm ents.com> wrote:
        [color=blue]
        > Hi,
        > Whenever we type in this code
        > int main()
        > {
        > printf("%f",10) ;
        > }
        > we get an error.[/color]

        Compiling MAIN.C:
        Warning MAIN.C 4: Call to function 'printf' with no prototype
        Warning MAIN.C 5: Function should return a value
        Linking EXE\PROJ.EXE:

        This code invokes an undefined behaviour (UB). It is mandatory to supply a
        prototype when using a variadic function. Add

        #include <stdio.h>

        That said, 10 is a int. "%f" is expecting a double. If you want to printf a
        double, use 10.0, or the (double) typecast.

        Finally, some old Borland C compilers anre buggy and forget to link the
        floating point library un such a case. This little hack can help:

        #include <stdio.h>

        #ifdef __BORLANDC__
        /* The pesky "floating point formats not linked" killer hack : */
        extern unsigned _floatconvert;
        #pragma extref _floatconvert
        #endif

        int main (void)
        {
        printf ("%f\n", 10.0);

        return 0;
        }

        --
        -ed- get my email here: http://marreduspam.com/ad672570
        The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
        C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
        FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

        Comment

        • jacob navia

          #5
          Re: Typecasting in C


          "Alex Fraser" <me@privacy.net > a écrit dans le message de
          news:2kcp3fFnvq bU1@uni-berlin.de...[color=blue]
          > "andynaik" <andynaik.18ltt q@mail.codecomm ents.com> wrote in message
          > news:2e08dd6cca e1bbebc6cdc92dc 381e629@news.th enewsgroups.com ...[color=green]
          > > Whenever we type in this code
          > > int main()
          > > {
          > > printf("%f",10) ;
          > > }
          > > we get an error. We can remove that by using #pragma directive to
          > > direct that to the 8087.[/color]
          >
          > Means nothing to me (compiler specifics are off-topic here).
          >[color=green]
          > > Even after that the output is 0.00000 and not 10.0000. Can anybody tell
          > > me why it is like that and why typecasting is not done in this case??[/color]
          >
          > Read section 15 in the FAQ. Post back if you still have questions.
          > http://www.eskimo.com/~scs/C-faq/s15.html
          >
          > Alex
          >[/color]

          The FAQ should mention that some compilers DO test the arguments
          for sprintf/printf/fprintf etc for validity.

          This CAN be done. lcc-win32 does it, and other compilers too.

          For instance the above code produces
          Warning tx.c: 4 printf argument mismatch for format f. Expected double got
          int
          0 errors, 1 warnings


          Comment

          • jacob navia

            #6
            Re: Typecasting in C

            Some compilers DO test the arguments of printf for
            validity.
            Under lcc-win32 the above code produces:

            Warning tx.c: 4 printf argument mismatch for format f. Expected double got
            int
            0 errors, 1 warnings


            Comment

            • Dan Pop

              #7
              Re: Typecasting in C

              In <cbrhh5$ro2$1@n ews-reader3.wanadoo .fr> "jacob navia" <jacob@jacob.re mcomp.fr> writes:

              [color=blue]
              >The FAQ should mention that some compilers DO test the arguments
              >for sprintf/printf/fprintf etc for validity.
              >
              >This CAN be done.[/color]

              ONLY if the compiler can "see" the contents of the format string.
              Which is usually the case, but exceptions are not that rare either.

              At some point, gcc had the annoying habit of warning if it couldn't
              perform such a check (if enabled), because the format was not a
              string literal.
              [color=blue]
              >lcc-win32 does it, and other compilers too.[/color]

              Other compilers do a much better job than lcc-win32, when it comes to
              format string consistency checks. See below.
              [color=blue]
              >For instance the above code produces
              >Warning tx.c: 4 printf argument mismatch for format f. Expected double got
              >int
              >0 errors, 1 warnings[/color]

              OTOH, lcc-win32 silently accepts printf("%d\n", "foo"), which is very
              bad, considering the relative positions of the D and S keys on most
              keyboard layouts (i.e. it is a fairly frequent mistake to type d
              when you mean s).

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

              Comment

              • jacob navia

                #8
                Re: Typecasting in C


                "Dan Pop" <Dan.Pop@cern.c h> a écrit dans le message de
                news:cbrqt4$29a $1@sunnews.cern .ch...[color=blue]
                > OTOH, lcc-win32 silently accepts printf("%d\n", "foo"), which is very
                > bad, considering the relative positions of the D and S keys on most
                > keyboard layouts (i.e. it is a fairly frequent mistake to type d
                > when you mean s).[/color]

                I pondered a long time about that one, since it is perfectly legal to
                do:

                char *p;
                ....

                printf("The address is %d\n",p);

                specially in debugging code.

                Granted, this is weird, but how to discriminate between
                legal and wrong usage?

                But maybe you are right. I added a warning when the "d" format
                is used with a pointer.

                The "x" format will NOT provoke any warnings.

                But this is at the limit of what a compiler can do.



                Comment

                • Dan Pop

                  #9
                  Re: Typecasting in C

                  In <cbrvkb$bbr$1@n ews-reader1.wanadoo .fr> "jacob navia" <jacob@jacob.re mcomp.fr> writes:

                  [color=blue]
                  >"Dan Pop" <Dan.Pop@cern.c h> a écrit dans le message de
                  >news:cbrqt4$29 a$1@sunnews.cer n.ch...[color=green]
                  >> OTOH, lcc-win32 silently accepts printf("%d\n", "foo"), which is very
                  >> bad, considering the relative positions of the D and S keys on most
                  >> keyboard layouts (i.e. it is a fairly frequent mistake to type d
                  >> when you mean s).[/color]
                  >
                  >I pondered a long time about that one, since it is perfectly legal to
                  >do: ^^^^^^^^^^^^^^^ ^^^
                  >
                  >char *p;
                  >...
                  >
                  >printf("The address is %d\n",p);[/color]

                  What have you been smoking recently?
                  [color=blue]
                  >specially in debugging code.
                  >
                  >Granted, this is weird, but how to discriminate between
                  >legal and wrong usage?[/color]
                  ^^^^^
                  Can I have a chapter and verse? When did they drop the following
                  paragraph from the C standard?

                  9 If a conversion specification is invalid, the behavior is
                  undefined. If any argument is not the correct type for the
                  corresponding conversion specification, the behavior is undefined.

                  What is the type expected by %d? What is the type of p?
                  So, the legal usage would be (int)p instead of a plain p, right?
                  [color=blue]
                  >But maybe you are right. I added a warning when the "d" format
                  >is used with a pointer.
                  >
                  >The "x" format will NOT provoke any warnings.[/color]

                  Which is just as bad, especially given that x is also a typo candidate
                  for s.
                  [color=blue]
                  >But this is at the limit of what a compiler can do.[/color]

                  In your humble opinion, what is the purpose of the %p conversion
                  specification? Why support *anything else* for displaying pointer values?

                  And if a user *really* wants to use the extra flexibility of the
                  signed or unsigned integer conversion descriptors, what is preventing
                  him from casting the pointer to the desired type?

                  As I said in other threads, you have nothing to lose by using gcc's
                  behaviour as a guide. If you can benefit from the thought many competent
                  people have put into the same issue, why reinvent the wheel?

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

                  Comment

                  • Martin Ambuhl

                    #10
                    Re: Typecasting in C

                    andynaik wrote:
                    [color=blue]
                    > Hi,
                    > Whenever we type in this code
                    > int main()
                    > {
                    > printf("%f",10) ;
                    > }
                    > we get an error. We can remove that by using #pragma directive to
                    > direct that to the 8087. Even after that the output is 0.00000 and not
                    > 10.0000. Can anybody tell me why it is like that and why typecasting is
                    > not done in this case??[/color]

                    Because you forgot
                    #include <stdio.h>
                    You also forgot to terminate the last line of output with an end-of-line
                    character.

                    The first mistake, omission of the prototype for a variadic function, is
                    always an error. The second is an error in code designed to be
                    portable, and may result in behavior you were not expecting.

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: Typecasting in C

                      jacob navia wrote:
                      [color=blue]
                      > "Dan Pop" <Dan.Pop@cern.c h> a écrit dans le message de
                      > news:cbrqt4$29a $1@sunnews.cern .ch...
                      >[color=green]
                      >>OTOH, lcc-win32 silently accepts printf("%d\n", "foo"), which is very
                      >>bad, considering the relative positions of the D and S keys on most
                      >>keyboard layouts (i.e. it is a fairly frequent mistake to type d
                      >>when you mean s).[/color]
                      >
                      >
                      > I pondered a long time about that one, since it is perfectly legal to
                      > do:
                      >
                      > char *p;
                      > ...
                      >
                      > printf("The address is %d\n",p);[/color]

                      The specifier for a pointer is "%p" and it expects a (void *) argument.
                      This supposedly "legal" line should be
                      printf("The address is %p\n", (void *)p);

                      Please stop misleading people who might think you know what you're
                      talking about.
                      [color=blue]
                      >
                      > specially in debugging code.
                      >
                      > Granted, this is weird, but how to discriminate between
                      > legal and wrong usage?[/color]

                      You "legal" usage is _wrong_. There is no need to discriminate between
                      your error and that which is wrong.

                      Comment

                      • jacob navia

                        #12
                        Re: Typecasting in C

                        The expression

                        printf("the address is: 0x%x\n",p);

                        where p is some pointer appears in several million lines in
                        existing code.

                        The warnings can become a nuisance and people would stop
                        using this feature. Personally I think warnings should be
                        kept to the essential ones, warnings that would uncover a
                        possible error.

                        Strictly speaking you should use %p, but I have almost
                        never seen it in debugging code, where this conversion is
                        used.

                        To the contrary of your expectations, I work to make a usable
                        compiler, not one that will please the purists around c.l.c




                        Comment

                        • Randy Howard

                          #13
                          Re: Typecasting in C

                          In article <cbrvkb$bbr$1@n ews-reader1.wanadoo .fr>, jacob@jacob.rem comp.fr
                          says...[color=blue]
                          > I pondered a long time about that one, since it is perfectly legal to
                          > do:
                          >
                          > char *p;
                          > ...
                          >
                          > printf("The address is %d\n",p);
                          >
                          > specially in debugging code.[/color]

                          I just lost any faith I might have had in lcc-win32. Thanks for
                          the flashing warning label.

                          Comment

                          • Dan Pop

                            #14
                            Re: Typecasting in C

                            In <2kdhpoF11tmdU1 @uni-berlin.de> Martin Ambuhl <mambuhl@earthl ink.net> writes:
                            [color=blue]
                            >andynaik wrote:
                            >[color=green]
                            >> Hi,
                            >> Whenever we type in this code
                            >> int main()
                            >> {
                            >> printf("%f",10) ;
                            >> }
                            >> we get an error. We can remove that by using #pragma directive to
                            >> direct that to the 8087. Even after that the output is 0.00000 and not
                            >> 10.0000. Can anybody tell me why it is like that and why typecasting is
                            >> not done in this case??[/color]
                            >
                            >Because you forgot
                            >#include <stdio.h>
                            >You also forgot to terminate the last line of output with an end-of-line
                            >character. ^^^^^^^^^^^[/color]
                            ^^^^^^^^^
                            In C, it is called new-line character, even if no line follows. You must
                            be confusing with the end-of-line *indicator* (which needs not be a
                            character).
                            [color=blue]
                            >The first mistake, omission of the prototype for a variadic function, is
                            >always an error. The second is an error in code designed to be
                            >portable, and may result in behavior you were not expecting.[/color]

                            Neither of then having anything to do with the real cause of the OP's
                            problem: type mismatch in a printf call, which will continue to manifest
                            even after including <stdio.h> and adding the new-line character.

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

                            Comment

                            • Harti Brandt

                              #15
                              Re: Typecasting in C

                              On Tue, 29 Jun 2004, jacob navia wrote:

                              jn>The expression
                              jn>
                              jn>printf("the address is: 0x%x\n",p);
                              jn>
                              jn>where p is some pointer appears in several million lines in
                              jn>existing code.
                              jn>
                              jn>The warnings can become a nuisance and people would stop
                              jn>using this feature. Personally I think warnings should be
                              jn>kept to the essential ones, warnings that would uncover a
                              jn>possible error.
                              jn>
                              jn>Strictly speaking you should use %p, but I have almost
                              jn>never seen it in debugging code, where this conversion is
                              jn>used.
                              jn>
                              jn>To the contrary of your expectations, I work to make a usable
                              jn>compiler, not one that will please the purists around c.l.c

                              That has nothing to do with purism. Ever cared to work on a machine where
                              sizeof(void *) > sizeof(int)? The nearest to correct thing to do if you
                              happen to have a printf() without %p would be to use %lx and cast the
                              pointer to an unsigned long.

                              harti

                              Comment

                              Working...