Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 格式字符串未使用数据参数-输出长_C_Timer_Arguments_Real Time_Clock - Fatal编程技术网

C 格式字符串未使用数据参数-输出长

C 格式字符串未使用数据参数-输出长,c,timer,arguments,real-time,clock,C,Timer,Arguments,Real Time,Clock,好的,这是一个C程序。。。我得到了这个问题,即使我已经把它放好了。长ms,印刷品上有“l”。现在,x是一个全局int变量,用户通过命令行参数输入,然后是atoi()参数 此问题发生在函数中。我想知道我是否输出错误。在输出这个长ms变量之前,我是否应该在将int x设置为长ms变量时进行类型转换?我很困惑,试图输出毫秒 struct timespec now; clock_gettime(CLOCK_REALTIME, &now); long ms; x = ms; ms

好的,这是一个C程序。。。我得到了这个问题,即使我已经把它放好了。长ms,印刷品上有“l”。现在,x是一个全局int变量,用户通过命令行参数输入,然后是atoi()参数

此问题发生在函数中。我想知道我是否输出错误。在输出这个长ms变量之前,我是否应该在将int x设置为长ms变量时进行类型转换?我很困惑,试图输出毫秒

    struct timespec now;
 clock_gettime(CLOCK_REALTIME, &now);
 long ms;
 x = ms;
 ms = round(now.tv_nsec / 1.0e6);

 fprintf(stdout, "l: flip\n", ms);

我已经用我看到的东西注释了你的代码

struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
long ms;
x = ms;  
// <== this is assigning a variable 'ms' to the variable 'x'
// however, the variable 'ms' is not initialized 
// this will cause the compiler to raise a warning message
// about using an uninitialized variable
// this is (for the above reason) undefined behavior

ms = round(now.tv_nsec / 1.0e6); 
// <-- initialized the variable 'ms'
// but it is too late for the prior statement
// AND the compiler will not be happy about the 'implied' 
// conversion from the returned type from 'round()' 
// which is 'double' to the type 'long int' of the variable 'ms'
// The compiler will be happy if the line is written like this:
ms = (long int)round( now.tv_nsec / 1.0e6 );


fprintf(stdout, "l: flip\n", ms); 
// <-- will cause the compiler to complain, as you already know
struct timespec now;
clock\u gettime(clock\u REALTIME和now);
长ms;
x=ms;

//
fprintf(stdout,“l:flip\n”,ms)看起来不对。您忘记添加格式说明符
%ld
警告:无效转换说明符“:”[-Wformat无效说明符]fprintf(标准输出,“%l:flip\n”,ms);谢谢,但它仍然会出现一个问题,如反转转换说明符。请使用
%ld
而不是
%l
谢谢。是的,嗯,至少现在它给了我一些数字。。。现在我要让它更有效。伟大的因此,这种类型的铸造问题得到了解决。谢谢大家!我可以继续工作以完成我的计划。:)
fprintf(stdout, "%l: flip\n", ms); 
// <-- will also cause the compiler to complain about a invalid format specifier
fprintf(stdout, "%ld: flip\n", ms); 
// <-- now compiler happy