Fixing Error "formatspecifier" in Visual Studio 2008

  • Thread starter Thread starter Luongo
  • Start date Start date
  • Tags Tags
    Visual
Click For Summary

Discussion Overview

The discussion revolves around an error encountered in Visual Studio 2008 related to the use of format specifiers in a C/C++ program. Participants explore potential causes of the error and the correct usage of input/output functions and format specifiers in C and C++.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet that produces an error message related to format specifiers, questioning the cause.
  • Another participant suggests checking the error messages for specific line indications.
  • Some participants discuss the validity of using C-style input/output in C++ and the necessity of including the correct headers, such as stdio.h or cstdio.
  • There is a suggestion that the format specifier in the printf function should be changed from %f to %lf for double precision.
  • Participants clarify that using %f in printf is valid, while %lf is necessary in scanf for double variables.
  • One participant humorously admits to being mistaken about format specifier usage for many years.

Areas of Agreement / Disagreement

Participants generally agree that the inclusion of the correct headers is necessary and that the format specifiers have specific requirements in different contexts. However, there is some disagreement regarding the necessity of changing the format specifier from %f to %lf in the printf statement, as some assert that %f is acceptable.

Contextual Notes

There are unresolved questions regarding the specific error message and the implications of using different format specifiers in various contexts. The discussion does not reach a consensus on whether the original code is fundamentally correct or if it requires changes.

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/count? (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/count?

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/count.
 
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 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
14
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K