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

📄 cintro.doc

📁 一个简单的编译器
💻 DOC
📖 第 1 页 / 共 5 页
字号:
   3. INTRODUCTION TO 'C'
   
         'C' is a "high level" computer language,  which  has  become  very
      popular in recent years. It has proven suitable for a  large  variety
      of programming tasks, and unlike most other high level languages,  is
      quite good for "low  level"  and  "system"  type  functions.  A  good
      example of this capability is the popular  "UNIX"  operating  system,
      which is  written  almost  entirely  in  'C'.  Before  UNIX,  it  was
      generally thought that the only language efficent enough for  writing
      an operating system was assembly language.
   
         Programs in 'C' are divided into two main parts, VARIABLES,  which
      are blocks of memory reserved for storing data, and FUNCTIONS,  which
      are blocks of memory containing executable CPU instructions. They are
      created using a DECLARATION STATEMENT, which is basically  a  command
      to the compiler telling it what type of variable or function you wish
      to create, and what values or instructions to place in it.
   
         There are several 'C' KEYWORDS which serve to inform the  compiler
      of the size and type of a variable or function. This  information  is
      used by the compiler to determine how to interpret the  value  STORED
      in a VARIABLE, or RETURNED by a FUNCTION.
   
       Size:   int         - 16 bit value (default)
               char        - 8 bit value
       type:   unsigned    - Positive only (0-2**bits)
                           + Default is signed (positive & negative)
   
       Examples:   int a;              /* 16 bit signed variable */
                   char b;             /* 8 bit signed variable */
                   unsigned int c;     /* 16 bit unsigned variable */
                   unsigned d;         /* Also 16 bit unsigned variable */
                   unsigned char e;    /* 8 bit unsigned variable */
   
         Normally, when you define a function or global variable, its  name
      is made accessable to all object modules which will  be  linked  with
      the program. You may access a  name  which  is  declared  in  another
      module by  declaring  it  in  this  module  with  with  the  "extern"
      modifier:
   
          extern int a;       /* external variable of type int    */
          extern char b();    /* external function returning char */
   
         If you want to make sure that a function or global  variable  that
      you are declaring is not accessable to  another  module  (To  prevent
      conflicts with names in other modules etc), you can declare  it  with
      the "static" modifier. This causes the name to be accessable only  by
      functions within the module containing the "static" declaration:
   
                                static int a;
   Intro to MICRO-C                                                 Page: 12


      3.1 Functions
   
            FUNCTIONS in 'C' are  collections  of  C  language  STATEMENTS,
         which are grouped together under a name. Each statement represents
         an operation which is to be performed by the CPU. For example, the
         statement:
   
                                  A = A + 1;
   
         directs the CPU to read the variable called 'A', add a value of  1
         to it, and to store the result back into the variable  'A'  (we'll
         discuss variables in the mext section). Note the  SEMICOLON  (';')
         at the end  of  the  statement.  The  'C'  compiler  uses  ';'  to
         determine when the statement ends. It does not care about lines or
         spaces. For example, the above statement could also be written:
   
                                      A
                                      =
                                      A
                                      +
                                      1
                                      ;
   
         and would still compile without error. Thus, you can have  a  VERY
         long statement in 'C', which spans several lines. You must  always
         however, be very careful to include the terminating ';'.
   
            Each function within a 'C' program can be "called" by using its
         name in any statement, may accept "argument" values which  can  be
         accessed by the statements contained within it, and may  return  a
         value back to the caller. This allows functions in 'C' to be  used
         as "building blocks", providing extensions to the language,  which
         may be used from any other function.
   
            Below is a sample 'C' function,  which  performs  an  operation
         (addition) on two argument values. The text between '/*' and  '*/'
         is COMMENTS, and is ignored by the compiler.
   
           /* Sample 'C' function to add two numbers */
           int add(num1, num2)     /* Declaration for function */
               int num1, num2;     /* Declaration for arguments */
           {
               return num1+num2;   /* Send back sum of num1 and num2 */
           }
   
            The names located within the  round  brackets  '()'  after  the
         function name "add" tells the compiler what names you want to  use
         to refer to the ARGUMENT VALUES. The "return" statement  tell  the
         compiler that you want to terminate the execution of the function,
         and return the value of  the  following  expression  back  to  the
         caller. (We'll discuss "return" in more detail later).
   Intro to MICRO-C                                                 Page: 13


            Now that you have defined the function "add", you could use  it
         in any other statement, in any function in your program, simply by
         calling it with its name and argument values:
   
                                a = add(1, 2);
   
            The above statement would call "add", and pass  it  the  values
         '1' and '2' as arguments. "add" evaluates 1  +  2  to  be  3,  and
         returns that value back, which is then stored in the variable 'a'.
   
            Note that 'C' uses the  round  brackets  following  a  name  to
         determine that you wish to call the function, therefore, even if a
         function has no argument values, you must include '()':
   
                               a = function();
   
            Also note, that if a function does not return a value,  or  you
         do not want to  use  the  returned  value,  you  simply  code  the
         function name by itself:
   
                                 function();
   
      3.2 Variables
   
            VARIABLES in 'C' are reserved areas of memory  where  the  data
         manipulated by the program is stored. Each variable is assigned  a
         name by which it  is  referenced  by  other  'C'  statements.  ALL
         VARIABLES IN 'C' MUST BE DECLARED.
   
            Variables in 'C' may be single values as shown eariler, or they
         may be declared as an ARRAY, which reserves  memory  space  for  a
         number of data elements of the variables type:
   
                                int array[4];
   
            The above statement reserves memory  for  four  16  bit  signed
         values, under the name "array". It is important to know  that  'C'
         considers the elements of an array to be numbered from  zero  (0),
         so the four locations in the above array are referenced by using:
   
                                   array[0]
                                   array[1]
                                   array[2]
                                   array[3]
   
            There are two basic types  of  varaibles  in  'C',  GLOBAL  and
         LOCAL.
   Intro to MICRO-C                                                 Page: 14


         3.2.1 GLOBAL Variables
   
               GLOBAL variables are set up permanently in memory, and exist
            for the duration of the entire programs execution. The names of
            global variables may be referenced by  any  statement,  in  any
            function, at any time. Global variables are declared in 'C'  by
            placing the declaration statement OUTSIDE of any function.  For
            example:
   
               int a;      /* Declare GLOBAL variable */
   
               inita()     /* Function to initialize 'a' with 1 */
               {
                   a = 1;
               }
   
               Note that the declaration statement for 'a' is NOT contained
            within the definition of "inita".
   
               Since global variables are permanent blocks of memory, it is
            possible to INITIALIZE them in the declaration statement.  This
            causes the variable to be assigned a  value  at  COMPILE  time,
            which will be loaded into memory at  the  same  time  that  the
            program is loaded to be executed. This means that your  program
            will not have to explicitly store a value in a.
   
                                  int a = 1;
   
               Array variables may also be initialized in  the  declaration
            statement, by using the  curly  brackets  '{}'  to  inform  the
            compiler that you have multiple values:
   
                          int a[4] = { 1, 2, 3, 4 };
   
               In MICRO-C, the initial values for an array are expressed as
            a single string of values  REGUARDLESS  of  the  shape  of  the
            array:
   
                        int a[2][2] = { 1, 2, 3, 4 };
   
               If an array has only one dimension (set of  '[]'s),  you  do
            not have to specify the  size  of  initialized  variables.  The
            compiler will automatically set  the  size  to  the  number  of
            initial values given:
   
                        int array[] = { 1, 2, 3, 4 };
   Intro to MICRO-C                                                 Page: 15


         3.2.2 LOCAL Variables
   
               Variables  which  are  declared  WITHIN   a   function   are
            determined by the compiler to be  LOCAL.  The  memory  used  by
            these variables is automatically  reserved  when  the  function
            begins to execute, and is released when it terminates. Names of
            LOCAL variables are only accessable from  within  the  function
            where they are defined:
   
               inita()     /* Function to initialize 'a' with 1 */
               {
                   int a;  /* Declare LOCAL variable */
   
                   a = 1;
               }
   
               The  above  function  shows  the  declaration  of  a   local
            variable, but is not very useful since the local variable  will
            cease to exist when the function returns. Local  variables  are
            used as temporary locations  for  holding  intermediate  values
            created during a functions execution, which are not required by
            any other part of the program.
   
               Each function may have its own local  variables,  but  since
            memory is  only  used  by  the  functions  which  are  actually
            executing, the total amount of memory reserved is usually  less
            that the total size of all local variables in the program.
   
               Since local variables are allocated and released during  the
            execution of your program, it is  not  possible  to  initialize
            them at compile time, and therefore MICRO-C does not allow them
            to be initialized in the declaration. Some compilers  do  allow
            this, however,  the  code  generated  is  equivalent  to  using
            assignment statements to initialize them at  the  beginning  of
            the function.
   
               The ARGUMENTS to a function  (See  Functions)  are  actually
            local variables for that function which are  created  when  the
            function is called. For this reason,  the  argument  names  are
            also un-available outside of the function  in  which  they  are
            defined.
   Intro to MICRO-C                                                 Page: 16


      3.3 Pointers
   
            A POINTER in 'C' is a memory address,  which  can  be  used  to
         access a data item in memory. All pointers in MICRO-C are  16  bit
         values, which allows access to a maximum of 65536 bytes of  memory
         through it.
   
            Any variable or function  may  be  declared  as  a  pointer  by
         preceeding its name with the '*' character in the declaration:
   
           int *a;                 /* a = 16 bit pointer to int value */
           char *b;                /* b = 16 bit pointer to char */
           extern char *fgets();   /* Returns 16 bit pointer to char */

⌨️ 快捷键说明

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