problem abt printf on Turboc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muralipmanohar@hotmail.com

    #1

    problem abt printf on Turboc

    Hello all ,
    I need a help on this code
    kindly help me out
    for the below code I worked on the Turboc
    the result I was expecting was different from what has been printed I
    have indicated the line with " /*this one*/" line no 10
    I did expected some junk for the first %ld and 6553 for the second %d
    ,but it printed in different way I am also pasting the result printed ,
    it is below the code .
    I am totally confused because I got one answer that was it depends on
    compiler but I am not satisfied .can any body kind hearted people help
    me with proper explanation

    thanks and regards

    /*code */

    1 int main(void)
    2 {
    3 unsigned int un = 3000000000; /* system with 32-bit int */
    4 short end = 200; /* and 16-bit short */
    5 unsigned int big = 6553;
    6 long long verybig = 12345678908642;
    7 clrscr();
    8 printf("un = %u and not %d\n", un, un);
    9 printf("end = %hd and %d\n", end, end);
    10 printf("big = %ld and not %d\n", big,big); /*this one */
    11 printf("verybig = %lld and not %ld\n", verybig, verybig);
    12 getch();
    13 return 0;
    14 }


    the Output is below

    un = 24064 and not 24064
    end = 200 and 200
    big = 429463961 and not 996 /* <-why it is 996 whynot 6553*/
    verybig= 1942899938 and not 1942899938

  • Eltee

    #2
    Re: problem abt printf on Turboc

    muralipmanohar@ hotmail.com wrote:

    Thought I'd try blending in ... ;-)
    [color=blue]
    > Hello all ,
    > I need a help on this code
    > kindly help me out
    > for the below code I worked on the Turboc
    > the result I was expecting was different from what has been printed I
    > have indicated the line with " /*this one*/" line no 10
    > I did expected some junk for the first %ld and 6553 for the second %d
    > ,but it printed in different way I am also pasting the result printed ,
    > it is below the code .
    > I am totally confused because I got one answer that was it depends on
    > compiler but I am not satisfied .can any body kind hearted people help
    > me with proper explanation
    >
    > thanks and regards
    >
    > /*code */
    >
    > 1 int main(void)
    > 2 {
    > 3 unsigned int un = 3000000000; /* system with 32-bit int */
    > 4 short end = 200; /* and 16-bit short */
    > 5 unsigned int big = 6553;
    > 6 long long verybig = 12345678908642;[/color]

    <error message="OT in c.l.c" cause="Not ISO C"
    redirect="borla nd.public.cpp.t urbocpp">
    [color=blue]
    > 7 clrscr();[/color]

    </error>
    [color=blue]
    > 8 printf("un = %u and not %d\n", un, un);
    > 9 printf("end = %hd and %d\n", end, end);
    > 10 printf("big = %ld and not %d\n", big,big); /*this one */
    > 11 printf("verybig = %lld and not %ld\n", verybig, verybig);
    > 12 getch();
    > 13 return 0;
    > 14 }
    >
    >
    > the Output is below
    >
    > un = 24064 and not 24064
    > end = 200 and 200
    > big = 429463961 and not 996 /* <-why it is 996 whynot 6553*/
    > verybig= 1942899938 and not 1942899938
    >[/color]

    Comment

    • muralipmanohar@hotmail.com

      #3
      Re: problem abt printf on Turboc

      I didnt get you
      if it is any error please comment the lines "clrscr()" and "getch()"
      thanks

      Comment

      • Michael Mair

        #4
        Re: problem abt printf on Turboc



        muralipmanohar@ hotmail.com wrote:[color=blue]
        > I didnt get you
        > if it is any error please comment the lines "clrscr()" and "getch()"[/color]

        Please ignore "Eltee". He/she/it is a newly acquired troll round here.

        - Michael
        --
        E-Mail: Mine is a gmx dot de address.

        Comment

        • Michael Mair

          #5
          Re: problem abt printf on Turboc



          muralipmanohar@ hotmail.com wrote:[color=blue]
          > Hello all ,
          > I need a help on this code
          > kindly help me out
          > for the below code I worked on the Turboc
          > the result I was expecting was different from what has been printed I
          > have indicated the line with " /*this one*/" line no 10
          > I did expected some junk for the first %ld and 6553 for the second %d
          > ,but it printed in different way I am also pasting the result printed ,
          > it is below the code .
          > I am totally confused because I got one answer that was it depends on
          > compiler but I am not satisfied .can any body kind hearted people help
          > me with proper explanation
          >
          > thanks and regards
          >
          > /*code */
          >[/color]
          0a #include <stdio.h>
          [color=blue]
          > 1 int main(void)
          > 2 {
          > 3 unsigned int un = 3000000000; /* system with 32-bit int */
          > 4 short end = 200; /* and 16-bit short */
          > 5 unsigned int big = 6553;
          > 6 long long verybig = 12345678908642;
          > 7 clrscr();[/color]
          7 printf("\v\v\v" );[color=blue]
          > 8 printf("un = %u and not %d\n", un, un);
          > 9 printf("end = %hd and %d\n", end, end);
          > 10 printf("big = %ld and not %d\n", big,big); /*this one */
          > 11 printf("verybig = %lld and not %ld\n", verybig, verybig);
          > 12 getch();[/color]
          13 getchar();[color=blue]
          > 13 return 0;
          > 14 }
          >
          >
          > the Output is below
          >
          > un = 24064 and not 24064
          > end = 200 and 200
          > big = 429463961 and not 996 /* <-why it is 996 whynot 6553*/
          > verybig= 1942899938 and not 1942899938[/color]

          Are you sure about the size of your data types?
          I would rather use

          0b #include <limits.h>

          and initialise the variables accordingly:

          3 unsigned in un = UINT_MAX - 1;
          6 long long verybig = LLONG_MAX;

          This is portable.

          Another thing: If long int is a wider data type than int,
          then the arguments of printf() are evaluated wrongly in line 10.
          In addition, I would either output unsigned int or make big signed
          int:

          10 printf("big = %u and not %lu\n", big,big); /*this one */


          As to the problems with long long:
          Are you sure that your compiler is C99 compliant?
          Try
          0c #ifndef __STDC_VERSION_ _
          0d # error Error: __STDC_VERSION_ _ not defined
          0e #elif __STDC_VERSION_ _ < 199901L
          0d # error Error: Not C99 compliant
          0f #endif
          Another thing: Maybe your standard library printf() does not
          support long long.


          Cheers
          Michael
          --
          E-Mail: Mine is a gmx dot de address.

          Comment

          • Mark McIntyre

            #6
            Re: problem abt printf on Turboc

            On 29 Dec 2004 04:48:53 -0800, in comp.lang.c , muralipmanohar@ hotmail.com
            wrote:
            [color=blue]
            >I am totally confused because I got one answer that was it depends on
            >compiler but I am not satisfied .can any body kind hearted people help
            >me with proper explanation[/color]

            You're lying to the compiler. The format string you pass to printf must
            match the type of the variables passed. Otherwise the results will be
            garbage. Note also that the size of short, int, long and long long depend
            on the compiler and OS. so sometimes you'll get lucky, and using the wrong
            specifier will 'work'.

            If you want to know *exactly* why its 996 not 6553, the answer lies in the
            bitpattern of how your Compiler or OS stores numbers. Thats offtopic here
            and would be different on each different OS.


            --
            Mark McIntyre
            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

            Comment

            • Eltee

              #7
              Re: problem abt printf on Turboc

              muralipmanohar@ hotmail.com wrote:[color=blue]
              > I didnt get you
              > if it is any error please comment the lines "clrscr()" and "getch()"
              > thanks
              >[/color]

              The thing is that your code is not ISO C, and that makes it waaaaaaaaaaaay off
              topic in this NG. Maybe if you post the question to borland.public. cpp.turbocpp,
              you'd get proper help there.

              Comment

              • Eltee

                #8
                Re: problem abt printf on Turboc

                Mark McIntyre wrote:
                [color=blue]
                > Thats offtopic here[/color]

                See? Toldya! ;-)

                Comment

                • Eltee

                  #9
                  Re: problem abt printf on Turboc

                  Michael Mair wrote:[color=blue]
                  >
                  >
                  > muralipmanohar@ hotmail.com wrote:
                  >[color=green]
                  >> I didnt get you
                  >> if it is any error please comment the lines "clrscr()" and "getch()"[/color]
                  >
                  >
                  > Please ignore "Eltee". He/she/it is a newly acquired troll round here.[/color]

                  That would be "it", Michael. Thanks for your concern.

                  As for the "newly acquired troll" ... well, like I said, just trying to blend
                  in. After a week of semi-lurking I realized that if there's a hint of a chance
                  of your problem not being well within the limits of ISO C specification, what
                  you're supposed to get is, in a word, "buggeroff" . That's my impression, anyway.
                  Still learning, though. ;-)

                  Comment

                  • Richard Bos

                    #10
                    Re: problem abt printf on Turboc

                    Eltee <eltee@hotmail. com> wrote:
                    [color=blue]
                    > muralipmanohar@ hotmail.com wrote:
                    >
                    > Thought I'd try blending in ... ;-)[/color]

                    You fail to do so.
                    [color=blue][color=green]
                    > > 1 int main(void)
                    > > 2 {
                    > > 3 unsigned int un = 3000000000; /* system with 32-bit int */
                    > > 4 short end = 200; /* and 16-bit short */
                    > > 5 unsigned int big = 6553;
                    > > 6 long long verybig = 12345678908642;[/color]
                    >
                    > <error message="OT in c.l.c" cause="Not ISO C"
                    > redirect="borla nd.public.cpp.t urbocpp">
                    >[color=green]
                    > > 7 clrscr();[/color]
                    >
                    > </error>[/color]

                    You are an imbecile, and intentionally so to boot. It is immediately
                    obvious to anyone who knows C at all that the problem is not with that
                    line...
                    [color=blue][color=green]
                    > > 8 printf("un = %u and not %d\n", un, un);
                    > > 9 printf("end = %hd and %d\n", end, end);
                    > > 10 printf("big = %ld and not %d\n", big,big); /*this one */
                    > > 11 printf("verybig = %lld and not %ld\n", verybig, verybig);[/color][/color]

                    ....but with these. The OP is invoking undefined behaviour by sending
                    printf() variables of a type it doesn't expect. This has nothing to do
                    with Turbo C at all; it would be a bug under any implementation. The
                    only thing that's Turbo-C-specific about this is the precise result of
                    this UB; IOW, only the specific output is Turbo-dependent, not the fact
                    that the output is not as expected.

                    Richard

                    Comment

                    • Eltee

                      #11
                      Re: problem abt printf on Turboc

                      Richard Bos wrote:[color=blue]
                      > Eltee <eltee@hotmail. com> wrote:
                      >
                      >[color=green]
                      >>muralipmanoha r@hotmail.com wrote:
                      >>
                      >>Thought I'd try blending in ... ;-)[/color]
                      >
                      >
                      > You fail to do so.[/color]

                      Oops! Back to the drawing board. :-|

                      Comment

                      • Michael Mair

                        #12
                        Re: problem abt printf on Turboc

                        Eltee wrote:[color=blue]
                        > Mark McIntyre wrote:
                        >[color=green]
                        >> Thats offtopic here[/color]
                        >
                        > See? Toldya! ;-)[/color]

                        *sigh* You quoted out of context; Mark did not say anything
                        about the stuff _you_ saw as making the whole thing offtopic.

                        Moreover, he gave an answer to the _topical_ problem the OP
                        encountered.
                        The only thing which he marked OT was the platform-specific
                        representation of integers.

                        But I guess you will just "semi-lurk" a while longer without
                        being helpful or topical at all. *PLONK*


                        -Michael
                        --
                        E-Mail: Mine is a gmx dot de address.

                        Comment

                        • Eltee

                          #13
                          Re: problem abt printf on Turboc

                          Michael Mair wrote:[color=blue]
                          > Eltee wrote:
                          >[color=green]
                          >> Mark McIntyre wrote:
                          >>[color=darkred]
                          >>> Thats offtopic here[/color]
                          >>
                          >>
                          >> See? Toldya! ;-)[/color]
                          >
                          >
                          > *sigh* You quoted out of context;[/color]

                          I was trying to be ironic. In my first reply in this thread as well as in reply
                          to your post.
                          [color=blue]
                          > Mark did not say anything
                          > about the stuff _you_ saw as making the whole thing offtopic.
                          >
                          > Moreover, he gave an answer to the _topical_ problem the OP
                          > encountered.
                          > The only thing which he marked OT was the platform-specific
                          > representation of integers.
                          >
                          > But I guess you will just "semi-lurk" a while longer without
                          > being helpful or topical at all. *PLONK*[/color]

                          Oh puh-lease fer-tha-luv-o'-gawd, not a *PLONK*! Is it for life or am I eligible
                          for a parole in, say, 10 to 15? >:->

                          Being the holiday season an' awl, I'm going to go with it and not retaliate. So,
                          Michael, have a happy new year and ...read you soon. ;-)

                          Comment

                          • Mark McIntyre

                            #14
                            Re: problem abt printf on Turboc

                            On Wed, 29 Dec 2004 16:31:34 +0100, in comp.lang.c , Eltee
                            <eltee@hotmail. com> wrote:
                            [color=blue]
                            >Mark McIntyre wrote:
                            >[color=green]
                            >> Thats offtopic here[/color]
                            >
                            >See? Toldya! ;-)[/color]

                            You have added exactly zero to this thread, apart from fuckwittedness. If
                            you must fiddle with your plonker please do it in private, nobody here
                            thinks its big or clever.

                            --
                            Mark McIntyre
                            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                            Comment

                            • Mark McIntyre

                              #15
                              Re: problem abt printf on Turboc

                              On Wed, 29 Dec 2004 18:03:52 +0100, in comp.lang.c , Eltee
                              <eltee@hotmail. com> wrote:
                              [color=blue]
                              >Michael Mair wrote:[color=green]
                              >> Eltee wrote:
                              >>[color=darkred]
                              >>> Mark McIntyre wrote:
                              >>>
                              >>>> Thats offtopic here
                              >>>
                              >>> See? Toldya! ;-)[/color]
                              >>
                              >> *sigh* You quoted out of context;[/color]
                              >
                              >I was trying to be ironic. In my first reply in this thread as well as in reply
                              >to your post.[/color]

                              Your past record of stupid posts works against you. I believe you were
                              warned about this and yet insisted on your right to be a fool.
                              [color=blue][color=green]
                              >> But I guess you will just "semi-lurk" a while longer without
                              >> being helpful or topical at all. *PLONK*[/color]
                              >
                              >Oh puh-lease fer-tha-luv-o'-gawd, not a *PLONK*![/color]

                              Irony aside, its your loss if you get plonked by the regulars.

                              --
                              Mark McIntyre
                              CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                              CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                              Comment

                              Working...