Short Int Related

This is a discussion on Short Int Related within the C Programming forums, part of the General Programming Boards category; Hi, Recently I was executing a code in C. The program is: Code: #include<stdio.h> int main() { short int i ...

  1. #1
    NKP
    NKP is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    8

    Short Int Related

    Hi,
    Recently I was executing a code in C. The program is:

    Code:
    #include<stdio.h>
    int main()
    {
    short int i =0;
    for(i<=5 && i>=-1;++i;i>0)
    printf("%u\n",i);
    printf("\n");
    return 0;
    }
    And the output was nos. 1 to 65536.
    (Given that short int is 2 bytes in length).

    Can any one explain the logic behind such output.I am unable to analyse.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Whoever wrote that for loop is either trying to submit something to The International Obfuscated C Code Contest, or he doesn't know how for loops work.

    The first and last parts of the for statement do absolutely nothing relevant, so that leaves us with:
    Code:
    ++i
    This will end the loop as soon as i becomes negative, i.e. when the short int wraps around.

  3. #3
    NKP
    NKP is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    8
    Hi Memloop,
    Can you explain how is this:-

    "This will end the loop as soon as i becomes negative, i.e. when the short int wraps around."
    as you have mentioned in your post.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    for(INITIALIZATION EXPRESSION; LOOP CONDITION; INCREMENT EXPRESSION)
    The loop you've posted looks like this (the other statements don't actually do anything):
    Code:
    for(; ++i;)
    As you can see, the only active part of the for loop is the loop condition. In C, any negative integer value evaluates to boolean false, so the loop will terminate when ++i becomes negative. That happens when i wraps around, i.e. it goes something likes this depending on how large it is:
    Code:
    1, ..., SHORT_MAX, SHORT_MIN, SHORT_MIN + 1, ...
    Last edited by Memloop; 01-24-2010 at 08:50 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Memloop View Post
    In C, any negative integer value evaluates to boolean false, so the loop will terminate when ++i becomes negative.
    This is INCORRECT. The only false value in C is 0. The loop terminates and ends when ++i would equal 0, not a negative.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by NKP
    Can any one explain the logic behind such output.I am unable to analyse.
    Despite a blooper that MK27 has explained, Memloop's analysis is generally correct. That said, I believe that your code snippet actually results in undefined behaviour as it results in signed integer overflow to result in 0 and thus terminate the loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Replies: 2
    Last Post: 03-24-2006, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21