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

📄 cintro.doc

📁 c语言的小编译器,学习的好东东.
💻 DOC
📖 第 1 页 / 共 5 页
字号:
   
            Later on, I will show you how you can use  a  special  operator
         called INDIRECTION to access data items at the  address  contained
         in the pointer.
   
      3.4 A complete 'C' program
   
            With all of the preceeding  information  under  you  belt,  you
         should be able to understand most  of  this  simple  but  complete
         program:
   
           #include stdio.h
   
           /* Main program */
           main()
           {
               int a, b, c;
   
               a = 1;
               b = 2;
               c = add(a, b);
               printf("The result of (1+2)=%d\n", c);
           }
   
           /* Our old familiar "add" function */
           int add(num1, num2)
               int num1, num2;
           {
               return num1+num2;
           }
   
            Did you fully understand it??? ... I thought not!!!
   
            There are a few new things presented in this program,  which  I
         will now explain.
   
            First of all, you should know that the function name "main"  is
         a special name which will be called at the  very  beginning,  when
         the program is first run. It provides a starting  point  for  your
         programmed functions. All 'C' programs have a "main" function.
   Intro to MICRO-C                                                 Page: 17


            You may also be wondering about those "#include"  and  "printf"
         statements. This all comes back to the concept of PORTABILITY, and
         has to do with the programs ability to perform  INPUT  and  OUTPUT
         (I/O). Methods of performing  I/O  may  differ  greatly  from  one
         operating system to another, and hence make it difficult to  write
         "portable" programs. If you don't know  what  portability  is,  go
         back and read the "Background Information" section.
   
            In order to insure that 'C' compilers could be  easily  adapted
         to nearly any operating system,  the  designers  of  the  language
         decided not to  include  ANY  I/O  capabilities  in  the  compiler
         itself. By not implementing it, they didn't have  to  worry  about
         it. All such activity is performed by a set of  functions  in  the
         'C' STANDARD LIBRARY, which is provided for each operating system.
   
            These library functions are used in nearly all programs,  since
         a program which can't read or  write  any  data  doesn't  do  much
         useful work.
   
            Many of the library functions must be  declared  as  a  certain
         type, which may be specific  to  the  compiler  implementation  or
         operating system. (For example  the  "printf"  functions  must  be
         declared as "register" in MICRO-C). The file "stdio.h" is provided
         with  all  "standard  libraries",   and   contains   any   special
         declarations required by the library functions.
   
            The "#include"  statement  causes  the  compiler  to  read  the
         "stdio.h"  file,  and  to  process  the   declaration   statements
         contained within it.
   
            The "printf" statement is actually a call to a STANDARD LIBRARY
         FUNCTION. It is available in almost all 'C'  implementations,  and
         in the above example, displays the prompt "The result of  (1+2)=",
         followed by the decimal value of the passed argument 'c'. For more
         information about "printf" and other library functions,  refer  to
         the MICRO-C Technical manual.
   
            At his point you may wish to enter  the  demonstration  program
         into a file called "DEMO1.C", and  compile  it  with  the  MICRO-C
         compiler. Remember that 'C' IS CASE SENSITIVE, so be sure to enter
         the program EXACTLY as it is shown. Also, make sure that  you  are
         positioned in the MICRO-C directory before you  create  the  file.
         After entering and saving the file with your favorite text editor,
         compile the program using the command:
   
                                   CC DEMO1
   
            You can  run  the  resultant  "DEMO1.COM"  program,  by  simply
         typeing "DEMO1", at the DOS command prompt.
   Intro to MICRO-C                                                 Page: 18


      3.5 'C' memory organization
   
            Now that you have seen a complete 'C'  program,  and  know  the
         basic concepts of functions and variables, you may  want  to  know
         how MICRO-C organizes the computer memory  when  these  constructs
         are used. Knowing this  may  help  you  understand  functions  and
         variables more precicely.
   
            The information in this section is  not  really  necessary  for
         casual use of the language, if you feel  that  such  detail  would
         only confuse you, feel free to skip it until later.
   
            The MICRO-C compiler builds your program to occupy a  block  of
         memory. In the case of small 8 bit computers, this block of memory
         will usually be the entire free ram in the machine. In the case of
         larger machines, it will usually be 64K (65536 bytes), but may  be
         larger or smaller depending on the implementation.
   
            The exact size of the memory block  is  unimportant,  since  it
         affects only the maximum size of a MICRO-C program. The methods of
         memory allocation remain the same.
   
         3.5.1 Static memory
   
               MICRO-C places all of the executable code from the  compiled
            functions at the very  beginning  of  the  memory  block.  This
            includes all  CPU  instructions  which  are  generated  by  the
            compiler. MICRO-C places all initialized  global  variables  in
            this area as well, and also something called the LITERAL POOL.
   
               The "literal pool" consists of all string data which is used
            in statements or initializations in the program. An example  of
            this is the string used in the preceeding demonstration program
            ("Result of (1+2)=%d\n"), which is  a  series  of  data  bytes,
            which are passed to the "printf" function.
   
               This collection of CPU  instructions,  Initialized  variable
            data, and literal pool data is the complete program image which
            must be loaded into memory every time the program is executed.
   
               The next section of memory allocated by  MICRO-C  holds  the
            global variables which have not been  initialized.  Since  they
            have no initial values, they do not have  to  be  loaded  every
            time the program runs. This also means that until  the  program
            stores a value in a particular  memory  location  its  contents
            will be some random value which happened to be at that location
            in memory before the program was loaded.
   
               All of this memory is called "STATIC" memory, because it  is
            reserved for code and data at COMPILE time. Once the program is
            compiled, the above mentioned items are fixed  in  memory,  and
            cannot be moved or changed in size.
   Intro to MICRO-C                                                 Page: 19


         3.5.2 Dynamic memory
   
               When your program begins execution, one of the first  things
            that happems, is that a STACK is set up at the very top of  the
            memory block. This  stack  is  always  referenced  by  a  STACK
            POINTER register which always points to the lowest  address  of
            memory used on the stack. All memory in  the  block  above  the
            stack pointer is deemed to be in use. This is usually  a  built
            in feature of the CPU.
   
               At the beginning of every function,  the  code  produced  by
            MICRO-C contains instructions to reduce the value of the  stack
            pointer by the number of bytes of memory required by the  local
            variables in that function.  When  the  function  "returns"  or
            terminates, the stack pointer is increased by the same amount.
   
               This allows the function to use the memory immediatly  above
            the new stack pointer for its local variables without fear that
            another function will also try to use it. When another function
            is called, it will reserve its memory BELOW the memory  already
            in use by the calling  function,  and  will  return  the  stack
            pointer when it is finished. Thus, all local variables  may  be
            accessed as constant offsets from the stack pointer set  up  at
            the beginning of the function.
   
               ARGUMENTS to a function are passed by  reserving  memory  on
            the stack, and setting it to the argument values, just PRIOR to
            calling the function. When the  called  function  returns,  the
            stack reserved for its arguments is  removed  by  the  function
            performing the call. In this way, the arguments are  just  more
            local variables, and may also be accessed as  constant  offsets
            from the stack pointer.
   
         3.5.3 Heap memory
   
               Some programs need temporary memory which will not disappear
            when the function terminates, or they may not  know  the  exact
            amount of memory they need for a certain operations until  some
            calculations have been performed.
   
               To resolve these problems,  'C'  provides  another  type  of
            dynamic memory which is called "HEAP" memory. To  make  use  of
            this memory, the program uses the "malloc" function  (from  the
            standard library)  which  allocates  a  block  of  memory,  and
            returns a pointer value to its address. The  program  may  then
            access and manipulate this memory through the pointer.
   
               When the program is finished with the memory,  it  may  then
            use the "free" library function to  release  it,  and  make  it
            available for use by other calls to "malloc".
   
               A program may continue allocating  memory  via  "malloc"  as
            long as there is available free memory to allocate. The library
            functions will  keep  track  of  which  blocks  of  memory  are
            allocated, and which blocks are available for allocation.
   Intro to MICRO-C                                                 Page: 20


               A typical memory layout for a 'C' program in the  middle  of
            execution might look something like this:
   
               +----------------------------------------+
               |            CPU Instructions            |
               |         which make up program          |
               |                "code"                  |
               +----------------------------------------+
               |      Initialized GLOBAL variables      |
               +----------------------------------------+
               |           LITERAL POOL data            |
               +----------------------------------------+
               |    Un-initialized GLOBAL variables     |
               +----------------------------------------+
               |      Memory allocated to the heap      |
               +----------------------------------------+
               |          (Heap grows upward)           |
               |                   |                    |
               |  Free memory, available for growth of  |
               |           the heap and stack.          |
               |                   |                    |
               |         (Stack grows downward)         |
               +----------------------------------------+
               |  Local variables of innermost function |
               +----------------------------------------+
               |  Return address of innermost function  |
               +----------------------------------------+
               |     Arguments of innermost function    |
               +----------------------------------------+
               |  Local variables of middle function    |
               +----------------------------------------+
               |   Return address of middle function    |
               +----------------------------------------+
               |      Arguments of middle function      |
               +----------------------------------------+
               |    Local variables of main function    |
               +----------------------------------------+
               |    Return address of main function     |
               +----------------------------------------+
   
               For those  not  familiar  with  computer  architecture,  the
            RETURN ADDRESS is placed on the stack by  a  CALL  INSTRUCTION,
            and  is  the  memory   address   immediately   following   that
            instruction. When a RETURN INSTRUCTION is later  executed,  the
            CPU removes the return address from the stack, and places it in
            the PROGRAM COUNTER, thus causing program execution  to  resume
            with the instruction immediately following  the  original  call
            instruction.
   Intro to MICRO-C                                                 Page: 21


   4. EXPRESSIONS
   
         An expression in 'C' consists of one or more values, and OPERATORS
      which cause those values to be modified in  a  calculation.  Anywhere

⌨️ 快捷键说明

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