printf转换规范%后面是标志,宽度,精度,长度修改器和转换说明符.转换规范的大小是否有实际限制?
即%s2个字符长,而%08.2f6个字符长.我的问题是,根据C99标准,可以创建的格式字符串中的最大单一规范的长度是多少?
我尝试用printf函数编写简单的汇编程序.我编译它nasm -f elf 64并使用链接gcc.跑完后我明白了segmentation fault.怎么了?
[Bits 32]
extern printf
global main
section .data
hello:
db "Hello", 0xa, 0
section .text
main:
push hello
call [printf]
add esp, 4
mov eax, 1
mov ebx, 0
int 80h
Run Code Online (Sandbox Code Playgroud) 对于以下代码
struct orderSlip
{
char source[64];
char destination[64];
char item[64];
int position;
};
//global
struct orderSlip data[100];
Run Code Online (Sandbox Code Playgroud)
除了以下方法之外,还有其他方法可以打印出每个元素的数据:
printf("%s\n",data[0].source);
printf("%s\n",data[0].destination);
printf("%s\n",data[0].item);
printf("%i\n\n", data[0].position);
printf("%s\n",data[1].source);
printf("%s\n",data[1].destination);
printf("%s\n",data[1].item);
printf("%i\n", data[1].position);
Run Code Online (Sandbox Code Playgroud)
等等
for(int n = 0; n< 3; n++)
{
printf("%s\n",data[n].source);
printf("%s\n",data[n].destination);
printf("%s\n",data[n].item);
printf("%i\n\n", data[n].position);
}
Run Code Online (Sandbox Code Playgroud)
要删除和添加,我是否必须创建一个动态的结构数组?如果是这样,最简单的语法是什么?像这个c ++代码的东西
int * bobby;
bobby = new int [5];
delete bobby[5];
Run Code Online (Sandbox Code Playgroud)
但在C?我猜它与malloc和free有关
我正在为SAM3N arm cortex-M3微控制器编写一个C程序.当我尝试打印浮点数时,它只打印'f'.示例:仅printf("%f",43.12);打印f,而不是43.12.
但用整数打印效果很好.
如何启用浮动的完整打印?我知道默认情况下,编译器禁用浮动打印以减少代码大小(即看起来,它们链接了缩减版本).另请注意,CFLAGS=-Dprintf=iprintfmakefile中没有使用.
工具详情:
为什么这段代码有运行时错误?
#include <cstdio>
#include <map>
#include <string>
#include <iostream>
using namespace std;
map <int, string> A;
map <int, string>::iterator it;
int main(){
A[5]="yes";
A[7]="no";
it=A.lower_bound(5);
cout<<(*it).second<<endl; // No problem
printf("%s\n",(*it).second); // Run-time error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你使用cout,它工作正常; 但是,如果使用printf,则会产生运行时错误.我该如何纠正?谢谢!
我对printf如何处理ascii字符有点困惑.
当我像这样打印字符%时,我得到一个正确的响应,如"ascii%",这很好.
printf("ascii %% \n");
printf("ascii \x25 \n");
printf("ascii %c \n", 0x25);
ascii %
Run Code Online (Sandbox Code Playgroud)
我可以把它们放在同一条线上,然后我得到"ascii %%",这也很好.
printf("ascii %c \x25 \n", 0x25);
ascii % %
Run Code Online (Sandbox Code Playgroud)
但是我不能以其他顺序那样做,因为那时我得到c而不是%,就像这个"ascii%c"
printf("ascii \x25 %c \n", 0x25);
ascii %c
Run Code Online (Sandbox Code Playgroud)
怎么了?
但是我注意到printf看起来像正常的%符号一样处理\ x25,因为如果我在输出后直接添加另一个%(\ x25%)它就会成为我的期望.
printf("ascii \x25% %c \n", 0x25);
ascii % %
Run Code Online (Sandbox Code Playgroud)
但后来我也注意到打印一个%也似乎有效,但我没想到.
printf("ascii % \n");
ascii %
Run Code Online (Sandbox Code Playgroud)
为什么这样做,我认为单个%不是printf的有效输入...有人可以澄清printf应该如何工作吗?
注意:我在Ubuntu 12.04(gcc版本4.6.3)上使用默认gcc.
我想打印几个数组,输出元素的字段宽度为3,我想我可以使用printf,但如果我使用printf那么我需要编写数组的所有元素的格式,但数组很大.
例如
@array = (1,10,100,30);
printf ("%3d %3d %3d %3d\n",$array[0],$array[1],$array[2],$array[3]);
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用循环打印元素,直到所有数组循环,但我认为这不是一个好主意.
有没有办法可以让我只描述一次元素的格式,然后自动应用于整个数组?
这样的事情?
printf ("%3d\n",@array);
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试使用printfshell 格式化字符串,我将从文件中获取输入字符串,其中包含特殊字符等%,',"",,\user, \tan.
如何转义输入字符串中的特殊字符?
例如
#!/bin/bash
#
string='';
function GET_LINES() {
string+="The path to K:\Users\ca, this is good";
string+="\n";
string+="The second line";
string+="\t";
string+="123"
string+="\n";
string+="It also has to be 100% nice than %99";
printf "$string";
}
GET_LINES;
Run Code Online (Sandbox Code Playgroud)
我希望这将以我想要的格式打印
The path to K:\Users\ca, this is good
The second line 123
It also has to be 100% nice than %99
Run Code Online (Sandbox Code Playgroud)
但它给出了意想不到的结果
./script: line 14: printf: missing unicode digit for \U
The path to K:\Users\ca, this is good …Run Code Online (Sandbox Code Playgroud) 有没有办法打印一定数量的空白字符?
我不能用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)
但我会感谢更有效的解决方法.
我正在尝试打开一个文件进行编写和使用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)
非常感谢你 !!