Unformatted text preview:

Introduction to “C” HLL’s and the Basic SyntaxC: A High-Level LanguageCompilation vs. InterpretationSlide 4Compiling a C ProgramCompilerA Simple C ProgramPreprocessor DirectivesCommentsmain FunctionVariable DeclarationsInput and OutputMore About OutputExamplesExamples of InputCompiling and LinkingBasic C ElementsData TypesVariable NamesSlide 20LiteralsScope: Global and LocalExampleOperatorsExpressionStatementSlide 27Assignment OperatorSlide 29Arithmetic OperatorsArithmetic ExpressionsBitwise OperatorsLogical OperatorsRelational OperatorsSpecial Operators: ++ and --Using ++ and --Practice with PrecedenceSymbol TableLocal Variable StorageAllocating Space for VariablesVariables and Memory LocationsExample: Compiling to LC-3Example: Symbol TableExample: Code GenerationExample (continued)Slide 46Special Operators: +=, *=, etc.Special Operator: ConditionalQuestions?Slide 501Introduction to “C”Introduction to “C”HLL’s and the Basic HLL’s and the Basic Syntax Syntax Patt and Patel Ch. 11 & 12Patt and Patel Ch. 11 & 122C: A High-Level LanguageC: A High-Level LanguageGives symbolic names to valuesGives symbolic names to values–don’t need to know which register or memory locationProvides abstraction of underlying hardwareProvides abstraction of underlying hardware–operations do not depend on instruction set–example: can write “a = b * c”, even thoughLC-3 doesn’t have a multiply instructionProvides expressivenessProvides expressiveness–use meaningful symbols that convey meaning–simple expressions for common control patterns (if-then-else)Enhances code readabilityEnhances code readabilitySafeguards against bugsSafeguards against bugs–can enforce rules or conditions at compile-time or run-time3Compilation vs. InterpretationCompilation vs. InterpretationDifferent ways of translating high-level languageDifferent ways of translating high-level languageInterpretationInterpretation–interpreter = program that executes program statements–generally one line/command at a time–limited processing–easy to debug, make changes, view intermediate results–languages: BASIC, LISP, Perl, Java, Matlab, C-shellCompilationCompilation–translates statements into machine language•does not execute, but creates executable program–performs optimization over multiple statements–change requires recompilation•can be harder to debug, since executed code may be different–languages: C, C++, Fortran, Pascal4Consider the following algorithm:Consider the following algorithm:Get W from the keyboard.X = W + WY = X + XZ = Y + YPrint Z to screen.•If If interpretinginterpreting, how many arithmetic operations occur?, how many arithmetic operations occur?•If If compilingcompiling, we can analyze the entire program and possibly , we can analyze the entire program and possibly reduce the number of operations. Can we simplify the above reduce the number of operations. Can we simplify the above algorithm to use a single arithmetic operation?algorithm to use a single arithmetic operation?Compilation vs. Interpretation5Compiling a C Compiling a C ProgramProgramEntire mechanism is usually called Entire mechanism is usually called the “compiler”the “compiler”PreprocessorPreprocessor–macro substitution–conditional compilation–“source-level” transformations•output is still CCompilerCompiler–generates object file•machine instructionsLinkerLinker–combine object files(including libraries)into executable imageCSource andHeader FilesC PreprocessorCompilerSource CodeAnalysisTarget CodeSynthesisSymbol TableLinkerExecutableImageLibraryObject Files6CompilerCompilerSource Code AnalysisSource Code Analysis–“front end”–parses programs to identify its pieces•variables, expressions, statements, functions, etc.–depends on language (not on target machine)Code GenerationCode Generation–“back end”–generates machine code from analyzed source–may optimize machine code to make it run more efficiently–very dependent on target machineSymbol TableSymbol Table–map between symbolic names and items–like assembler, but more kinds of information7A Simple C ProgramA Simple C Program#include <stdio.h>#include <stdio.h>#define STOP 0#define STOP 0/* Function: main *//* Function: main *//* Description: counts down from user input to STOP *//* Description: counts down from user input to STOP */main()main(){{ /* variable declarations *//* variable declarations */ int counter; int counter; /* an integer to hold count values *//* an integer to hold count values */ int startPoint; int startPoint; /* starting point for countdown *//* starting point for countdown */ /* prompt user for input *//* prompt user for input */ printf("Enter a positive number: ");printf("Enter a positive number: "); scanf("%d", &startPoint); scanf("%d", &startPoint); /* read into startPoint *//* read into startPoint */ /* count down and print count *//* count down and print count */ for (counter=startPoint; counter >= STOP; counter--)for (counter=startPoint; counter >= STOP; counter--) printf("%d\n", counter);printf("%d\n", counter);}}8Preprocessor DirectivesPreprocessor Directives#include <stdio.h>#include <stdio.h>–Before compiling, copy contents of header file (stdio.h)into source code.–Header files typically contain descriptions of functions and variables needed by the program.•no restrictions -- could be any C source code#define STOP 0#define STOP 0–Before compiling, replace all instances of the string"STOP" with the string "0"–Called a macro–Used for values that won't change during execution,but might change if the program is reused. (Must recompile.)9CommentsComments•Begins with Begins with /*/* and ends with and ends with */*/•Can span multiple linesCan span multiple lines•Cannot have a comment within a commentCannot have a comment within a comment•Comments are not recognized within a stringComments are not recognized within a string–example: "my/*don't print this*/string"would be printed as: my/*don't print this*/stringAs before, use comments to help reader, not to As before, use comments to help reader, not to confuse or to restate the obviousconfuse or to restate the obvious10mainmain Function Function•Every C program must have a function called Every C program must have a function called main()main()..•This is the code that is executed when the program is run.This is the


View Full Document

UCSC CMPE 012 - HLL’s and the Basic Syntax

Download HLL’s and the Basic Syntax
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view HLL’s and the Basic Syntax and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view HLL’s and the Basic Syntax 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?