⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language-full.html

📁 palm的pocketc
💻 HTML
📖 第 1 页 / 共 3 页
字号:
   }
}</pre>

<p>In this clearly contrived example, the output is &quot;5421&quot;. When <i>x</i>
reaches 3, the <code>continue</code> is executed, passing execution to the beginning of
the loop, skipping over the <i>puts</i>.</p>

<p><b>switch, case, default</b> </p>

<pre>which_number(int x) {
   switch (x) {
      case 1:
         puts(&quot;x == 1\n&quot;);
         break;
      case 2:
      case 3:
         puts(&quot;x == 2 or x == 3\n&quot;);
         break;
      case 8:
         puts(&quot;x == 8\n&quot;);
      case 10:
         puts(&quot;x == 8 or x == 10\n&quot;);
         break;
      default:
         puts(&quot;x is not 1,2,3,8, or 10\n&quot;);
   }
}</pre>

<p>The <i>which_number</i> function is passed a value, and will print out a fact
or two about it. If the value is 1, <code>case 1</code> is executed and <code>break</code>s to the end of
the <code>switch</code>. If the value is 2 or 3, the code following <code>case 3:</code> is
executed. If the value is 8, both &quot;x=8&quot; and &quot;x=8 or x=10&quot; is
printed because there is no <code>break</code> before <code>case 10:</code>, this is called fall-through.
If none of the cases match the passed in value, the code following <code>default:</code> is
executed.</p>

<h3>Pointers</h3>

<p><strong>Note:</strong> Pointers are an advanced topic, which should be dealt with after
the user is familiar with all the other programming concepts.</p>

<p>All variables are stored at some address in memory. A pointer is a variable which
refers to another variable by containing that variable's address.</p>

<p>There are two primary operators which are used with pointers, * and &amp;. The *
operator dereferences the pointer. A dereferenced pointer acts just like the data to which
it points. The &amp; operator returns the address of a given variable. To illustrate: </p>

<pre>pointer p, q;
int i;

main() {
  i = 5;
  p = &amp;i;   // Assign the address of 'i' to the pointer 'p'
            // now, typing '*p' is the same as typing 'i'
  puts(*p); // Print the value of 'i'
  *p = 7;   // Assign 7 to 'i'
  q = p;    // Assign the value of 'p', which is the address of 'i', to 'q'
            // now, typing '*q' is the also the same as typing 'i'

  // Things not to do
  p = 8;    // BAD! Don't assign a constant value to a pointer
  *i = 9;   // BAD! Don't try to dereference a non-pointer
}</pre>

<p>A pointer can also be used to take the address of a function (but NOT a built in
function!). Unlike variables, however, the &amp; operator is <strong>NOT</strong> used.
Calling a function through a pointer is a little tricky. First, the code looks ugly.
Second, <strong>no error checking can be done on the parameters</strong>, so type
conversions are not done and the number of arguments is not confirmed. For example: </p>

<pre>
func(int x) { return 5*x; }

main() {
  int result;
  pointer ptr;

  ptr = func; // Take the address of a function
  result = (*ptr)(5); // call the function (ugly)

  // Things not to do
  result = (*ptr)(&quot;5&quot;); // this won't work, since the string
                        // isn't converted to an integer
  result = (*ptr)(5,7); // this will compile, but will result
                        // in stack corruption because the
                        // wrong number of arguments are used
}</pre>
<b>

<p>Pointers and arrays</b><br>
Pointers and arrays are fairly similar. Pointers can use the [] operator, and an array
variable (when not used with []) results in the address of the first element. For example:
</p>

<pre>int array[5];
pointer p;

main() {
  p = array; // Assign the address of the first element of
             // 'array' to 'p'
  *p = 7;    // Assign 7 to array[0]
  p[1] = 8;  // Assign 8 to array[1]
}</pre>

<p>This enables the pointers to arrays to be passed as function parameters. This also
allows the user to implement their own version of two-dimensional arrays. By creating an
array of pointers, each of which is a pointer to an array (or part of one), a
two-dimensional array can be simulated. </p>

<pre>int array[100];
pointer twod[10]; // after init(), this can be treated
                  // like at 10x10 matrix

init() {
  int i;
  for (i=0;i&lt;10;i++)
    twod[i]=array + i*10; // Pointer arithmetic
}

main() {
  int x, y;
  init();
  for (x=0;x&lt;10;x++)
    for (y=0;y&lt;10;y++)
      twod[x][y]=x * y; // Sets array[x*10 + y] = x*y
}</pre>
<b>

<p>Pointer arithmetic</b><br>
Pointer values can used in a limited number of expression. You can add and subtract from a
pointer (and, thus, can use the increment and decrement operators as well). When you add 1
to a pointer, the pointer points to the next value in memory. Similarly, when you subtract
1 from a pointer, the pointer points to the previous value in memory. Caution should be
used when using pointer arithmetic, because dereferencing an invalid memory location will
cause an error in the applet. </p>

<h3>Include</h3>

