Thread: [newbie question] C setup

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

    [newbie question] C setup

    Hello everyone, I am a intermediate programmer that had done some programming with PHP, ASP, SQL, Fortran, Matlab and VB and now wanting to learn C.

    however I am coming through some very awkward problem, the logic is defeating itself.

    Code:
    #include <stdio.h>
    
    main()
    {	double x;
    	double y;
    	printf("hello\n");
    
    	y = 2.0;
    	
    	while (y < 100.0)
    	{
    	y = y + 0.5;
    	printf("y = %f\n" y);
    
    /* output statement */
    		if (y < 100.0)
    		{
    			printf("y is less than 100.0 because y is %f\n", y);
    		}
    		else
    		{
    			printf("y is greater than 100.0 because y is %f\n", y);
    		}
    	}
    }
    output:
    y is less than 100.0 because y is 3.000000
    y is less than 100.0 because y is 3.500000
    y is less than 100.0 because y is 4.000000
    y is less than 100.0 because y is 4.500000
    y is greater than 100.0 because y is 5.000000
    y is greater than 100.0 because y is 5.500000
    y is greater than 100.0 because y is 6.000000
    y is greater than 100.0 because y is 6.500000
    y is greater than 100.0 because y is 7.000000
    y is greater than 100.0 because y is 7.500000
    y is greater than 100.0 because y is 8.000000
    y is greater than 100.0 because y is 8.500000
    y is greater than 100.0 because y is 9.000000

    Strange thing is, the output statement is outputing the else after y becomes greater than 5.0.

    its as simple as, I went into a loop with a specific condition (y < 100), but within the loop, when I use if (y<100), some times the result is true, sometimes its false!!!

    I am not sure if it is a compiler problem, a windows problem, a type casting problem or anything, I am scratching my head all over it.

    Below is the system and C compiler configuration:
    Windows Vista Business SP2 32bit
    CPU: Intel Q9500
    C compiler: Tiny C compiler 0.9.25 (TCC)

    would be grateful if anyone can give any suggestion.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It appears the floating point package didn't get linked in. I'm not familiar with Tiny C, but I know Borland Turbo C has this problem, and a work around to force it to link in the floating point package.

    I found my fix by Googling, but also check your doc's and options that you have set up, currently.

    For some goofy reason, the linker may work better if you declare:
    double y=2.0L; //or l "el", lowercase

    or

    double y=2.0;

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    15
    You sure ur posting the code that prints this?
    Code:
    printf("y = %f\n" y);
    This line won't even compile.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Thanks for Adak's and Vincent's reply, I was afraid the packaging was the problem.

    For Vincent's info, the code I pasted was a chopped down version of my original program, some parts I chopped off and added on, the comer was just a typo, but the programming logic was as it states.

    I was never very good at establishing a workstation for a language (had trouble installing a fortran compiler on vista, so switched to C), I too wanted to include some math package into the compiler however I cannot find libm.a in the lib directory of TCC.

    I suppose with a very chopped down version of C, it is not surprising that many of the functions are missing, I just didn't thought that even floating are missing. I tried to use y = sin(x) and printf y, it just gave a non-sense number, I really have not too much idea how a C compiler functions and thought if the sin function was not included, the compiling should generate an error, however it just returned a non-sense value (y = 107859394.0000, x = 2.0000 dah?).

    Anyway, would like to hear some suggestions for a sort of "normal" C compiler on Vista. Not a very chopped down version, where I will get normal numerical functions, array functions when I need it, and can do some scientific programming. Any suggestions?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Gcc and PellesC seem to be the most popular here, along with VisualStudio C++ (which will do C also)... My choice is PellesC (others will disagree)...

    smorgasbordet - Pelles C

    Full blown C-99 with windows development included. Very nice IDE... free...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    15
    Quote Originally Posted by jimmychauck View Post
    I suppose with a very chopped down version of C, it is not surprising that many of the functions are missing, I just didn't thought that even floating are missing. I tried to use y = sin(x) and printf y, it just gave a non-sense number, I really have not too much idea how a C compiler functions and thought if the sin function was not included, the compiling should generate an error, however it just returned a non-sense value (y = 107859394.0000, x = 2.0000 dah?).
    I've been using tcc on Windows xp for small programs and it's working fine in the sine case. And in its doc it claims float is being supported. Not sure about Vista though. Anyways it's not so hard to switch to a more professional look compiler/IDE on Windows. You might also want to take a look at Code::Blocks.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Sorry for the confusion caused, now I found what went wrong, when I do #include <math.h>, everything was solved. because I didn't see it in the lib directory I thought it didn't exist, but later I found it in the include directory.

    Now I am just curious that why wouldn't it generate an error when I used sin(x) but math.h was not included.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimmychauck View Post
    Sorry for the confusion caused, now I found what went wrong, when I do #include <math.h>, everything was solved. because I didn't see it in the lib directory I thought it didn't exist, but later I found it in the include directory.

    Now I am just curious that why wouldn't it generate an error when I used sin(x) but math.h was not included.
    Check the "warning level" on your compiler settings... make sure it's on maximum.

    A compiler would have to be pretty brain-dead to not warn you of a missing library function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setup project in msvc2005
    By sgh in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-30-2008, 08:22 AM
  2. Problem about making setup project
    By sgh in forum Windows Programming
    Replies: 0
    Last Post: 04-30-2008, 03:09 AM
  3. IE Page Setup
    By afreedboy in forum Tech Board
    Replies: 5
    Last Post: 05-07-2005, 04:35 AM
  4. Setup
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-10-2003, 09:22 AM
  5. linking?! [newbie question]
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-14-2002, 02:01 AM