Thread: Function with no argument but return val - throwing up erroneous results

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Function with no argument but return val - throwing up erroneous results

    I'm trying to write a function to calculate diameter, circumference and area of circle whereby the function has no argument but has return value. The diameter is coming up fine but circumference and circle area results are way off and the results are also equal to each other! Is there something wrong with my variable declaration or elsewhere? Thanks!

    ps: also getting the exactly same wrong results when I'm trying to tackle the same problem through a function with no return val but with an argument.

    Code:
    #include<stdio.h>
    
    
    int radius();
    
    
    int main()
    
    
    {
        int num;
        
        num = radius();
        
        printf("Diameter is %d\n", num * 2); 
        
        printf("Circumference is %d\n", 2 * 3.14 * num); 
        
        printf("Area is %d\n", 3.14 * num * num); 
    }
    
    
    int radius()
    
    
    {    int x; 
        
        printf("Enter the radius: ");
        
        scanf("%d", &x);
        
        return x; 
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    $ gcc main.c -Wall -Werror
    main.c: In function ‘main’:
    main.c:17:5: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format]
    main.c:19:5: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format]
    main.c:20:1: error: control reaches end of non-void function [-Werror=return-type]
    Perhaps try working with doubles instead of ints.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Hello the problem is that when you multiply 3.14 (PI) then the number you obtain is a float and you are trying to print a float as a regular integer and that's why it's failing.

    Code:
    printf("Circumference is %d\n", 2 * 3.14 * num);
    printf("Area is %d\n", 3.14 * num * num);
    change them to handle floats

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Thank you, its working but the result is showing up in scientific notation after I've rounded down the float to 2 decimal places using printf("...%.2E"), etc.

    Circumference is: 2.512000E+001
    Area is: 5.024000E+001

    So now my question is how do I convert the scientific notation back to floating point notation? Many thanks,

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just use %f instead of %e.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Many thanks for all your helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  2. How to properly return MySQL results to a calling function?
    By Effenberg0x0 in forum C Programming
    Replies: 3
    Last Post: 06-09-2011, 03:42 AM
  3. Replies: 12
    Last Post: 05-24-2011, 05:57 AM
  4. strcspn return incorrect results
    By qualia in forum C Programming
    Replies: 5
    Last Post: 10-08-2010, 08:40 AM
  5. Problem with Pointer as return Argument
    By gregorsart in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:35 AM