Fixing Error "formatspecifier" in Visual Studio 2008

  • Thread starter Thread starter Luongo
  • Start date Start date
  • Tags Tags
    Visual
AI Thread Summary
The error "formatspecifier" in Visual Studio 2008 is likely due to the absence of the necessary header file, specifically "stdio.h" or "cstdio," which is required for using printf and scanf functions. Additionally, the format specifier in the printf statement should remain as %f, while scanf should use %lf for reading double values. The discussion highlights that using C-style input/output in C++ is acceptable as long as the correct headers are included. It is emphasized that understanding the differences between format specifiers in printf and scanf is crucial for proper functionality. Overall, including the right headers and using the correct format specifiers will resolve the compilation issues.
Luongo
Messages
119
Reaction score
0
Hi,

My visual studioes is acting very weird the following code:

#include <math.h>
#define GRAVITY 9.81

int main(void)
{
double range;
double angle;
double initspeed;

printf("enter speed\n");
scanf("%lf", &initspeed);

printf("enter angle\n");
scanf("%lf", &angle);

range=(initspeed*initspeed)/GRAVITY*sin(2*angle);
printf("The range is: %f m/s^2\n", range);

system( "PAUSE" );
return 0;
}

gives an error: 1>formatspecifier - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm not sure why? My code seems fine does anyone know what the problem may be? I think it's a compiler error.
 
Physics news on Phys.org
scroll up the error messages and it will tell you which line it doesn't like
 
This might be dumb, but can you use C-style input/output in C++ instead of cin/cout? (Presumably, you're using Visual C++ or Visual C++ Express)

Ah, I think that might be the problem... You probably need to include the cstdio library to access those sorts of instructions (see the C Library section of the link below):
http://www.cplusplus.com/reference/

But yeah, as mgb_phys says, the compiler should tell you which lines your errors are in.
 
MATLABdude said:
This might be dumb, but can you use C-style input/output in C++ instead of cin/cout?

Any reason why not to?
 
Borek said:
Any reason why not to?

Not as long as the right header (stdio.h or cstdio.h) is included! I suppose that you'd also have to include iostream.h to make use of cin/cout.
 
There's nothing wrong with the program.
It's complaining because you don't have "stdio.h" included so it doesn't know about printf/scanf
 
mgb_phys said:
There's nothing wrong with the program.

I expect unexpected output.
 
I concur with mgb_phys that the code needs to #include <stdio.h> and I believe Borek is correct that it (might) produce some unexpected output.

This %f format specifier in this line should probably be changed to %lf.
Code:
printf("The range is: %f m/s^2\n", range);

I would advise reading up on the proper use of the %f and %lf format specifiers used in both scanf() and printf(). They are a little tricky and somewhat nonintuitive.
 
Using %f in printf is perfectly valid, the printf call doesn't need to know how big the float is it can handle that when generating the output.

But in scanf you do need the "lf" so the call knows how big the destination variable is to copy the result into, you can't necessarily copy a float into a double or float at the same address correctly.
 
  • #10
mgb_phys said:
Using %f in printf is perfectly valid, the printf call doesn't need to know how big the float is it can handle that when generating the output.

Surprise, I was wrong for 25 years :bugeye:
 

Similar threads

Replies
3
Views
1K
Replies
1
Views
10K
Replies
12
Views
2K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
4
Views
2K
Back
Top