如何在printf表达式中包含变量?
这是我的例子:
printf "%${cols}s", $_;
Run Code Online (Sandbox Code Playgroud)
$cols列数在哪里,$_是一个字符串.
该语句导致"无效转换"警告.
问题最终是我忘记了变量.嘎.感谢大家.
我刚刚在维基百科上找到了这段代码.
链接:http://en.wikipedia.org/wiki/Sizeof#Use
代码:
/* the following code illustrates the use of sizeof
* with variables and expressions (no parentheses needed),
* and with type names (parentheses needed)
*/
char c;
printf("%zu,%zu", sizeof c, sizeof(int));
Run Code Online (Sandbox Code Playgroud)
它声明:"z前缀应该用于打印它,因为每个架构的实际大小可能不同."
我在我的编译器上尝试过,但它给出了以下结果:
"祖,祖"
我想在C中使用printf()函数打印一些东西,而不包括stdio.h,所以我编写了程序:
int printf(char *, ...);
int main(void)
{
printf("hello world\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以上程序是否正确?
我看到一些代码看起来像:
fprintf(fd, "%4.8f", ptr->myFlt);
Run Code Online (Sandbox Code Playgroud)
这些天我没有使用C++,我阅读了关于printf及其同类的文档,并了解到在这种情况下4是"宽度",8是"精度".宽度定义为输出占用的最小空间数,如果需要,填充带前导空格.
在这种情况下,我无法理解像"%4.8f"这样的模板的重点是什么,因为在该点之后的8(如果必要的话填零)小数点已经确保满足4的宽度并且超标.所以,我在Visual C++中写了一个小程序:
// Formatting width test
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("Need width when decimals are smaller: >%4.1f<\n", 3.4567);
printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
printf("Doesn't matter if argument has no decimal places: >%4.8f<\n", (float)3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了以下输出:
Need width when decimals are smaller: > 3.5<
Seems unnecessary when decimals are greater: >3.45670000<
Doesn't matter if argument has no decimal places: >3.00000000<
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,精度小于指定的宽度,实际上增加了前导空间.然而,当精度更高时,宽度似乎是多余的.
是否有这样的格式的原因?
snprintf(3)的Linux手册页给出了以下示例:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *
make_message(const char *fmt, ...)
{
int n;
int size = 100; /* Guess we need no more than 100 bytes */
char *p, *np;
va_list ap;
if ((p = malloc(size)) == NULL)
return NULL;
while (1) {
/* Try to print in the allocated space */
va_start(ap, fmt);
n = vsnprintf(p, size, fmt, ap);
va_end(ap);
/* Check error code */
if (n < 0)
return NULL;
/* If that worked, return …Run Code Online (Sandbox Code Playgroud) 我已经看过各种官方消息来源如何做到这一点,但我找不到它.想象一下,你有以下枚举(我知道golang没有经典意义上的枚举):
package main
import "fmt"
type LogLevel int
const (
Off LogLevel = iota
Debug
)
var level LogLevel = Debug
func main() {
fmt.Printf("Log Level: %s", level)
}
Run Code Online (Sandbox Code Playgroud)
我能从上面得到的最接近的%s,这给了我:
Log Level: %!s(main.LogLevel=1)
Run Code Online (Sandbox Code Playgroud)
我想拥有:
Log Level: Debug
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
我在C语言中以字符串定义的十六进制数字:
char chars[] = "\xfb\x54\x9c\xb2\x10\xef\x89\x51\x2f\x0b\xea\xbb\x1d\xaf\xad\xf8";
Run Code Online (Sandbox Code Playgroud)
然后我想将这些值与另一个值进行比较.它不起作用,如果我打印的值如下:
printf("%02x\n", chars[0]);
Run Code Online (Sandbox Code Playgroud)
它写道fffffffb.为什么这样以及如何fb准确地获得价值?
以下代码
x = [1.1, 2.22, -3.3; 4.44, 5.55, 6.6];
fmt = '%.16g ';
y = num2str(x, fmt)
Run Code Online (Sandbox Code Playgroud)
在Matlab中产生不同的结果(R20105b)
y =
1.1 2.22 -3.3
4.44 5.55 6.6
Run Code Online (Sandbox Code Playgroud)
在Octave(4.0.0)
y =
1.1 2.22 -3.3
4.44 5.55 6.6
Run Code Online (Sandbox Code Playgroud)
不同之处在于对齐:在Matlab中,列是右对齐的,而在Octave中,它们没有对齐.
我想在Octave中完全实现Matlab的行为.你知道任何解决方案吗?当然我可以编写自己的函数,但也许已经存在一个解决方案.
编辑
另一个区别是如何处理多维数组.例如,
x = cat(3, magic(3), -magic(3));
fmt = '%.16g ';
y = num2str(x, fmt)
Run Code Online (Sandbox Code Playgroud)
在Matlab中生成
y =
8 1 6 -8 -1 -6
3 5 7 -3 -5 -7
4 9 2 -4 -9 -2
Run Code Online (Sandbox Code Playgroud)
在Octave
y = …Run Code Online (Sandbox Code Playgroud) 我有一个我想要打印的数组或矩阵,但只有三位数的精度.我怎么做.我尝试了以下内容.
> @printf("%.3f", rand())
0.742
> @printf("%.3f", rand(3))
LoadError: TypeError: non-boolean (Array{Bool,1}) used in boolean context
while loading In[13], in expression starting on line 1
Run Code Online (Sandbox Code Playgroud)
更新:理想情况下,我只想调用一个函数,printx("{.3f}", rand(m, n))而不必进一步处理我的数组或矩阵.