新聞中心

#if 0用法和 #if 1 用法

作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏
今天在看程序時(shí),發(fā)現(xiàn)一個(gè)函數(shù)中使用的很多的
# 1
......
#end
# 0
......
#endif
因?yàn)闆](méi)有用過(guò),感到有點(diǎn)莫名。如是上網(wǎng)查找答案。終于明白是用來(lái)注釋編譯內(nèi)容的。意思是說(shuō):
#if 1 需要編譯器編譯以下內(nèi)容;
#if 0 編譯器不要編譯以下內(nèi)容;
#if 0 #endif 這種用法還可以實(shí)現(xiàn)“注釋嵌套?。。?rdquo;
例如:
void Time_Display(u32 TimeVar)
{
#if 1
u32 TY = 0, TM = 1, TD = 0;
s32 Num4Y,NumY, OffSec, Off4Y = 0;
u32 i;
s32 NumDay; //OffDay;
#endif
u32 THH = 0, TMM = 0, TSS = 0;
#if 0
THH = TimeVar/3600;
TMM = (TimeVar % 3600)/60;
TSS = (TimeVar % 3600)% 60;
#endif
#if 1
{
Num4Y = TimeVar/SecsPerFourYear;
OffSec = TimeVar%SecsPerFourYear;
i=1;
while(OffSec > Year_Secs_Accu[i++])
Off4Y++;
NumY = Num4Y*4 + Off4Y;
TY = 2000+NumY;
OffSec = OffSec - Year_Secs_Accu[i-2];
i=0;
if(TY%4)
{ // common year
while(OffSec > Month_Secs_Accu_C[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_C[i-2];
}
else
{ // leap year
while(OffSec > Month_Secs_Accu_L[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_L[i-2];
}
NumDay = OffSec/SecsPerDay;
OffSec = OffSec%SecsPerDay;
TD = NumDay+1;
THH = OffSec/3600;
TMM = (OffSec % 3600)/60;
TSS = (OffSec % 3600)% 60;
}
#endif
printf("Date: %0.4d-%0.2d-%0.2d Time: %0.2d:%0.2d:%0.2dr",TY, TM, TD,THH, TMM, TSS);
}


關(guān)鍵詞: if

評(píng)論


技術(shù)專區(qū)

關(guān)閉