#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Red face Range of a data type!!


    Hi All,

    Tried to run the following program:

    void main() {
    int i;
    for(i=-1;i!=0;i--)
    printf ("%d\n",i);
    }

    It ran continously but than stopped at 1 and never terminates

    Q1 : Does it mean it will go upto the integer data type rang and than stopped why??

    Q2: Why it never terminates?

    Please help me to understand it.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Well (assuming numeric underflow is not an exception) I would expect it to terminate when it reaches 1 (indeed it does when tested).

    Try it with 'signed char' as the data type, if you're bored of waiting for 4 billion numbers to be printed.

    Also, fix the void main thing by using int main and a return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    "stopping" and "terminating" might be interpreted as the same thing! Explain what you mean exactly by "stopped".

    Did it continuously print "1" repeatedly or just stop printing?

    Were you running in a debugger with a break point set on the end of main perhaps?

    The code will do what you expect all other things being equal, so there is something you are not telling us in this report.

IMN logo majestic logo threadwatch logo seochat tools logo