gavin3389 发表于 2010-10-16 08:59:43

变量定义的一个小问题

本帖最后由 gavin3389 于 2010-10-16 09:00 编辑

在结合LED跑马灯实验室,想只是让LED做一次顺序点亮就可以了,while(1)循环中其他代码没有注释掉,代码如下:
        while(1)
        {   
               
                Led_Display(1);
                Delay(1000);
                Led_Display(2);
                Delay(1000);
                Led_Display(4);
                Delay(1000);
                Led_Display(8);
                Delay(1000);
                U8 idx;
                Uart_Printf("\nPlease select function : \n");       
                for(i=0; CmdTip.fun!=0; i++)
                        Uart_Printf("%d : %s\n", i, CmdTip.tip);
                idx = Uart_GetIntNum_GJ() ;       
                if(idx<i)
                {
                        (*CmdTip.fun)();
                        Delay(20);
                        Uart_Init( 0,115200 );
                }
       
        }       
但是这样添加的话,编译的时候会报错

但是,如果把U8 idx;放到while开始定义,程序如下:
while(1)
        {   
                U8 idx;
                Led_Display(1);
                Delay(1000);
                Led_Display(2);
                Delay(1000);
                Led_Display(4);
                Delay(1000);
                Led_Display(8);
                Delay(1000);
               
                Uart_Printf("\nPlease select function : \n");       
                for(i=0; CmdTip.fun!=0; i++)
                        Uart_Printf("%d : %s\n", i, CmdTip.tip);
                idx = Uart_GetIntNum_GJ() ;       
                if(idx<i)
                {
                        (*CmdTip.fun)();
                        Delay(20);
                        Uart_Init( 0,115200 );
                }
       
        }
程序就能编译通过。
提问:对变量的定义规则有何要求,是不是一定要在循环开始的时候定义呢?以前一直没有注意过这个问题~谢谢回答,其实感觉这个问题有点弱,但真的想弄清楚,百度也没有找到合适的解释。

gavin3389 发表于 2010-10-16 09:13:08

哇咔咔,后面的字体咋变了?
页: [1]
查看完整版本: 变量定义的一个小问题