Docstoc

Lecture 1-4-05

Document Sample
Lecture 1-4-05 Powered By Docstoc
					    Chapter 11-12 , Appendix D

           C Programs

•   Higher Level languages


•   Compilers


•   C programming


•   Converting C to Machine Code

•   C Compiler for LC-3
         Higher Level Languages


High Level Languages give us:

•   Symbolic Names
•   Expressions
•   Libraries of functions/subroutines
•   Abstraction of underlying hardware
•   Readability
•   Structure – help keep nasty bugs out
          Translating Higher Level Program




•   Interpreters
    Translated to machine code as the program runs




•   Compilers
    Translated into a load module before program runs
Compiling C
                      Simple Example C Program
/*
 *    Program Name : countdown, our first C program
 *
 *    Description : This program prompts the user to type in
 *    a positive number and counts down from that number to 0,
 *    displaying each number along the way.
 */
/* The next two lines are preprocessor directives */
#include <stdio.h>
#define STOP 0
/* Function    : main                                     */
/* Description : prompt for input, then display countdown */
int main()
{
  /* Variable declarations */
    int counter;         /* Holds intermediate count values */
    int startPoint;      /* Starting point for count down   */
    /* Prompt the user for input */
    printf("===== Countdown Program =====\n");
    printf("Enter a positive integer: ");
    scanf("%d", &startPoint);
    /* Count down from the input number to 0 */
    for (counter = startPoint; counter >= STOP; counter--)
    {
      printf("%d\n", counter);
    }
    return 0
}
                               Terms, Etc.

•   Pre processor directives
       #define
       #include

•   Header Files
       <stdio.h>

•   Data Types
       int
       char
       double

•   Scope
       Local
       Global


•   Variable Initiation
       Local – not initialized
       Global – initialized to 0
                               Scope



#include <stdio.h>
int globalVar = 2;                /* This variable is global */
int main()
{
   int localVar = 3;        /* This variable is local to main */
    printf("Global %d Local %d\n", globalVar, localVar);
    {
        int localVar = 4;           /* Local to this sub-block */
        printf("Global %d Local %d\n", globalVar, localVar);
    }
    printf("Global %d Local %d\n", globalVar, localVar);
    return 0
}
                        Pointers - IMPORTANT
• A pointer is a variable which contains the address in memory of another variable.
   We can have a pointer to any variable type.
• The unary or monadic operator & gives the ``address of a variable''.
• The indirection or dereference operator * gives the ``contents of an object pointed
 to by a pointer variable''.
• To declare a pointer to a variable:    int *pointer;




   Note: ip = ip + 1 actually increments ip by 4.        Why?

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:7/19/2013
language:English
pages:8