我正在尝试机智。我创建了以下(无用的)代码:
char cChar0[30];
char cChar1[30];
char cChar2[30];
char cChar3[30];
char cChar4[30];
clock_t t1;
clock_t t2;
clock_t t3;
uint16_t t4;
uint32_t t5;
uint16_t i;
uint16_t uInitDone;
void dateTime (void){
if(!uInitDone){
uInitDone = 1;
t1 = clock();
for (i = 0; i < 1000; i++){
sprintf(cChar0,"do something...");
}
t2 = clock();
t3 = t2 - t1;
t4 = (uint16_t)t2 - (uint16_t)t1;
t5 = (uint32_t)t2 - (uint32_t)t1;
sprintf(cChar1, "t1: %u; t2: %u;",t1,t2);
sprintf(cChar2, "t1: %u; t2: %u;",(uint16_t)t1,(uint16_t)t2);
sprintf(cChar3, "t1: %u; t:2 %u;",(uint32_t)t1,(uint32_t)t2);
sprintf(cChar4, "t3: %u; t4: %u; t5: %u;",t3,t4,t5);
}
}
int main(void) {
while(1)
dateTime();
return 0;
}
我对结果感到惊讶:
cChar0 = "do something..."
cChar1 = "t1: 0;t2: 0;..."
cChar2 = "t1: 0;t2: 10498;..."
cChar3 = "t1: 0;t2: 0;..."
cChar4 = "t3: 1;t4: 10498; t5: 10498;..."
我检查了time.h并找到了声明:
typedef long time_t;
有人可以向我解释为什么cChar1中的t2和cChar3 = 0? 为什么cChar4中的t3 = 1虽然它似乎是0和0(cChar1)的差异? 为什么cChar4中的t5 = 10498虽然它似乎也是0和0之差(cChar3)?
答案 0 :(得分:1)
如果您不知道数字的确切类型,则必须在使用适当格式调用$.ajax({
type:'GET',
dataType: 'json',
url: "technologies/"+ option+ "/topics"
}).then(function(data)
{
for(i=0;i<data.content.length;i++)
{
var topic = document.createElement("button");
var content= (Object.keys(data.content[i]));
topic.name= content.content.name;
AddTopic.appendChild(topic);
}
});
之前将其显式转换为固定类型。
代码中的错误是您使用格式sprintf()
,这不适用于%u
类型。
以这种方式修复long
行:
sprintf()
现在输出应该更加一致。