Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C编程中如何调用变量来指定printf的格式_C_Variables_Unix_Printf - Fatal编程技术网

在C编程中如何调用变量来指定printf的格式

在C编程中如何调用变量来指定printf的格式,c,variables,unix,printf,C,Variables,Unix,Printf,我想使用变量来指定C编程中printf的格式 我是一个C编程新手,虽然我已经练习过bash shell scritps 在bash脚本中,我可以使用一个变量来指定printf的格式,如下所示 #!/bin/bash ### Variable format="%4s%4d %8s\n" ### Main printf "$format" $1 $2 $3 那么,在C编程中是否有类似的方法 在C语言中可能吗 printf格式的字符串包括字符和数字 我听说

我想使用变量来指定C编程中
printf
的格式

我是一个C编程新手,虽然我已经练习过bash shell scritps

在bash脚本中,我可以使用一个变量来指定
printf
的格式,如下所示

#!/bin/bash

### Variable
format="%4s%4d %8s\n"

### Main
printf "$format" $1 $2 $3
那么,在C编程中是否有类似的方法

在C语言中可能吗

printf
格式的字符串包括字符和数字

我听说C编程使用不同的声明; i、 int或char

在C编程中是否有类似于上述[
format=“%4s%4d%8s\n”
]的方法 可能吗 在C中

是的,两个问题都有几种解决方法。
其中,您可以使用来准备缓冲区:

char format[80] = {0};//create editable char array
int a = 12;
char buf1[] = {"this is string 1"};
char buf2[] = {"this is string 2"};

sprintf(format, "%s", "%4s%4d %8s\n");
printf(format, buf1, val, buf2);
更接近您在示例中所做的(
format=“%4s%4d%8s\n”
),您只需将
format
定义为:

或者,创建一个已初始化但可编辑的字符数组

char format[] = {"%4s%4d %8s\n"};//editable, but only up to 
                                 //strlen("%4s%4d %8s\n"); 
                                 //(+ NULL, which is implied when using "...".)
请注意,C还提供了一个内置功能,用于在输出浮点数时启用精度的运行时设置:

double val = 22.123445677890;
int precision = 3;

printf("%.*f", precision ,  val);

将输出
22.123

是的,您可以动态生成格式字符串:

char fmt[20];
int b;
char a[SOME_SIZE];
char c[SOME_SIZE];

...
sprintf( fmt, "%%4s%%4d %%8s\n" ); // need %% to print a literal %, so fmt will contain "%4s%4d %8s\n"
printf( fmt, a, b, c );
还有其他动态构建格式字符串的方法,这只是最简单的方法

我听说C编程使用不同的声明;i、 int或char


是的,每个格式说明符都希望其相应的参数具有正确的类型
%d
期望其相应的参数具有type
int
%s
期望其参数具有type
char*
,等等。如果它们不匹配,那么行为是未定义的,您可能会得到外观怪异的输出。

多亏您的支持,我可以实现我的第一个目的

我在下面使用符号常量或“char anyname[]=string”编写了这样的代码

#包括
#定义格式“%30s\n”
int main()
{
char c_string[]=“你成功了!”;
printf(格式,c_字符串);
返回0;
}
/*
int main()
{
字符格式[]=%s\n;
char c_string[]=“你成功了!”;
printf(格式,c_字符串);
返回0;
}
*/

谢谢你的建议

您甚至可以将
printf
转换为在另一个
printf
中使用的格式字符串。是的,您可以,但这样做很少有用。谢谢大家!虽然我已经了解了很多方法,但到目前为止,代码中每一行的含义都很难理解,因为今天仍然是从C编程学习开始的第二天。当我能够理解样本行时,我将选择最佳答案。我感谢社区的所有支持。谢谢你的有用建议。我可以再问一个问题吗?在我自己的帖子中,我使用了“双引号,如”char format[]=%s\n“。然后,您的示例代码使用类似{}的“char format[80]={0};”。{}大括号有特殊意义吗?在这种情况下应该使用它们吗?因为我是个新手,我想知道它们有什么不同。@CaptainCookie-是的,
{0}
C
的一种方法,可以确保
数组的所有元素在用作初始值设定项时都归零。有一个伟大的讨论一般初始化,包括这一个。我会检查建议的网页!今天我可以理解另一个问题中单引号和双引号的区别。我也想了解{}。非常感谢。
char fmt[20];
int b;
char a[SOME_SIZE];
char c[SOME_SIZE];

...
sprintf( fmt, "%%4s%%4d %%8s\n" ); // need %% to print a literal %, so fmt will contain "%4s%4d %8s\n"
printf( fmt, a, b, c );
#include <stdio.h>

#define FORMAT "%30s\n" 

int main()
{
    char c_string[] = "You Suceed!";

    printf(FORMAT, c_string);

    return 0;
}

/*
int main()
{
    char format[] = "%s\n"; 
    char c_string[] = "You Suceed!";

    printf(format, c_string);

    return 0;
}
*/