![](/img/trans.png)
[英]MPLAB X, XC8, and C in general. Using variables across functions and source files
[英]MPLab / C prg / Variables
我已经安装了MPLab V8.43,并且刚刚安装了用于编程的C18编译器。 每当我尝试构建小的测试代码时,它都会在第一个变量声明时停止。 它说有一种语法。
unsigned char counter;
在我看来并没有错...即使是作为未签名的char计数器[1]也是如此; 而且它仍然向我抛出语法错误。 是什么赋予了? 有任何想法吗?
必须在块的顶部(在这种情况下为函数)的顶部声明局部变量。这符合C89标准。
这些是可以接受的:
void functionname(void)
{
unsigned char counter;
/* rest of code */
}
void functionname(void)
{
/* code */
for (unsigned char counter = 0; counter<30; counter++)
{
}
}
这是不可接受的:
void functionname(void)
{
/* code */
unsigned char counter = 0;
/* more code */
}
由于您具有带有char数据类型的计数器变量。 但是它不是数组或字符串。
so you can't access it by counter[1].
您可以在main中定义局部变量,但是应该对其进行定义,以使它们不会跟随变量赋值块或代码执行块。
这是MPLAB C18中的有效变量声明/定义:
void main ()
{
/* Declare or Define all Local variables */
unsigned char counter;
unsigned char count = 5;
/* Assignment Block or the code Execution Block starts */
conter++;
count++;
}
但是,这是无效的,并且会导致“语法错误”:
void main ()
{
/* Declare or Define all Local variables */
unsigned char count = 5;
/* Assignment Block or the code Execution Block starts */
count++;
/* What??? Another variable Declaration / Definition block */
unsigned char counter; /* Hmmm! Error: syntax error */
}
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.