Thread: fine! tried but facing a problem

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    fine! tried but facing a problem

    ok guys!
    my intention was not that.it was not a homework.
    anyways
    i have done them
    but there is a problem
    the following code goes to a infinite loop
    i am not getiing it

    main()
    {unsigned int i=10;
    while(i>=0){
    printf("%u",i)
    i--;}
    }

    also this one

    for (i=0;i=10;i+=2)
    printf("Hi\\n");

    difference b/w \n and \\n?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, now I know you're just not paying attention. I've already given you the Big Red Square for another post where you didn't use code tags. Unfortunately, I have to spread it around more before I can give you another one. So I'll just chew you out again for it. USE CODE TAGS!

    difference b/w \n and \\n?
    How about you spend ten seconds and find out for yourself?
    Code:
    #include <stdio.h>
    int main ( void )
    {
        printf("this is with two slashes: \'\\n\', and this is with one slash: \'\n\'.");
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    First off, edit your post and put [ CODE ] [ /CODE ] tags in. Do it now.

    Second, I'll offer some help. In the first code snippet
    Code:
    int main() 
    {
        unsigned int i=10;  
        while(i>=0)
        { 
            printf("%u",i) 
            i--;
        } 
        return 0;
    }
    //Edited for clarity
    Your missing something in the while loop. If you throw this in a compiler, it won't compile.

    In the second code snippet, you are suffering from a very very common typo.

    Code:
    for (i=0;i=10;i+=2) 
    printf("Hi\\n");
    Look closely at the condition for the for loop...

    And just for the record, a very safe coding practice that you should get in the habit of doing is to use the following in your conditionals...

    Code:
    int main()
    {
        int i = 10;
        if (10 == i)
        {
             //this is the safe way. If you forget a =, the compiler will tell you
        }
        while (i == 10)
        {
            //If you forget an =, then i will be set to 10, and since all non 0 values are TRUE, the loop goes on forever!
        }
        return 0;
    }
    PK

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    A unsigned integer can be lower than zero...

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by xErath
    A unsigned integer can be lower than zero...
    It never hurts to check...what if the type gets changed to an int later for some reason? Makes it less likely a bug appears when changes are made and clearly states the condition being checked, even if it is redundant.

    Plus, a good compiler should recongnize that fact and not put in the extra check...I hope.

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by pablo615
    It never hurts to check...what if the type gets changed to an int later for some reason? Makes it less likely a bug appears when changes are made and clearly states the condition being checked, even if it is redundant.

    Plus, a good compiler should recongnize that fact and not put in the extra check...I hope.
    What happens when i is 0? it satisfies i >=0, so the loop exectues again.... now what happens to i? does the loop keep on going? think about it
    hello, internet!

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Oh, thats what you meant...uhhh, I missed that. It will roll over and become all 1s (0xFFFFFFFF for 32 bit int) and the loop will just keep on going.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Which should have been obvious since he is printing the result. It would look something like

    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    <some really big number> -> max value for unsigned integer
    <that really big number-1>
    etc
    etc

    Unsigned integers are just that. They have no sign. The sign bit is in the higest order bit but it is used as a numerical value instead of a sign. The max value for a 16-bit unsigned integer is 65535 or 0xFFFF. For a 32-bit value it is 0xFFFFFFFF or something like 4,200,.... some really big number that I forget what it is. Your MSVC will tell you the upper limit of a 32-bit unsigned integer. So your loop will never terminate...it will always be greater than or equal to 0 since your variable can never be negative.

    And please don't do this:
    Code:
    int main()
    {
    	int i = 10;
    	if (10 == i)
    	{
    		 //this is the safe way. If you forget a =, the compiler will tell you
    	}
    	while (i == 10)
    	{
    		//If you forget an =, then i will be set to 10, 
    		//and since all non 0 values are TRUE, the loop goes on forever!
    	}
    	return 0;
    }
    If you can't remember an equals sign then you prob ought to go to the doctor and get your head checked. Better than that if your compiler flags the line as an error and you cannot see that you forgot an equals sign...definitely admit yourself to the emergency room. You've had a massive brain failure.

    I give up.
    Last edited by Salem; 10-07-2004 at 01:14 AM. Reason: comment folded to reduce page width

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    did this not give you an overflow warning?

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't think it would flag a warning. It's not a signed to unsigned comparison so that wouldn't flag it. Zero is unsigned (not actually, but you get the idea) and his data type is unsigned so I don't think that would cause a warning.

    Might be one of those errors that the compiler will not catch because after all, it is syntactically correct and legal. It's just using flawed logic and unfortunately there are some logic errors that compilers just won't catch.

    Technically this is an underflow. But again I don't think it will flag any warning because the variable will never lose any significant digits, it's just wrapping the data type. It might say something about possible underflow, but again it might not.

    And it's really not an underflow because the data type can represent everything he has specified in the code. An unsigned int can be 0 and can be <=10 and it also can be 0xFFFF.

    A good illustration about why it is so important to use the correct data type. Imagine if this error were part of a rendering loop or calculation loop in a huge program. Very hard to catch this error, especially if there was more code in the program.
    Last edited by VirtualAce; 10-07-2004 at 07:40 AM.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by misplaced
    did this not give you an overflow warning?
    C or C++ programs won't give runtime "overflow" warnings or errors for integer addition, subtraction or multiplication. Bits more significant than the size of the item are simply lost.

    Some compilers (Borland bcc32, for example) might give a compile-time warning like:
    Warning W8008 d.c 3: Condition is always true in function main
    That would be nice

    lint gives the following:

    d.c:3:7: Comparison of unsigned value involving zero: i >= 0
    An unsigned value is used in a comparison with zero in a way that is either a
    bug or confusing. (Use -unsignedcompare to inhibit warning)
    These are good tools, but the best tool is your brain: how could the loop go infinite when the loop condition is

    "i >= 0"

    Three possibilities that I can think of (there may be more, but I think you get the idea):

    1. The computer is defective; can't execute code properly.

    2. The compiler is defective; can't create a valid executable from valid source code.

    3. The variable "i" is always greater than equal to 0.

    Let's see, now, which do I think is more likely?

    Not 1 (everything else works OK)
    Not 2 ("Hello, world", compiles and executes OK)
    Not 3 since I perform "i--" every time through the loop, and "i" is clearly declared to be an unsigned int.

    Wait! Hold everything! The value of an unsigned int is always greater than or equal to 0. Oh, man, what a relief! I don't have to throw away my computer or get a new C compiler, just fix the code.

    Regards,

    Dave
    Last edited by Dave Evans; 10-07-2004 at 10:08 AM.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by Bubba
    And please don't do this:
    Code:
    int main()
    {
    	int i = 10;
    	if (10 == i)
    	{
    		 //this is the safe way. If you forget a =, the compiler will tell you
    	}
    	while (i == 10)
    	{
    		//If you forget an =, then i will be set to 10, 
    		//and since all non 0 values are TRUE, the loop goes on forever!
    	}
    	return 0;
    }
    If you can't remember an equals sign then you prob ought to go to the doctor and get your head checked. Better than that if your compiler flags the line as an error and you cannot see that you forgot an equals sign...definitely admit yourself to the emergency room. You've had a massive brain failure.

    I give up.
    Now thats just plain wrong. Typos happen all the time, even to the best of us. And its a human flaw that when your reading through something and expecting to see something, like 2 = signs, you will see 2, even if there is only one. Sure, when we read other peoples code, we see all the flaws because we don't know what to expect, so we actually read it.

    This coding practice can save you a headache, especially for beginners. The compiler won't flag a valid assignment in a conditional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc compiling and linking problem
    By maven in forum C Programming
    Replies: 6
    Last Post: 11-14-2008, 05:28 AM
  2. facing problem in cmopiling the code
    By ishwarverma in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 09:22 PM
  3. Problem using " and '
    By zombiezparadize in forum Tech Board
    Replies: 10
    Last Post: 12-18-2007, 04:23 PM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Random things are fine, just one problem with them.
    By DarkSFK in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2002, 08:40 AM