What type does the sscanf Modifier

2019-02-27 19:16发布

问题:

I have come across some legacy code that has the following type of line:

sscanf(szBuff,"%Fd %Ff %Fd %Ff"

Has anyone seeen a modifier like Fd or Ff? If so, what does it mean?

I cant seem to find any information on it. The code compiles and runs fine.

回答1:

As ouah pointed out, these are the same as their lower case counterparts. Why is that? For symmetry with the printf conversion specifiers. Here %x and %X write lowercase or uppercase numbers like deadbeef and DEADBEEF. The symmetry allows to use the same format string for both input with scanf and output with printf.

#define FMT "%F\n"

sscanf (str, FMT, &value);
printf (FMT, value);


回答2:

C says for fscanf functions:

(C991, 7.19.6.2p14) The conversion specifiers A, E, F, G, and X are also valid and behave the same as, respectively, a, e, f, g, and x.

So in %Fd, the conversion specification is %F which is equivalent to %f. Note that the d is not part of the conversion specification.

For example (for fprintf functions %F is also the same as %f):

printf("%fd\n", 3.141592);

will print:

3.141592d


1. C89/C90 does not recognize the F conversion specifier. For example, for fscanf the corresponding C90 paragraph in 7.9.6.2 says: The conversion specifiers E, G, and X are also valid and behave the same as, respectively e, g, and x



回答3:

%F is a POSIX (and C99) extension.

http://pubs.opengroup.org/onlinepubs/007904975/functions/scanf.html

"The conversion specifiers A, E, F, G, and X are also valid and shall be equivalent to a, e, f, g, and x, respectively."