这个简单的方法只是创建一个动态大小为 n 的数组,并用值 0 ... n-1 对其进行初始化。它包含一个错误,malloc() 仅分配 n 而不是 sizeof(int) * n 字节:
int *make_array(size_t n) {
int *result = malloc(n);
for (int i = 0; i < n; ++i) {
//printf("%d", i);
result[i] = i;
}
return result;
}
int main() {
int *result = make_array(8);
for (int i = 0; i < 8; ++i) {
printf("%d ", result[i]);
}
free(result);
}
Run Code Online (Sandbox Code Playgroud)
当您检查输出时,您会发现它会按预期打印一些数字,但最后的数字是乱码。然而,一旦我在循环中插入 printf() ,输出就出奇地正确,尽管分配仍然是错误的!是否有某种与 printf() 相关的内存分配?
我正在使用 printf 在 bash 脚本中格式化数字
printf "%04d" $input # four digits width with leading zeros if shorter
Run Code Online (Sandbox Code Playgroud)
如果input="011"printf 假定该值是八进制并返回0009而不是0011.
是否可以强制 printf 假设所有值都是十进制?
想要打印 uint32 十六进制。尝试过“PRIx32”,对我来说看起来很糟糕:
967407c0 23 4481d55f ffffff52 de3cd140
2fc aa7363cf fffff40d 563270c0 2b86
Run Code Online (Sandbox Code Playgroud)
所以...尝试了这个。肮脏但有效:
/*******************************************************************************
Print a digit in hex.
*******************************************************************************/
void mpal_digit_t_print_hex(
mpal_digit_t * input)
{
uint8_t p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8;
p_1 = (uint8_t)(((*input) << 0) >> 28);
p_2 = (uint8_t)(((*input) << 4) >> 28);
p_3 = (uint8_t)(((*input) << 8) >> 28);
p_4 = (uint8_t)(((*input) << 12) >> 28);
p_5 = (uint8_t)(((*input) << 16) >> 28);
p_6 = (uint8_t)(((*input) << 20) >> 28);
p_7 = …Run Code Online (Sandbox Code Playgroud) 看看代码吧:(简单得我自己都不敢相信)
package log
import "fmt"
func P(format string,a ...interface{}){
fmt.Printf(format,a)
}
Run Code Online (Sandbox Code Playgroud)
当在这样的地方被调用时:
log.P("%s,%s,%d","","",0)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
[ %!s(int=0)],%!s(MISSING),%!d(MISSING)
Run Code Online (Sandbox Code Playgroud)
但是如果我像这样直接调用 fmt.Printf :
fmt.Printf("%s,%s,%d","","",0)
Run Code Online (Sandbox Code Playgroud)
它工作得非常完美,非常完美(当然,作为 fmt 的基本用法)。
所以问题是:
供参考:
我相信这很简单,但我只是无法通过谷歌找到答案,从来没有人掉进过地狱?
或者也许我只是不知道如何问,所以我把纯代码放在上面。
或者只是我这次是个超级傻瓜?
我今天注册了 stackoverflow 来寻求这个问题的答案。让我知道我出了什么问题。尽快...
我正在使用 gcc (-std=gnu99) 进行嵌入式工具链 (Myriota) 的开发,但 printf 存在问题。
当我尝试以下代码时:
long long int time = TimeGet();
printf("\nSeconds since epoch: %lld\r\n", time);
Run Code Online (Sandbox Code Playgroud)
它打印:
Seconds since epoch: ld
Run Code Online (Sandbox Code Playgroud)
使用"%" PRId64打印相同的“ld”。
有任何想法吗?如果您能指出我正确的地方,我将不胜感激。
编辑
变量类型已更正long long int time
我想将浮点变量的值打印到屏幕上。我printf()在 LLVM IR 代码中声明函数,并且它已成功链接。
每当我打印整数或字符数据类型或字符串时,都会printf()像在 C 代码中打印它们一样将它们正常打印到屏幕上。但是,如果我将 a 传递给float,printf()它不会打印浮点数,而是打印0.000000。我检查了多次源代码,语法似乎是正确的。应该是打印的吧2.75!我正在查看这段代码,我完全不明白代码的行为与我编写的代码有何不同。
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@obj1 = global {i32, float, i8} zeroinitializer
@format_string = constant [10 x i8] c"%i %f %c\0A\00"
declare i32 @printf(i8*, ...)
define i32 @main() {
entry:
%obj1 = load {i32, float, i8}, {i32, float, i8}* @obj1
%obj2 = insertvalue {i32, float, i8} %obj1, i32 44, 0
%obj3 = insertvalue {i32, float, i8} …Run Code Online (Sandbox Code Playgroud) 我想使用 打印粗体文本printf。我该怎么做?
printf '%s\n' "\033[1m"bold_text"\033[0m"
Run Code Online (Sandbox Code Playgroud)
不起作用。它显示:
\033[1mbold_text\033[0m
Run Code Online (Sandbox Code Playgroud)
但是,相同的字符串可以很好地使用echo -e:
echo -e "\033[1m"bold_text"\033[0m"
bold_text
Run Code Online (Sandbox Code Playgroud) 我已经解析了文本文件中包含 url 和附加换行符的所有行,并且我想让链接在新文件中可单击。
如何<a href>使用标准 Linux 工具(最好是 awk)仅在 url 周围附加 -tags?它需要在 cron 中实现自动化。
例如,
源文件chaturls.txt:
12:30 <user> check this: https://link.to/stuff.jpg</br>
13:47 <user4> https://another.link.lol eyyyy</br>
Run Code Online (Sandbox Code Playgroud)
新文件chatlinkified.html中的所需输出:
12:30 <user> check this: <a href='https://link.to/stuff.jpg'>https://link.to/stuff.jpg</a></br>
13:47 <user4> <a href='https://another.link.lol'>https://another.link.lol</a> eyyyy</br>
Run Code Online (Sandbox Code Playgroud)
我尝试过awk '{printf "<a href=\"%s\">%s</a><br>", $0,$0}' chaturls.txt > chatlinkified.html,但这使得整行成为(无效)可点击链接。
假设我的代码是
void main()
{
for(i=1;i<5;i++)
{
printf("%d, ",i);
}
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出将是
1, 2, 3, 4,
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到像这样的输出
1, 2, 3, 4
Run Code Online (Sandbox Code Playgroud)
最后一个,应该被省略。
在 OCaml 中,当传递应该适用于Printf.printf. 可能是因为我不完全理解该功能,但我无法确定什么不起作用。
首先,我定义一个函数(用于日志记录):
utop # let log verbosity level str =
if level <= verbosity then (
Printf.printf "\nLevel %i: " level;
Printf.printf str);;
val log : int -> int -> (unit, out_channel, unit) format -> unit = <fun>
Run Code Online (Sandbox Code Playgroud)
一切似乎都很好,但后来我得到了这个:
utop # log 0 0 "%i" 0;;
Error: This function has type
int -> int -> (unit, out_channel, unit) format -> unit
It is applied to too many arguments; maybe you forgot a `;'.
Run Code Online (Sandbox Code Playgroud)
尽管以下作品有效:
utop …Run Code Online (Sandbox Code Playgroud)