首先,对于任何交叉发布,我们深表歉意。希望我不会在这里重复问题,但我无法在其他地方找到它(通过 Google 和 Stack Overflow)。
这是错误的要点。如果我打电话printf,sprintf或者fprintf我的代码中的任何位置,显示浮动,我得到一个SIGSEGV (EXC_BAD_ACCESS)错误。让我举个例子吧。
以下抛出错误:
float f = 0.5f;
printf("%f\n",f);
Run Code Online (Sandbox Code Playgroud)
此代码不会:
float f = 0.5f;
printf("%d\n",f);
Run Code Online (Sandbox Code Playgroud)
我意识到那里有一个隐式转换,但我并不关心。我只是无法理解为什么打印浮点数与打印整数会引发错误。
注意:部分代码用于malloc创建一些非常大的多维数组。但是,这些打印语句没有以任何方式引用这些数组。这是我如何声明这些数组的示例。
#define X_LEN 20
#define XDOT_LEN 20
#define THETA_LEN 20
#define THETADOT_LEN 20
#define NUM_STATES (X_LEN+1) * (XDOT_LEN+1) * (THETA_LEN+1) * (THETADOT_LEN+1)
#define NUM_ACTS 100
float *states = (float *)malloc(NUM_STATES * sizeof(float));
// as opposed to float states[NUM_STATES] (more memory effecient)
float **q = (float**)malloc(NUM_STATES * sizeof(float*)); …Run Code Online (Sandbox Code Playgroud) 这个 Perl 脚本是我想在 JavaScript 中实现的(源代码):
s/([0-9]+)/sprintf('%04d',$1)/ge;
Run Code Online (Sandbox Code Playgroud)
显然sprintf在 JavaScript 中不可用,但我知道您可以构建一个直接处理关注数量的函数,但是在我的情况下,我有一个长字符串,其中可能包含多个数字,可以将这样的字符串转换为abc8 23 123:(abc000008 000023 000123使用以 6 位为例)。
所以它要么是我没有得到的一些聪明的正则表达式,要么以某种方式找到某种方法将长字符串分成不同的字符串和数字的数组,并且只对数字一个一个地进行操作。
我真的想避免后一种方法,因为 1) 在这种情况下速度是一个问题,并且 2) 在正确的位置拆分字符串所涉及的正则表达式似乎比在这一点上添加前导零更难。
最后,还有其他方法可以使用 JavaScript 的正则表达式吗?
为什么会打印-1
#include <stdio.h>
int main(){
unsigned int i = -1;
printf("%d", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然这打印
#include <stdio.h>
int main(){
unsigned int i = -1;
printf("%u", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
最大可能的整数值
另外,为什么会这样
#include <stdio.h>
int main(){
int c = printf("Hello");
printf("%d", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打印 hello5 而不是 5。
埃里克的反驳//
你提到了副作用,对吧?
#include <stdio.h>
int main(){
int i = 0;
for (; i++; printf("%d", i));
printf("%d", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么 for 循环内的 printf ("%d",i) 不打印此代码中的值作为副作用?
上下文:我正在使用 Awesome package 创建 .docx 表flextable。该包具有在最终输出中格式化数字的功能:set_formatter_type()。如果我理解正确的话,这个函数需要一个可以被使用的字符输入sprintf()。我能够使用以下命令实现预期的输出set_formatter(),但需要明确命名每一列,而我无法对真实的表执行此操作。
问题:我无法找到如何使用sprintf()与flextable::set_formatter_type().
可以使用以下命令实现正确的格式设置formatC(),但此功能不适用于flextable::set_formatter_type()
formatC(x = signif(1715235456.5684, 3), big.mark = " ", digits = 0, format = "f")
[1] "1 720 000 000"
Run Code Online (Sandbox Code Playgroud)
使用sprintf()我能够实现:
sprintf("%.0f", signif(1715235456.5684, 3))
[1] "1720000000"
Run Code Online (Sandbox Code Playgroud)
可重现的例子:
df <- flextable::flextable(iris[1:3]*1000)
flextable::set_formatter_type(df, fmt_double = "%.0f") # works fine but I can't get big mark separator
flextable::set_formatter_type(df, fmt_double = function(x) formatC(x, digits = 0, big.mark = …Run Code Online (Sandbox Code Playgroud) 如何将下面脚本中生成的信息(包括以下格式的字符串和表格)保存到文本文件?
格式:
M=5
1 21
2 22
3 23
4 24
5 25
5points
Run Code Online (Sandbox Code Playgroud)
脚本:
clc;
clear all;
close all;
ElmentsNum = "EM=5";
x = (1:5)';
y =(21:25)';
t = table(x,y);
M = "5points"
fileID = fopen('E:/Data.txt');
fprintf(fileID,'%s/n',ElmentsNum)
fprintf(fileID,'%.4f',t)
fprintf(fileID,'%s/n',M)
fclose(fileID)
Run Code Online (Sandbox Code Playgroud) 谁能向我解释为什么它打印 32 以及它如何工作的总体概念?
#include <stdio.h>
int main()
{
int a=1;
for (printf ("3"); printf ("2"); printf ("1"))
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想使用printf、fprintf等将 C 风格代码重构为 C++。是否std::format容易受到格式字符串攻击,如前面提到的 C 函数?
如果我搜索格式字符串攻击,我发现的只是stdio 格式字符串漏洞。我想更多地了解是否std::format容易受到攻击,以及如何缓解它,即使我必须格式化用户提供的字符串。
示例代码:
#include <stdio.h>
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include <float.h>
#ifdef FLT16_MAX
_Float16 f16;
int main(void)
{
printf("%f\n", f16);
return 0;
}
#endif
Run Code Online (Sandbox Code Playgroud)
调用:
# gcc trunk on linux on x86_64
$ gcc t0.c -std=c11 -Wall
Run Code Online (Sandbox Code Playgroud)
预期诊断:
<nothing>
Run Code Online (Sandbox Code Playgroud)
实际诊断:
t0.c:9:14: warning: format '%f' expects argument of type 'double', but argument 2 has type '_Float16' [-Wformat=]
9 | printf("%f\n", f16);
| ~^ ~~~
| | |
| | _Float16
| double
Run Code Online (Sandbox Code Playgroud)
这是否意味着在__STDC_WANT_IEC_60559_TYPES_EXT__AND 下如果FLT16_MAX定义了 gcc 不知道printf可以与 一起使用_Float16?是否应该有所了解? …
我偶然发现GNU C 库文档中存在以下缺陷:
\n\n\n\xe2\x80\x98%f\xe2\x80\x99
\n以正常(定点)表示法打印浮点数。有关详细信息,请参阅浮点转换。
\n
%F尽管根据C99 标准,但它没有在任何地方提到:
\n\nF 转换说明符分别生成 INF、INFINITY 或 NAN,而不是 inf、infinity 或 nan。
\n
一个简单的例子来测试这一点:
\ndouble inf = 1.0 / 0.0;\nprintf( "Testing on %ld.%ld, %%f: %f and %%F: %F.\\n", __GLIBC__, __GLIBC_MINOR__, inf, inf );\nRun Code Online (Sandbox Code Playgroud)\n这输出:
\nTesting on 2.24, %f: inf and %F: INF.\nRun Code Online (Sandbox Code Playgroud)\n所以它应该按其应有的方式工作,而不是像文档所说的那样。
\nF我在想为什么文档中没有大写功能。是否有意义?是的。符合标准吗?是的。是否能够实施?是的。所以,我不明白原因。
是否有约定将标准规定的问题自动视为库文档的一部分?使用安全吗%F以 printf 格式
我有一个简单的程序(工作示例):
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t my_pid = getpid();
char str[30];
sprintf(str, "/proc/%d/fd", my_pid);
printf("hello, I am gonna print out: %s", str);
execvp( "ls", (char *[]) { "ls", str, NULL } );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 Linux VM 上用 gcc 编译。我的问题是为什么发送到的输出printf永远不会打印。
我知道printf缓冲其输出并且仅刷新\n. 我想知道为什么在这种情况下它不打印。我读到输出流在程序退出时被刷新。正在一块内存printf中缓冲输出(我在实现中确认了这一点)。malloc
我对此的疑问(欢迎更多详细信息):
execvp导致输出打印在 上stdout,但它没有按原样打印?ls我的想法是,即使在蚕食进程之后,它是否仍然被认为是程序退出?printf的内存缓冲区会被视为输出流吗?10文件描述符。zsh255bash