Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to output a float with

printf(theFloat);

However, this gives me the following error.

"argument of type "float" is incompatible with parameter of type "const char *""

I'm not sure why this isn't working, I have had a look and found people using printf to format floats... Is there another print method for floats etc?

share|improve this question

closed as too localized by H2CO3, John Dibling, juanchopanza, syam, Blastfurnace Jun 6 '13 at 21:04

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Have a look at a reference –  juanchopanza Jun 6 '13 at 19:17
    
Just because you want to "using printf to output floats" does not mean that you can simply cram a float into a printf like that and expect it to somehow work. What documents did you read on printf? What examples did you look through? I'd say that a single example is typically enough to figure out how to use printf for a simple task like that. –  AnT Jun 6 '13 at 19:19

3 Answers 3

up vote 2 down vote accepted

You are using wrong printf(char* format,...);

printf("%f",theFloat);
share|improve this answer

You should read the documentation of printf. The following prints a single float:

  printf ("%f", theFloat);

The first parameter should be a formatting string, which is const char * (This is why you got that compile error);

In C++, you can use:

std::cout << theFloat <<std::endl;

If you want to output the float number with fixed precision, take a look at setprecision and fixed.

share|improve this answer
1  
+1 for the first line –  user529758 Jun 6 '13 at 19:19

It should be

printf("%f",theFloat);

You can also add some options for the number of digits to print out on each side of the decimal point. Take a look here

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.