what is the reason ?

Discussion in 'C Programming' started by codergem@gmail.com, Aug 4, 2006.

  1. Guest

    It will not run. This is fine.
    main()
    {
    unsigned int i;
    for(i=1;i>-2;i--)
    printf("c aptitude");
    }

    but in the same code if i ll make unsigned int as short unsigned int
    then it will run for an infinite loop.
    what is the reason?
    thanks.
    , Aug 4, 2006
    #1
    1. Advertising

  2. wrote:
    > It will not run. This is fine.
    > main()
    > {
    > unsigned int i;
    > for(i=1;i>-2;i--)
    > printf("c aptitude");
    > }
    >
    > but in the same code if i ll make unsigned int as short unsigned int
    > then it will run for an infinite loop.
    > what is the reason?


    Turn on compiler warnings and you can probably figure this out yourself.


    Igmar
    Igmar Palsenberg, Aug 4, 2006
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote On 08/04/06 10:09,:
    > It will not run. This is fine.
    > main()
    > {
    > unsigned int i;
    > for(i=1;i>-2;i--)
    > printf("c aptitude");
    > }
    >
    > but in the same code if i ll make unsigned int as short unsigned int
    > then it will run for an infinite loop.
    > what is the reason?


    The "usual arithmetic conversions."

    --
    Eric Sosman, Aug 4, 2006
    #3
  4. Guest

    Sorry Man
    I dont know how turn ON this?
    , Aug 4, 2006
    #4
  5. wrote:
    > Sorry Man
    > I dont know how turn ON this?
    >

    "Consult your documentation."

    For example, if I was using Microsoft VC, I would consult
    http://msdn.microsoft.com or the local help files. For GCC, similar
    help is available.

    See also, <http://catb.org/~esr/faqs/smart-questions.html>
    Clever Monkey, Aug 4, 2006
    #5
  6. well in general you're hoping that an unsigned int will somehow count
    down to -2.

    Unlikely on the face of it, but you say it works fine.


    That may be, but only due to a binary coincidence.


    If you change it to a short, then it may seem to wrap around funny,
    like from 2, 1, 0, -65535.

    In any case, if you listen to the compiler warnings, you wouldnt have
    this problem.

    Styrongly suggest yuou use -warn=99 or whatever. "C" is hard enough--
    you can't afford to ignore the few warnigns the compiler can put out.
    Ancient_Hacker, Aug 4, 2006
    #6
  7. Flash Gordon Guest

    Ancient_Hacker wrote:

    Please provide context. You've been here long enough to know it is expected.

    The post you were replying to said

    | It will not run. This is fine.
    | main()
    | {
    | unsigned int i;
    | for(i=1;i>-2;i--)
    | printf("c aptitude");
    | }
    |
    | but in the same code if i ll make unsigned int as short unsigned int
    | then it will run for an infinite loop.
    | what is the reason?
    | thanks.

    > well in general you're hoping that an unsigned int will somehow count
    > down to -2.
    >
    > Unlikely on the face of it, but you say it works fine.
    >
    >
    > That may be, but only due to a binary coincidence.


    Or it could be because it does exactly what the C standard requires it
    does. Look up the usual arithmetic promotions.

    > If you change it to a short, then it may seem to wrap around funny,
    > like from 2, 1, 0, -65535.


    I somehow doubt it will wrap from 0 to -65535 under any conditions ;-)

    > In any case, if you listen to the compiler warnings, you wouldnt have
    > this problem.
    >
    > Styrongly suggest yuou use -warn=99 or whatever. "C" is hard enough--
    > you can't afford to ignore the few warnigns the compiler can put out.


    This advice is good. For details of how to get a compiler to produce
    decent warnings you are best asking on a group dedicated to your
    compiler. Or reading the manual/help, of course!
    --
    Flash Gordon
    Still sigless on this computer
    Flash Gordon, Aug 5, 2006
    #7
  8. Igmar Palsenberg wrote:
    > wrote:
    > > It will not run. This is fine.
    > > main()
    > > {
    > > unsigned int i;
    > > for(i=1;i>-2;i--)
    > > printf("c aptitude");
    > > }
    > >
    > > but in the same code if i ll make unsigned int as short unsigned int
    > > then it will run for an infinite loop.
    > > what is the reason?

    >
    > Turn on compiler warnings and you can probably figure this out yourself.


    Despite being riddled with issues, there is nothing in the code that
    requires a
    C90 compiler to issue a diagnostic. I know of at least 2 compilers that
    woundn't
    issue the particular desired warning, no matter how high you turned
    them up.

    IIRC, the gcc diagnostic warns about the cause, but doesn't elaborate
    on the
    issue. So, given the demonstration of coding so far, I seriously doubt
    that the
    OP will figure it out.

    Fortunately, the problem (actually its mirror) is covered in the FAQ...

    http://www.c-faq.com/expr/preservingrules.html

    --
    Peter
    Peter Nilsson, Aug 5, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang

    Re: Compilation error reason???

    Weng Tianxiang, Jul 23, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,572
    Mike Treseler
    Jul 24, 2003
  2. Blake Versiga
    Replies:
    2
    Views:
    19,766
    Yan-Hong Huang[MSFT]
    Jul 9, 2003
  3. rl30
    Replies:
    1
    Views:
    622
    emachine
    Aug 15, 2003
  4. Frederik
    Replies:
    0
    Views:
    1,415
    Frederik
    Nov 25, 2003
  5. Frederik
    Replies:
    2
    Views:
    5,499
Loading...

Share This Page