4 Replies - 477 Views - Last Post: 07 April 2013 - 09:59 AM Rate Topic: -----

#1 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

conflicting types for 'round', should be an easy fix...

Posted 06 April 2013 - 06:39 PM

This is the syntax for the round() function in C. round
Syntax:
#include <math.h>
double round(double x);

Somehow I don't understand it at all? I'm getting "conflicting types for 'round'" error when running this simple program i made just to see if i undertsand the round function.

 

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    float a = 12.33;
    float round(float a);
    printf("%f", a);
    return 0;
}



I just want to print the variable a after it is rounded! Or does rounding not actually store the new value in the variable? I am a C noob.

Is This A Good Question/Topic? 0
  • +

Replies To: conflicting types for 'round', should be an easy fix...

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: conflicting types for 'round', should be an easy fix...

Posted 06 April 2013 - 06:49 PM

The following:
    float round(float a);

Is declaring a function named round that takes one float argument and returns a float. The problem is that there is a function defined in math.h that has this signature: double round(double); which is what you should be using, not declaring your own function with this name. For information on how you properly call a function I suggest you read the function tutorials contained in my signature.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Re: conflicting types for 'round', should be an easy fix...

Posted 06 April 2013 - 07:37 PM

I've read that article multiple times now. This is what I originally tried
 
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    double a = 12.33;
    round(a);
    printf("%f", a);
    return 0;
}



Is that the right way to call the function? I still just prints 12.33000, not rounded...
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: conflicting types for 'round', should be an easy fix...

Posted 06 April 2013 - 10:51 PM

No its not

The compiler should have told you the statement has no effect at line 8

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    double a = 12.33;
    a = round(a);
    printf("%f", a);
    return 0;
}





see the difference here I reassign a to a new 'rounded value'

Snoopy.
Was This Post Helpful? 2
  • +
  • -

#5 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Re: conflicting types for 'round', should be an easy fix...

Posted 07 April 2013 - 09:59 AM

That makes sense. Thanks guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1