用户工具

站点工具


en:reference:language:define
no way to compare when less than two revisions

差别

这里会显示出您选择的修订版和当前版本之间的差别。


前一修订版
en:reference:language:define [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
 +====== Define ======
 +
 +
 +#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in ocrobot don't take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.
 +
 +
 +This can have some unwanted side effects though, if for example, a constant name that had been #defined is included in some other constant or variable name. In that case the text would be replaced by the #defined number (or text).
 +
 +
 +In general, the const keyword is preferred for defining constants and should be used instead of #define.
 +
 +
 +Ocrobot defines have the same syntax as C defines:
 +===== Syntax =====
 +
 +#define constantName value
 +
 +
 +Note that the # is necessary.
 +===== Example =====
 +<code cpp>
 +#define ledPin 3
 +// The compiler will replace any mention of ledPin with the value 3 at compile time.
 +</code>
 +===== Tip =====
 +
 +There is no semicolon after the #define statement. If you include one, the compiler will throw cryptic errors further down the page.
 +<code cpp>
 +#define ledPin 3;    // this is an error 
 +</code>
 +
 +Similarly, including an equal sign after the #define statement will also generate a cryptic compiler error further down the page.
 +<code cpp>
 +#define ledPin  = 3  // this is also an error 
 +</code>
  
en/reference/language/define.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1