奇怪的执行行为 [英] Weird Execuation Behaviour

查看:103
本文介绍了奇怪的执行行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我正在尝试一段简短的代码。 令我惊讶的是,非常简单的printf实现有一个错误。 它是这样的:

In the last couple of days I am trying a short and simple piece of code.  To my surprise, there is an error with the very simple printf implementation.  It like this:

目标是打印起点和当前时间之间的时差,以及小时,分钟当前时间的第二个信息。

The goal is to print out the time difference between a start point and current time, as well as the hour, minute and second information of current time.

我写的代码如下:

  ;   time_t timeNow;

     struct tm tmNow;

     time(& timeNow);

     memcpy(& tmNow,localtime(& timeNow),sizeof(tmNow));

     char buffer [100];



    sprintf(buffer,"%d \ t%。2d:%d:%d \ n",timeNow - timeStart,tmNow.tm_hour,tmNow.tm_min,tmNow.tm_sec);

    time_t timeNow;
    struct tm tmNow;
    time(&timeNow);
    memcpy(&tmNow, localtime(&timeNow),sizeof(tmNow));
    char buffer[100];

    sprintf(buffer, "%d\t%.2d:%d:%d\n", timeNow - timeStart,tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);

    fputs(缓冲区,fpOutput);

    fputs(buffer, fpOutput);

真正的打印输出是这样的:

The real print out was something like this:

2     00:11:50

4     00:11:50

6     00:11:50

2    00:11:50
4    00:11:50
6    00:11:50

请注意第二列,即"00"和"00"。来自哪儿。  11是我电脑的当前小时,50是当前分钟,第二小时是丢失。

Note that the second column, those "00" are coming from nowhere.  11 is current hour of my PC and 50 is the current minute, the second is lost.

我确实尝试将timeNow - timeStart更改为(timeNow - timeStart),希望括号会有所帮助,但事实并非如此。 然后我将该行更改为:

I did try to change timeNow - timeStart to (timeNow - timeStart), hoping the brackets will help, but it did not.  Then I changed the line to:

    sprintf(buffer,"%d \t%.2d:%d:%d \ t%d \ n",timeNow - timeStart,tmNow.tm_hour,tmNow.tm_min,tmNow.tm_sec);

    sprintf(buffer, "%d\t%.2d:%d:%d\t%d\n", timeNow - timeStart,tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);

这确实很奇怪,结果是:

It did work strangely, and the result is:

2     00:11:55     17

4     00:11:55     19

2    00:11:55    17
4    00:11:55    19

现在第二个就在那里。 有趣的是只有四个变量但只有五个%d。

Now the second is there.  What's interesting is there are only four variables but five %d.

最后我试着用idifftime引入另一个变量来替换timeNow - timeStart,因此该行变为:

finally I tried to introduce an other variable in as idifftime to replace timeNow - timeStart, thus the line became:

    int idifftime = timeNow - timeStart;

    int idifftime = timeNow - timeStart;

    sprintf(buffer,"%d \ t%。2d:%d:%d \ n",idifftime,tmNow.tm_hour,tmNow.tm_min,tmNow.tm_sec);

    sprintf(buffer, "%d\t%.2d:%d:%d\n", idifftime,tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);

这很有效。

这似乎是个错误。 所有更改之前的相同代码在GCC中都运行良好。

This seems to be a bug.  The same code before all the changes worked very well in GCC.

谢谢。

推荐答案

处理感知错误的最有效方法是共享实际代码。 您可以使用"插入代码块按钮"然后将实际代码粘贴到左侧打开的窗格中,将代码剪切并过去到论坛帖子中。

The most efficient way to deal with a perceived bug is to share the actual code.  You can cut and past code into a forum post by using the "Insert Code Block Button and then pasting actual code into the pane that opens on the left side-


这篇关于奇怪的执行行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