问题标签 [format-specifiers]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
66 浏览

python - 为什么我的 %s 格式说明符没有出现在这个片段中?

我已经多次编写并重写了此代码段,但对于我为什么要打印“%s”而不是用我为它指定的数据替换它是没有意义的。我找不到任何表明它为什么不起作用的东西。

0 投票
4 回答
72501 浏览

c++ - size_t 的正确 printf 格式说明符:%zu 还是 %Iu?

我想使用 Microsoft Visual Studio 2010 在 C++ 中打印出一个size_t变量的值printf(我想在这段特定的代码中使用printf而不是,所以请不要告诉我我应该使用的答案)。<<<<

根据帖子

c 中与平台无关的 size_t 格式说明符?

正确的独立于平台的方法是使用%zu,但这似乎在 Visual Studio 中不起作用。Visual Studio 文档位于

http://msdn.microsoft.com/en-us/library/vstudio/tcxf1dw6.aspx

告诉我必须使用%Iu(使用大写i,而不是小写l)。

微软没有遵循这里的标准吗?还是自 C99 以来标准已更改?还是 C 和 C++ 之间的标准不同(这对我来说似乎很奇怪)?

0 投票
3 回答
27594 浏览

c++ - Why the use of cin, cout or %I64d is preferred over %lld specifier in C++?

Why many of the Online Judges advises "do not use the %lld specifier to read or write 64-bit integers in С++"

It is preferred to use the cin, cout streams or the %I64d specifier?

0 投票
2 回答
118 浏览

c - 为什么 ] 在 C 中不显示?

这是我在 C 中的代码片段:

我希望这应该显示[2]。这len是3。

但运行此代码仅显示[2

为什么会这样?

0 投票
3 回答
1611 浏览

c++ - 模板类型的格式说明符

我有一个以整数类型为模板的 C++ 类,例如,

假设在那个类的某个地方,我想用来sscanf从文件中读取一些值,例如,

格式说明符仅在int_type是内在的 时才能正常工作int

有没有更好的方法来处理 general 的格式说明符int_type

0 投票
2 回答
17438 浏览

c - Doesn't %[] or %[^] specifier in scanf(),sscanf() or fscanf() store the input in null-terminated character array?

Here's what the Beez C guide (LINK) tells about the %[] format specifier:

It allows you to specify a set of characters to be stored away (likely in an array of chars). Conversion stops when a character that is not in the set is matched.

I would appreciate if you can clarify some basic questions that arise from this premise:

1) Are the input fetched by those two format specifiers stored in the arguments(of type char*) as a character array or a character array with a \0 terminating character (string)? If not a string, how to make it store as a string , in cases like the program below where we want to fetch a sequence of characters as a string and stop when a particular character (in the negated character set) is encountered?

2) My program seems to suggest that processing stops for the %[^|] specifier when the negated character | is encountered.But when it starts again for the next format specifier,does it start from the negated character where it had stopped earlier?In my program I intend to ignore the | hence I used %*c.But I tested and found that if I use %c and an additional argument of type char,then the character | is indeed stored in that argument.

3) And lastly but crucially for me,what is the difference between passing a character array for a %s format specifier in printf() and a string(NULL terminated character array)?In my other program titled character array vs string,I've passed a character array(not NULL terminated) for a %s format specifier in printf() and it gets printed just as a string would.What is the difference?

//Program to illustrate %[^] specifier

//character array vs string

Output JON

//Using %c instead of %*c

Output fruit,apple,lemon,and the character is |

0 投票
3 回答
714 浏览

c# - 是否有一个 C# 格式说明符会省略前导零,除非值为零?

换句话说,我希望 0.123 显示为“.123”,但 0 应该显示为“0”。目前我所拥有的最好的是

它给出了 0.123 的“.123”,但 0 的“”(空字符串)。

0 投票
2 回答
1530 浏览

java - 当没有足够的参数时克服 String.format 的 MissingFormatArgumentException 的方法?

在 Java 上,我知道接下来没问题:

(打印“aaa”)

但是,我希望能够处理相反的事情,例如:

(这会产生一个异常 java.util.MissingFormatArgumentException )

我希望使其尽可能通用,无论有多少说明符/参数,如果没有足够的值,只需忽略它们(从有问题的位置跳过所有说明符)并编写字符串的其余部分(例如,在我展示的情况下,它将写为 "aaabbb" ) 。

是否有可能开箱即用,还是我应该编写一个函数来做到这一点?

0 投票
3 回答
155 浏览

c - For "char list[3][10];" why does all of these work as scanf() %s arguments---&list[i],list[i],&list[i][0]?

Isn't char* the only valid argument type for the %s format specifier used in the format specifier string of scanf()?If so,in my program why each one of these work exactly the same for both scanf()'s and printf()'s %s format specifier:

I'll appreciate if you clear the following confusions that arise from this premise:

1) Why is &name[i] working given it is of type char (*)[].Isn't &name[i][0] is the only valid argument as it is of type char* ?

2) Does name[i] decompose/translate into char* when we pass it as an argument for %s?Is it same as passing &name[i][0],which is the address of the first character of each name we enter?Is it why it works?

0 投票
2 回答
21925 浏览

c - Using fscanf() to read a file with lines of 3 numbers each,why does "%d%d%d%*c" act as good as "%d%d%d"?

I know that the %d format specifier,when used here in fscanf(), reads an integer and ignores the white-space preceding it,including the newline(I verified it).But in my following program that uses fscanf() to read from a file of multiple lines with 3 integers each,the format string "%d%d%d%*c" works as good as "%d%d%d".

Why is it so?Since fscanf() used with %d as the first format specifier in the format specifier string ignores any whitespace preceding an integer, why doesn't the extra %*c used as last specifier cause any error or side-effect?Had the %d specifier not been ignoring the newline after each group of 3 numbers in a line,then %*c would have make sense as it would eat away the newline.But why it works without error or side-effect even if fscanf() ignores whitespace for %d by default? Shouldn't fscanf() stop scanning when %*c can't find a character to eat and there is a mismatch between the specifier and the input? Isn't fscanf() supposed to stop when there is a mismatch,just as scanf() does?

EDIT: It even works if I use "%*c%d%d%d"!!Shouldn't the scanning and processing of subsequent characters stop once there is a mismatch between the format specifier and input at the beginning?

Here's the format of the data in my file data.txt:

Output: