如果某些条件为真,我只想打印 printf。我知道 printf 是一个可变参数函数,但遗憾的是我似乎无法在这里找到任何解释我可以包装它的线程。
基本上我写的代码中的每一个:
printf(" [text and format] ", ... args ...);
Run Code Online (Sandbox Code Playgroud)
我想写一些类似的东西
my_custom_printf(" [text and format] ", ... args ...);
Run Code Online (Sandbox Code Playgroud)
然后是这样实现的:
int my_custom_printf(const char* text_and_format, ... args ...)
{
if(some_condition)
{
printf(text_and_format, ... args...);
}
}
Run Code Online (Sandbox Code Playgroud)
条件的第一个版本将独立于 args(它将在某个全局变量上),但将来可能会成为需要的条件一个参数。
无论如何,现在我只需要... args ...原型中的语法和my_custom_printf.
我正在使用 GCC,但我不知道哪个 C 标准 - 但我们可以尝试一下。
我遇到了这个代码
输入:
3
0.1227...
0.517611738...
0.7341231223444344389923899277...
Run Code Online (Sandbox Code Playgroud)
int N;
char x[110];
int main() {
scanf("%d\n", &N);
while (N--) {
scanf("0.%[0-9]...\n", &x);
printf("the digits are 0.%s\n", x);
Run Code Online (Sandbox Code Playgroud)
我无法理解几件事:
1.为什么不使用double数组(因为输入值是double)?
2.scanf中[0-9]后的那三个点...有什么作用?和
3.为什么我们不在 scanf 中的 [0-9] 之后使用任何格式说明符(在这种情况下为 d)?
我用这段代码编译了c程序
printf("|%5c|", '\0'); 宽度 5 只是举例
然后在 a.out 上使用 cat -e
./a.out | cat -e
并在输出上有这个
| ^@|$
'\0' 表示为 ^@ 对吗?它是什么以及它是如何工作的?
printf()当我们在没有格式说明符的情况下向它提供多个参数时,它的行为是什么?
例子:
int main()
{
printf("hello", "hi");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器在编译上述程序时会产生警告?:
warning: too many arguments for format [-Wformat-extra-args]
Run Code Online (Sandbox Code Playgroud)
如果我们编译如下类似的程序:
int main()
{
char *s1 = "hello";
char *s2 = "hi";
printf(s1, s2);
}
Run Code Online (Sandbox Code Playgroud)
不产生警告。这是什么原因?
另外,为什么两个程序hello都只输出而不打印hi?
#include <stdio.h>
void fillline(char *line, char c, int len){
for(int i = 0; i<len-1; i++)
line[i] = c;
line[len-1] = '\n';
line[len] = '\0';
}
int main() {
char xs[4][30];
fillline(xs[0], '-', 30);
fillline(xs[1], '(', 30);
fillline(xs[2], ')', 30);
fillline(xs[3], 'Z', 30);
printf("%s", xs[0]);
printf("%s", xs[1]);
printf("%s", xs[2]);
printf("%s", xs[3]);
}
Run Code Online (Sandbox Code Playgroud)
你好,这里是 C 编程新手。我上面的代码有问题。我预计
-----------------------------
(((((((((((((((((((((((((((((
)))))))))))))))))))))))))))))
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Run Code Online (Sandbox Code Playgroud)
作为从我的代码生成的命令行输出,但我得到
-----------------------------
(((((((((((((((((((((((((((((
)))))))))))))))))))))))))))))
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
(((((((((((((((((((((((((((((
)))))))))))))))))))))))))))))
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
)))))))))))))))))))))))))))))
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我试着fflush(stdout)和setbuf(stdout, NULL)(虽然我绝对不知道这是stdout-问题)。
先感谢您。
我是一个 C 编程新手。
通过其他问题的答案,我可以理解“角色”一词可能指向不同的元素;例如字母和数字。
我将“字符串”视为字符。
然后,请看以下几行:
#include <stdio.h>
main()
{
int numbers = 123;
printf("%c%s\n", "N", numbers);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此代码时,显示错误消息并且编译失败。
教科书“C 编程语言”第 13 页对 printf 格式说明符的说明如下;
“
%c为性格”“
%s为字符串”。
第一个问题:
虽然“N”是一个字符,但为什么%c说明符无效?
第二个问题:
尽管变量“numbers”的值为“123”,但它们是数字,包含在字符中。
我认为字符等于“字符串”。
那么,虽然我用 %s 表示“123”,但为什么无效呢?
我知道我的代码会起作用,如果我像下面那样修改它们:
#include <stdio.h>
main()
{
int numbers = 123;
printf("%s%d\n", "N", numbers);
}
Run Code Online (Sandbox Code Playgroud)
但我想了解为什么上面的代码无效。
我有一个用 C 编写的函数,它由一个像这样的指针变量组成
#include<stdio.h>
void print()
{
char *hello="hello world";
fprintf(stdout,"%s",hello);
}
void main()
{
print();
print();
}
Run Code Online (Sandbox Code Playgroud)
如果我调用 print() 函数两次,它会为hello变量分配内存两次吗?
#include <stdio.h>
#include <math.h>
int main() {
float g1, g2, c1, c2, dismul, cadd, csub, dis;
printf("Enter the latitudes of the places (L1 and L2): ");
scanf("%f %f", &c1 ,&c2);
printf("Enter the longitudes of the places (G1 and G2): ");
scanf("%f %f ", &g1, &g2);
cadd = cos(c1 + c2);
csub = cos(g2-g1);
dismul = cadd * csub;
dis = 3963 * acos(dismul);
printf("The distance between the places is:%f\n", dis);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我写了这段代码来给出两点之间的距离,但是当我运行它时,它不会给出最后一个,printf直到我输入任何字母并按回车键。
我注意到printf("%#g\n", 0.0)任何 gcc/clang 版本与 Windows 7 上的 Visual Studio 2019(截至今天的最新版本)都会提供不同的输出。
gcc/clang 给出0.00000(总共 6 位数字,在 之后 5 位.)而 VS 给出0.000000(总共 7 位,在 之后 6 位.)。同样,"%#.8g"gcc/clang 总共给出 8 位数字,VS 总共给出 9 位数字。
问题:
我正在使用 C 字符串,如以下程序所示:
#include <stdio.h>
int main(void){
char *player1 = "Harry";
char player2[] = "Rosie";
char player3[6] = "Ronald";
printf("%s %s %s\n", player1, player2, player3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打印以下内容:
Harry Rosie RonaldRosie
Run Code Online (Sandbox Code Playgroud)
为什么“Rosie”会打印两次?