Thread: float gives weird output

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    20

    float gives weird output

    I've just begun programming C, but I'm familiar with the syntax and such.

    I've made a list where fahrenheit is compared to celsius, and it works fine when I use decimal integers. But when I use floating point then the output goes weird.

    the code (with float):

    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
    	int fahr, celsius;
    	int lower, upper, step;
    
    	lower = 0;
    	upper = 300;
    	step = 20;
    
    	fahr = lower;
    	
    	while (fahr <= upper)
    
    	{
    		celsius = (5.0/9.0) * (fahr-32.0);
    		printf("%3.0f %6.1f\n", fahr, celsius);
    		fahr = fahr + step;
    	}
    }
    the weird output of 1 line of the above:

    Code:
     -1 8064894583287849000lotsof0000 finally ending with 00.0
    plz help

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That's because you're outputting ints as floats. Change your declarations so that the variables are floats.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Hmm,

    Try changing:

    int fahr, celsius;

    To:

    float fahr, celsius;

    Since you are displaying floats in your printf() using %f.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    20
    omg duh, but thx.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    22
    Your type declarations were wrong.

    Code:
    #include<conio.h>
    int main()
    
    {
    
    float fahr, celsius;
    int lower, upper, step;
    clrscr();
    lower = 0;
    upper = 300;
    step = 20;
    
    fahr = lower;
    
    while (fahr <= upper)
    	{
    		celsius = (5.0/9.0) * (fahr-32.0);
    		printf("%3.0f \t %6.1f\n", fahr, celsius);
    		fahr = fahr + step;
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  3. Backdooring Instantaneous Radius of Curvature & Functions
    By just2peachy in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2004, 12:25 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM