我试图用这样的方法填充一个带有double值的字符串sprintf:
sprintf(S, "%f", val);
Run Code Online (Sandbox Code Playgroud)
但精度正在被削减到小数点后六位.我需要大约10个小数位才能达到精度.
怎么能实现呢?
我看了看但找不到合适的答案.
我想知道printf是如何工作的,如下所示:
char arr[2] = {5,6};
printf ("%d%d",arr[0],arr[1]);
Run Code Online (Sandbox Code Playgroud)
我认为printf只是遍历格式,当它包含%d时,例如它从它的当前位置读取4个字节...但是这是错误的,因为上面的工作完美.
那么,我哪里错了?
printf转换规范%后面是标志,宽度,精度,长度修改器和转换说明符.转换规范的大小是否有实际限制?
即%s2个字符长,而%08.2f6个字符长.我的问题是,根据C99标准,可以创建的格式字符串中的最大单一规范的长度是多少?
可能重复:
在linux gcc中使用fork()
#include <stdio.h>
void main ()
{
printf ("ciao");
fork ();
}
Run Code Online (Sandbox Code Playgroud)
我有一些关于C优化的想法,但我不确定.希望你知道答案.
有没有办法打印一定数量的空白字符?
我不能用min.宽度空白填充"\"%-5s\"",因为它会导致"str "...,我需要输出"str" ...
我知道我可以做一个愚蠢的方式:
int len = strlen (str);
printf ("/"%s/"", str);
for (int i = len - 5; i > 0; i--)
printf (" ");
Run Code Online (Sandbox Code Playgroud)
但我会感谢更有效的解决方法.
假设我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int num1 = 0;
int main(int argc, char **argv){
double num2;
int *ptr = &num1;
printf(argv[1]);
if (num1== 2527){
printf("Well done");
}
if(num2 == 4.56)
printf("You are a format string expert");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在努力了解如何正确行事,但我无法用互联网上的指南来组织我的思想.
它假设是这样的:
./Program %p %p %p %p
Run Code Online (Sandbox Code Playgroud)
然后
./Program $( printf "\xAA\xAA\xAA\xAA") %.2523d%n
Run Code Online (Sandbox Code Playgroud)
我只是想不出来,请帮我解决这个问题.
其重点是通过prinft函数将字符串利用到正在运行的程序中.我需要打印"完成"和"你是格式字符串专家".就我而言,通过Linux终端/ shell.正如HuStmpHrrr所注意到的:这确实应该是White Hacking - Software Security
我想使用Visual Studio在C中扫描和打印字符串.
#include <stdio.h>
main() {
char name[20];
printf("Name: ");
scanf_s("%s", name);
printf("%s", name);
}
Run Code Online (Sandbox Code Playgroud)
在我这样做之后,它不打印名称.会是什么呢?
我正在尝试打开一个文件进行编写和使用printf来进行格式化,但文档和现实似乎并不一致.我错过了什么吗?
To exit type 'exit' or '^D'
> my $fh=open "test", :w;
IO::Handle<"test".IO>(opened, at octet 0)
> $fh.printf: "test";
No such method 'printf' for invocant of type 'IO::Handle'
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
但根据文档我的代码似乎没问题:
https://docs.perl6.org/routine/printf
Run Code Online (Sandbox Code Playgroud)
非常感谢你 !!
此代码的问题在于,在用户在命令行中输入某些文本后,它实际上不会打印任何内容.
代码的目的是接受用户在文件名后通过命令提示符输入的行数.然后用户将输入内容进行反转.该程序应该反转每行的用户输入.
示例输入=大红狗
示例输出=狗红大
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 80
char * reverseWords(char *string);
//argc is the count of cmd arguments.
//each command line argument is of type string
int main(int argc, char *argv[]){
//initialize local variables
int i;
int N;
char str[SIZE];
for(i = 1; i <argc; i++)
{
//set N equal to the users number in the command line
N = atoi(argv[i]);
}
if(argc != 2){//2 means that something is in the argument. …Run Code Online (Sandbox Code Playgroud) 是否有可能以列表或data.frame而不是单独的向量来提供R的sprintf(fmt,...)函数?
例如,我想要实现的目标:
df <- data.frame(v1 = c('aa','bb','c'),
v2 = c('A', 'BB','C'),
v3 = 8:10,
stringsAsFactors = FALSE)
sprintf('x%02s%02s%02i', df[[1]], df[[2]], df[[3]])
[1] "xaa A08" "xbbBB09" "x c C10"
Run Code Online (Sandbox Code Playgroud)
但有一个更简洁的语法,如:
sprintf('x%02s%02s%02i', df)
Error in sprintf("x%02s%02s%02i", df) : too few arguments
Run Code Online (Sandbox Code Playgroud)
在我的实际情况中,我有更多的列来提供sprintf,使代码变得丑陋.
我想更普遍的问题是如何规避省略号...功能.
我肯定不是第一个问这个问题,但我找不到任何方向......