<p>Using the <code>include</code> keyword, it becomes possible to write programs whose
source code is longer than the current 4K Memo Pad limit, or to create memos of frequently
used code. The contents of the included file are functionally inserted into the line
containing the <code>include</code> keyword.<br>
<b>Notes:</b> An included memo can begin with '/$' instead of '//' to hide it from the
compile form. You may also include a doc file. However, PocketC searches for a
matching memo before searching for a matching doc file. On the Desktop Edition,
any slashes in a path must be escaped<br>
<b>Example</b> </p>

<pre>/$ MyFunctions
times5(int x) {
   return x*5;
}</pre>

<p>Another memo: </p>

<pre>// My Program
include &quot;MyFunctions&quot;

main() {
   int y;
   y = times5(7);
   puts(y); // Prints 35
}</pre>

<p>The compiler sees this as: </p>

<pre>// My Program
times5(int x) {
   return x*5;
}
main() {
   int y;
   y = times5(7);
   puts(y); // Prints 35
}</pre>

<b>Note: </b><code>include</code> can only be used at the top level (i.e. you cannot use <code>include</code> within a function)

<h3>Using Native Libraries</h3>

<p>To use the functions defined by a native library, you must first tell the compiler to
load the library by using the <code>library</code> keyword. The functions are then used as
if they were normal functions. (DO NOT use this keyword with MathLib. MathLib functions
are available as built-in functions.)</p>

<p>Example:</p>

<pre>// My Applet
// PocketCLib defines times5(int)
library &quot;PocketCLib&quot;

main() {
   int x;
   x = times5(7);
}</pre>

<p>For information of creating native libraries, see <a href="native.html">native.htm</a>l.
</p>

<h3>Special characters</h3>

<p>There are two ways to add special characters to a string. The first is by appending
them by number, such as: </p>

<pre>str = &quot;Here is a neat little square: &quot; + (char)149;</pre>

<p>The other method is through using escape sequences. The following escape sequences are
supported: </p>

<table border="1">
  <tr>
    <th>Escape sequence</th>
    <th width="40">\\</th>
    <th width="40">\'</th>
    <th width="40">\&quot;</th>
    <th width="40">\n</th>
    <th width="40">\t</th>
    <th>\x</th>
  </tr>
  <tr>
    <th>Interpretation</th>
    <td align="center">\</td>
    <td align="center">'</td>
    <td align="center">&quot;</td>
    <td align="center">newline</td>
    <td align="center">tab</td>
    <td>character specified by the following two hex digits. Example: '\x95' is the block
    character (decimal 149)</td>
  </tr>
</table>

<p>So, to create a string that contains a quote: </p>

<pre>str = &quot;She said \&quot;I'm sorry,\&quot; but it was too late...&quot;;
puts(str); // Prints: She said &quot;I'm sorry,&quot; but it was too late...</pre>

<h3>Preprocessor</h3>

<p>Just as in C, PocketC contains a preprocessor, which allow a programmer to define
macros and conditionally compile a section of code based on the presence of a given macro.</p>

<p><strong>#define <em>macro macro_data</em></strong></p>

<p>A macro is an identifier which, when read by the compiler, is replaced by the macro's
data. A macro's data can be any number of tokens (including zero). The macro data is
terminated by the next newline. For example: </p>

<pre>#define calc 5 * (x + 7)

main() {
  int x, y;
  x = 9;
  y = calc;
  puts(&quot;y = &quot; + y);
}</pre>

<p>The compiler sees this as: </p>

<pre>main() {
  int x, y;
  x = 9;
  y = 5 * (x + 7);
  puts(&quot;y = &quot; + y);
}</pre>
<b>

<p>#ifdef <i>macro</i></b></p>

<p>If the macro has been previously defined (even if it is defined to be nothing), the
section of code between it and the matching <b>#endif</b> is compiled. Otherwise, the
compiler ignores the section of code.</p>

<p><b>#ifndef <i>macro</i></b></p>

<p>If the macro has NOT been previously defined, the section of code between it and the
matching <b>#endif</b> is compiled. Otherwise, the compiler ignores the section of code.</p>

<p><b>#endif</b></p>

<p>Marks the end of a section of code preceeded by an <b>#ifdef</b> or <b>#ifndef</b>.</p>

<p><b>#else</b></p>

<p>Placed in the middle of a <b>#ifdef</b> or <b>#ifndef</b> block, the code between the <b>#else</b>
and the <b>#endif</b> is compiled if and only if the previous block was not.</p>

<p><b>#undef</b></p>

<p>Undefines a previously defined macro. If the macro was never defined, this
does nothing.</p>

<p>Example:</p>

<pre>#define DEBUG

main() {
#ifdef DEBUG
  puts(&quot;In debugging mode&quot;);
#else
  puts(&quot;In normal mode&quot;);
#endif
}</pre>

<p>The compiler predefines these macros:
<pre>#define __PKTC__ 1
#define __PKTC_PALM__ 1
#define __PKTC_PRC__ 1 // only if PocketC Desktop Edition is building a .prc file
</pre>

<hr>

<p>That should just about cover it. Next, look over the <a href="Functions.html">Built-in
Functions</a> to see which may be useful for you.</p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -