当我手动测试我的程序时,它可以编译并正常工作,但是当我尝试在终端中使用自动评分器时,我收到此错误。我是 C 新手,不明白它的含义以及如何修复它。
我的代码:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char* argv[]){
char *s = (char *)malloc(sizeof(char) * 100);
char prev = argv[1][0];
if(isdigit(prev)!=0){
printf("%s","ERROR");
return 0;
}
int count = 1;
for(int i=1; i<strlen(argv[1]); i++){
if(isdigit(argv[1][i])!=0){
printf("%s","ERROR");
return 0;
}
if(prev==argv[1][i]){
count++;
}else{
sprintf(s, "%s%c%d", s, prev, count);
count = 1;
}
prev=argv[1][i];
}
sprintf(s, "%s%c%d", s, prev, count);
if(strlen(s) > strlen(argv[1])){
printf("%s\n", argv[1]);
}else{
printf("%s\n", s);
}
free(s);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 C 代码从一个平台移植到另一个平台并删除依赖项。
有一个名为的调试函数dbg_out,它打印出类似printf().
原型是void dbg_out(int dbgMask, const char *pFormat, ...);
这是一个调用示例:dbg_out((5, "SerialDriver : (%04x-%08lx) \n", id, sn));
我想将此函数映射到正常printf()调用,但遇到一些问题。
到目前为止我已经尝试过了
void dbg_out (int dbgMask, const char *pFormat, ...)
__attribute__((format(printf, 2, 3)))
;
Run Code Online (Sandbox Code Playgroud)
我需要忽略第一个参数 dbgMask 并只考虑其他参数。
我更喜欢使用预处理器来定义包装器。我希望得到一些帮助。
我正在学习 C 编程语言,在 Xcode 13.x 中,当我写下以下内容时:
float a = 3 / 2;
float b = 1 / 3;
printf("3 divided by 2 as a float is %f, 1 divided by 3 as a float is %f\n", a, b);
Run Code Online (Sandbox Code Playgroud)
控制台会喊出这样的内容:
3 divided by 2 as a float is 1.000000, 1 divided by 3 as a float is 0.000000
我希望它会显示1.500000和0.333333,但为什么不显示呢?我确信解决方案是显而易见且简单的,但基本的谷歌搜索/搜索没有帮助。
我试图#include float.h认为这会有帮助,但没有运气。
我盲目地错过了什么?
谢谢
#include <stdio.h>
int main()
{
printf("%d", "123456"[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期输出是123456,但实际输出是50,这是为什么呢?
我有这个程序:
\nmy $d = 40000 * 100 / 360;\nprint "At the equator\\n";\nprintf "%9s\xc2\xb0 = %10.3f meters\\n", 10**-$_, 10**-$_ * $d for 0 .. 7;\nRun Code Online (Sandbox Code Playgroud)\n它输出
\nmy $d = 40000 * 100 / 360;\nprint "At the equator\\n";\nprintf "%9s\xc2\xb0 = %10.3f meters\\n", 10**-$_, 10**-$_ * $d for 0 .. 7;\nRun Code Online (Sandbox Code Playgroud)\n如何修复我的程序,以便指数表示法不用于较小的数字?
\n使用 int 进行 float 隐式转换时,失败并显示 printf()
#include <stdio.h>
int main(int argc, char **argv) {
float s = 10.0;
printf("%f %f %f %f\n", s, 0, s, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用它编译时gcc -g scale.c -o scale会输出垃圾
./scale
10.000000 10.000000 10.000000 -5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520512.000000
Run Code Online (Sandbox Code Playgroud)
如果我显式地将整数转换为浮点数,或者使用 a 0.0(这是 a double),它会按设计工作。
#include <stdio.h>
int main(int argc, char **argv) {
float s = 10.0;
printf("%f %f %f %f\n", s, 0.0, s, 0.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当用它编译时gcc -g scale.c -o scale确实输出预期的输出
./scale
10.000000 …Run Code Online (Sandbox Code Playgroud) c printf type-conversion implicit-conversion conversion-specifier
在 w3resource.com 上的一篇文章中,我看到了以下代码:
double x, y;
pr1 = sqrt(pr1);
x = (-b + pr1)/(2*a);
y = (-b - pr1)/(2*a);
printf("Root1 = %.5lf\n", x);
printf("Root1 = %.5lf\n", y);
Run Code Online (Sandbox Code Playgroud)
文章说取三个浮点数。但在他们的解决方案(此代码片段)中,他们使用了%lf格式说明符。他们为什么使用%lf而不是%f?我也可以使用%f代替%lf浮点数吗?
如果我使用float关键字代替double, 并使用%f, 有什么问题吗?输出对我来说是一样的。
我尝试使用简单的日志记录机制在创建日志时创建错误检查。我观察到,与 printf 相比,可以使用vfprintf(),它在编译时不会打印任何警告,传递给 format 的参数太多(参见下面的示例)。(我不希望他们有相同的行为,但我仍然期望类似的行为)。关于这个问题,即使在编译时已知可变参数的数量。在编译时是否可以通过任何方式进行检查?\n下面,我准备了一个代码示例,在编译时编译器将打印有关 printf 的警告,并且工作vfprintf()正常,没有任何错误。也许第二个可变参数被忽略了?我读过这个人,但找不到任何有用的信息。
#include <stdarg.h>\n#include <stdio.h>\n\nvoid check_printf_usage(char *fmt, ...)\n{\n va_list ap;\n \n va_start(ap, fmt);\n vfprintf(stdout, fmt, ap);\n va_end(ap);\n}\n\nint main()\n{\n int x = 1;\n int y = 2;\n printf("test nr.1 %i\\n", x, y); // works as expected, prints warning\n check_printf_usage("test nr.2 %i\\n", x, y); // does not print any warning\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n这是编译输出+程序输出:
\n#include <stdarg.h>\n#include <stdio.h>\n\nvoid check_printf_usage(char *fmt, ...)\n{\n va_list ap;\n \n va_start(ap, fmt);\n vfprintf(stdout, fmt, ap);\n va_end(ap);\n}\n\nint main()\n{\n int …Run Code Online (Sandbox Code Playgroud) 我有以下代码,它将 94 x 插入到 .txt 文件中:
printf 'x%.0s' {1..94}
Run Code Online (Sandbox Code Playgroud)
是否可以在输出 94 x 之前合并插入的新行?
我努力了:
printf '\n x%.0s' {1..94}
printf "\n x%.0s" {1..94}
Run Code Online (Sandbox Code Playgroud)
但我无法让它发挥作用。
echo -e "\n"我可以在上面的代码行中使用,但是这是重复出现的代码行,如果可能的话我更愿意将其合并为一个
#include <stdio.h>
int main()
{
int (*ptr) [5];
printf ("%lu\n" , sizeof (ptr));
printf ("%lu\n" , sizeof (*ptr));
}
Run Code Online (Sandbox Code Playgroud)
我使用的是64位系统。当我在 gcc 编译器中运行此代码时,它会生成输出8 and 20。我的问题是为此分配多少字节pointer to an array?是 8 或 20,为什么?