C 这是什么;“继续”;确实如此吗?

C 这是什么;“继续”;确实如此吗?,c,C,我有这部分代码,我不明白。我知道if中的“continue”跳过了该语句。但是为什么呢?它应该打印出一个数组,如“073.45*C”。i=2是数字,对吗?Temp_s[5-i}=result%10+0x30有什么作用?有人能解释一下这个代码吗 void Convert_data_to_temp(unsigned long data) { unsigned long result; unsigned char i=0; result=((33*1000*data)/4096

我有这部分代码,我不明白。我知道if中的“continue”跳过了该语句。但是为什么呢?它应该打印出一个数组,如“
073.45*C
”。
i=2
是数字,对吗?
Temp_s[5-i}=result%10+0x30有什么作用?有人能解释一下这个代码吗

void Convert_data_to_temp(unsigned long data)
{
    unsigned long result;
    unsigned char i=0;
    result=((33*1000*data)/4096)%100000;//rounding off to maximum 5 digits

    Temp_s[0]=0x30;
    Temp_s[3]='.';
    for(i=0;i<6;i++)
    {
        if(i==2){continue;}
        Temp_s[5-i]=(result%10)+0x30;
        result=result/10;
    }
    Temp_s[i]=32;
    Temp_s[i+1]=32;
    Temp_s[i+2]='*';
    Temp_s[i+3]=32;
    Temp_s[i+4]='C';
    Temp_s[i+5]=13;
    Temp_s[i+6]=10;
}
void将数据转换为临时数据(无符号长数据)
{
无符号长结果;
无符号字符i=0;
结果=((33*1000*数据)/4096)%100000;//四舍五入到最大5位
温度[0]=0x30;
温度[3]=';
对于(i=0;i
在数组temps的索引5-i处写入
模10+48的结果

continue跳过循环中的语句,返回到i++语句,然后返回到循环的谓词,但前提是i等于2

总而言之,这将为您提供以前作为字符串表示形式计算的结果


if i==2保留不覆盖
”。

这很奇怪。看起来您正在对for循环进行迭代,但没有执行i==2的代码。continue语句会在执行任何操作之前将您发送到for循环的下一次迭代


这是一种真正受益于位置恰当的注释的代码。

整个代码将5位十进制数(如54321)转换为
“543.21*C\r\n”
-除了不能确保字符串以null结尾外。但是,如果目标数组
Temp\u s
是一个全局变量,并且足够大,并且仅由此函数写入,则可能在结尾处已经有null,但确保它更简单、更安全

赋值
Temp_s[0]=0x30;
可以删除,循环可以写得更清楚,如下所示:

for (i = 0; i < 6; i++)
{
    if (i == 2)
        Temp_s[5-i] = '.';
    else
    {
        Temp_s[5-i] = (result % 10) + '0';
        result /= 10;
    }
}
strcpy(&Temp_s[6], "  * C\r\n");  // Adds null termination
您可以改为编写(注意,
result
是一个
unsigned long
,因此更改了格式转换说明符):

最好能够使用
snprintf()
,但不清楚该全局变量是如何声明的,使用
sizeof(Temp_s)
可能不正确:

snprintf(Temp_s, sizeof(Temp_s), "%.3lu.%.2lu  * C\r\n", result / 100, result % 100);

好吧,这确实是一个很好的混乱。下面是这段代码的作用(假设作用域中有一个至少有13个元素的
Temp\u s
char数组)


continue
continues
对于
循环执行,它将使循环转到下一次迭代。换句话说,只有当i=0、1、3、4或5时,才会运行Temp\u s。当i为2时,它不会运行。读取一些。可能重复的附加提示:
0x30
对于ASCII字符
'0'
,不需要混淆。
继续e> 转到循环的
i++
部分,然后转到条件,而不是直接转到条件。@JonathanLeffler感谢您的帮助。此函数void Convert_data_to_temp(无符号长数据)在LCD上显示温度。因此它需要是此函数的输出。snprintf仅用于打印;我如何使用此函数“”%.3lu.%.2lu*C\r\n“,结果/100,结果%100;”在函数中获取temp_s作为输出。@aliyaratran:在您的问题代码中,
temp_s
是一个全局变量,因此不需要将其作为输出;函数只需修改全局变量,所有人都可以很高兴地回家,除非您有多个线程在工作,或者需要多次调用它,或者运行不正常“全局变量不是一个好主意”。最好更改函数的接口,以将指针传递到
char
数组(及其大小)让函数将数据格式化成字符串。然后它就变成线程安全的、可重用的了——它消除了全局变量的问题。这实际上是C中一个相当常见的习惯用法。启动一个循环,然后里面的第一个语句是“if(…)continue”,意思是“循环所有这些东西,除了这个”@LeeDanielCrocker什么是显而易见的。但是为什么在
i==2
时跳过循环迭代呢?这就是评论的原因——解释为什么要做某些事情。如果在
/*skip中加入评论,就像在我2*/
时插入评论,那么任何有经验的程序员都只能得到“你是显而易见的大师”嘲笑。确切地说,Andrew-comment说代码在做什么是没有用的(经过几轮修改后通常是不真实的)。但你需要知道为什么一些可怜的灵魂感到被迫做这些旋转。你也可以使用:
div\u t d=div(result,100);
然后使用
d.quot
d.rem
。这将导致只执行一次除法运算,这在硬件上可能很昂贵。
int dp = result % 100;
int un = result / 100;
sprintf(Temp_s, "%.3d.%.2d  * C\r\n", un, dp);
sprintf(Temp_s, "%.3lu.%.2lu  * C\r\n", result / 100, result % 100);
snprintf(Temp_s, sizeof(Temp_s), "%.3lu.%.2lu  * C\r\n", result / 100, result % 100);
void Convert_data_to_temp(unsigned long data)
{
    unsigned long result;
    unsigned char i=0;

    // Calculate... something.
    // Apparently the calculation is done in fixed-point decimal,
    // with 3 decimal places (hence `*1000`).
    // Also, the comment is wrong: that's not rounding off, the result will wrap.
    // In any case, we can be sure that 0 <= result < 100000.
    result=((33*1000*data)/4096)%100000;//rounding off to maximum 5 digits

    Temp_s[0]=0x30; // ASCII for '0'
    Temp_s[3]='.';

    // Now Temp_s looks like this (? represents an indeterminate value:
    //
    // 0 ? ? . ? ? ? ? ? ? ? ? ?

    // Now we're filling Temp_s backwards from the 5th index,
    // with the respective digits of `result`. The `continue` skips over
    // index 3 so we don't overwrite the '.' we just put there.
    for(i=0;i<6;i++)
    {
        if(i==2){continue;}
        Temp_s[5-i]=(result%10)+0x30; // Again, 0x30 is just ASCII '0'.
        result=result/10;
    }

    // Let's say that result was 12345. Now Temp_s looks like this:
    //
    // 1 2 3 . 4 5 ? ? ? ? ? ? ?

    // And now we fill the rest of Temp_s with these hard-coded values.
    // Note that we retrieve i at the value it was left by the for, i.e. 6.
    Temp_s[i]=32;   // 32 is an ASCII space
    Temp_s[i+1]=32;
    Temp_s[i+2]='*';
    Temp_s[i+3]=32;
    Temp_s[i+4]='C';
    Temp_s[i+5]=13; // ASCII Carriage return
    Temp_s[i+6]=10; // ASCII Line feed

    // In the end, Temp_s looks like this:
    //
    // 1 2 3 . 4 5 [space] [space] * [space] C \r \n
}
snprintf(
    Temp_s, sizeof Temp_s,
    "%.3lu.%.2lu  * C\r\n",
    result / 100, result % 100
);