标签: printf

printf转换规范的大小限制

printf转换规范%后面是标志,宽度,精度,长度修改器和转换说明符.转换规范的大小是否有实际限制?

即%s2个字符长,而%08.2f6个字符长.我的问题是,根据C99标准,可以创建的格式字符串中的最大单一规范的长度是多少?

c printf c99 format-string

6
推荐指数
1
解决办法
2322
查看次数

Linux程序集和printf

我尝试用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)

c assembly printf nasm

6
推荐指数
1
解决办法
7018
查看次数

结构数组 - 删除/添加元素和打印

对于以下代码

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有关

c arrays printf struct

6
推荐指数
1
解决办法
4万
查看次数

arm-none-eabi-gcc:使用printf打印浮点数

我正在为SAM3N arm cortex-M3微控制器编写一个C程序.当我尝试打印浮点数时,它只打印'f'.示例:仅printf("%f",43.12);打印f,而不是43.12.

但用整数打印效果很好.

如何启用浮动的完整打印?我知道默认情况下,编译器禁用浮动打印以减少代码大小(即看起来,它们链接了缩减版本).另请注意,CFLAGS=-Dprintf=iprintfmakefile中没有使用.

工具详情:

  • ARM/GNU C编译器:(crosstool-NG 1.13.1 - Atmel build:13)4.6.1
  • 以上工具附带Atmel studio 6.0.

c printf gcc arm

6
推荐指数
2
解决办法
1万
查看次数

为什么此代码使用带字符串的映射(C++)会出现运行时错误?

为什么这段代码有运行时错误?

#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,则会产生运行时错误.我该如何纠正?谢谢!

c++ string printf stl map

6
推荐指数
1
解决办法
2084
查看次数

printf ascii char%有时给出c而不是%

我对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.

c printf ascii

6
推荐指数
1
解决办法
737
查看次数

如何在不描述每个元素的格式的情况下打印数组?

我想打印几个数组,输出元素的字段宽度为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)

谢谢

arrays perl printf

6
推荐指数
2
解决办法
6868
查看次数

在使用printf时如何在shell脚本中转​​义特殊字符?

我正在尝试使用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)

linux bash shell printf

6
推荐指数
4
解决办法
1万
查看次数

打印n个空格字符 - (f)printf格式

有没有办法打印一定数量的空白字符?

我不能用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)

但我会感谢更有效的解决方法.

c c++ printf

6
推荐指数
1
解决办法
3046
查看次数

perl6 IO :: Handle没有printf方法,与文档不一致,或者我错过了什么?

我正在尝试打开一个文件进行编写和使用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)

非常感谢你 !!

printf file perl6

6
推荐指数
2
解决办法
93
查看次数

标签 统计

printf ×10

c ×6

arrays ×2

c++ ×2

arm ×1

ascii ×1

assembly ×1

bash ×1

c99 ×1

file ×1

format-string ×1

gcc ×1

linux ×1

map ×1

nasm ×1

perl ×1

perl6 ×1

shell ×1

stl ×1

string ×1

struct ×1