Thread: Explain what can go wrong with the following C code

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    11

    Question Explain what can go wrong with the following C code

    Please help me to explain what can go wrong with the following C code, why it looping forever? I don't understand.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    int main()
    {
    	int x; 
    	float y; 
    	x = 1; 
    	while (x > 0) 
    	{ 
    		y = x+1; 
    		x = y; 
    	}
    	return 0;
    }
    Regards.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Quote Originally Posted by Elysia View Post
    Oh, Eysia. I'm a newbie, so I have learnt C for 2 week in this summer-semester. I have read your recommended document so I don't understand clearly. Can you detail my problem, many thanks to you!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In short: there isn't enough storage in a float to exactly store all kinds of numbers. Remember that a float has to store decimals as well as the integer part.
    Eventually, you will stumble upon a number that cannot be exactly represented in a float. It happens that two specific numbers are stored in the same way because it cannot be represented exactly (again, lack of enough storage).
    At some point, x and x + 1 are the same number in a float. Then you hit your ceiling and cannot proceed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User sfryett's Avatar
    Join Date
    Jul 2010
    Location
    London
    Posts
    4
    Code:
    	while (x > 0)
    x is always > 0?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will eventually overflow, resulting in a negative number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by luckyluke View Post
    Please help me to explain what can go wrong with the following C code, why it looping forever? I don't understand.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    int main()
    {
    	int x; 
    	float y; 
    	x = 1; 
    	while (x > 0) 
    	{ 
    		y = x+1; 
    		x = y; 
    	}
    	return 0;
    }
    Regards.
    The while loop will never break out, because you set x equal to 1, then you increase x by 1, in every iteration of the loop (using y as an intermediary).



    So how is x ever going to be anything else but greater than zero?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, it's also a question if a float can overflow?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Quote Originally Posted by Elysia View Post
    In short: there isn't enough storage in a float to exactly store all kinds of numbers. Remember that a float has to store decimals as well as the integer part.
    Eventually, you will stumble upon a number that cannot be exactly represented in a float. It happens that two specific numbers are stored in the same way because it cannot be represented exactly (again, lack of enough storage).
    At some point, x and x + 1 are the same number in a float. Then you hit your ceiling and cannot proceed.
    I have just understood, many thanks to you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Could someone explain what I did wrong with this?
    By Paroxysm in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2008, 03:08 PM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. whats wrong - long code
    By raja9911 in forum C Programming
    Replies: 0
    Last Post: 02-07-2006, 06:10 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM