Thread: c/c++ prpgramming

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    17

    Lightbulb c/c++ prpgramming

    hello everybody,
    here i've one problem .can u guys please tell me what is output and *why?
    main()
    {
    unsigned int i=10;
    while(i>=0)
    {
    printf("%u\n",i);
    i--;
    }
    }

    thank u
    srinu
    with regards
    srinu

  2. #2
    Unregistered
    Guest
    unsigned int always >= 0
    So you get infinite loop.

    change to signed int or modify your while ( condition )

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I don't know how 0 is dealt with with respect to being signed or unsigned, but I would expect it to be a valid value for an unsigned int. IF it isn't, then you will get a never ending loop.

    Otherwise the loop counts down from 10 to 0 inclusive by one stopping when i = -1, placing each new numeral on a new line using C style output.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    4294967295
    4294967294
    4294967293
    4294967292
    As already said I believe, unsigned int i is always greater than or equal to 0, hence the infinite loop. The above is the first few lines of output.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    dope slap to forehead.

Popular pages Recent additions subscribe to a feed