Computer Science & Engineering 150A Problem Solving Using

Computer Science & Engineering 150A Problem Solving Using

CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Language Elements General Form Lecture 02 - Introduction To C Operators & Expressions Formatting I/O Stephen Scott Running Programs (Adapted from Christopher M. Bourke) Common Pitfalls Fall 2009 1 / 77 [email protected] CSCE150A Introduction C Language Elements Language Variable Declarations and Data Types Elements General Form Executable Statements Operators & General Form of a C Program Expressions Formatting Arithmetic Expressions I/O Running Formatting Numbers in Program Output Programs Interactive Mode, Batch Mode, and Data Files Common Pitfalls Common Programming Errors 2 / 77 Overview of C Programming CSCE150A Introduction Language Elements This chapter introduces C { a high-level programming language developed General Form in 1972 by Dennis Ritchie at AT&T Bell Laboratories. Operators & Expressions This chapter describes the elements of a C program and the types of data Formatting I/O that can be processed by C. It also describes C statements for performing Running computations, for entering data, and for displaying results. Programs Common Pitfalls 3 / 77 C Language Elements CSCE150A Introduction Preprocessor Directives Language Elements Syntax Displays for Preprocessor Directives Preprocessor Comments \int main()" Function Main Function Reserved Words Reserved Words Program Style Executable Statements Standard Identifiers Input/Output General Form User-Defined Identifiers Operators & Uppercase and Lowercase Letters Expressions Formatting Program Style I/O Running Programs Common 4 / 77 Pitfalls Preprocessor Directives CSCE150A Introduction Language The C preprocessor modifies the text of the C program before it is Elements passed to the compiler. Preprocessor Comments Main Function Preprocessor directives are C program lines beginning with a # that Reserved Words Program Style provide instructions to the C preprocessor. Executable Statements Input/Output Preprocessor directives begins with a #, e.g. #include and #define. General Form Predefined libraries are useful functions and symbols that are Operators & Expressions predefined by the C language (standard libraries). Formatting I/O Running Programs Common 5 / 77 Pitfalls #include and #define CSCE150A Introduction #include<libraryName> gives the program access to a library Language Elements Example: #include<stdio.h> (standard input and output) has Preprocessor Comments definitions for input and output, such as printf and scanf. Main Function Reserved Words #define NAME value associates a constant macro Program Style Executable Statements Example: Input/Output General Form 1 #defineKMS_PER_MILE 1.609 Operators & 2 #definePI 3.14159 Expressions Formatting I/O Running Programs Common 6 / 77 Pitfalls Comments CSCE150A Introduction Comments provide supplementary information making it easier for us to Language Elements understand the program, but comments are ignored by the C preprocessor Preprocessor Comments and compiler. Main Function Reserved Words Program Style - anything between them with be considered a comment, even Executable /**/ Statements Input/Output if they span multiple lines. General Form // - anything after this and before the end of the line is considered a Operators & Expressions comment. Formatting I/O Running Programs Common 7 / 77 Pitfalls Function main CSCE150A The point at which a C program begins execution is the main Introduction Language function: Elements Preprocessor 1 int main(void) Comments Main Function Reserved Words Program Style Every C program must have a main function. Executable Statements Input/Output The main function (and every other function) body has two parts: General Form Declarations - tell the compiler what memory cells are needed in the Operators & function Expressions Executable statements - (derived from the algorithm) are translated Formatting I/O into machine language and later executed Running Programs Common 8 / 77 Pitfalls Function main CSCE150A Introduction Language Elements All C functions contain punctuation and special symbols Preprocessor Comments Punctuation - commas separate items in a list, semicolons appear at Main Function Reserved Words the end of each statement Program Style Executable Special symbols: *, =, {, }, etc. Statements Input/Output Curly braces mark the beginning and end of the body of every General Form function, including main Operators & Expressions Formatting I/O Running Programs Common 9 / 77 Pitfalls Reserved Words CSCE150A Introduction A word that has special meaning in C. E.g.: Language int - Indicates that the main function (or any other function) returns Elements Preprocessor an integer value, or that a memory cell will store an integer value Comments - Indicates that a function returns a real number or that a Main Function double Reserved Words memory cell will store a real number Program Style Executable Statements Always lower case Input/Output General Form Can not be used for other purposes Operators & Appendix E has a full listing of reserved words (ex: Expressions Formatting double, int, if, else, void, return etc.) I/O Running Programs Common 10 / 77 Pitfalls Standard Identifiers CSCE150A Introduction Language Standard identifiers have a special meaning in C (assigned by Elements Preprocessor standard libraries). Comments Main Function Standard identifiers can be redefined and used by the programmer for Reserved Words Program Style other purposes Executable Statements Not recommended If you redefine a standard identifier; C will no Input/Output longer be able to use it for its original purpose. General Form Operators & Examples: input/output functions printf, scanf Expressions Formatting I/O Running Programs Common 11 / 77 Pitfalls User-Defined Identifiers CSCE150A We choose our own identifiers to name memory cells that will hold data Introduction and program results and to name operations (functions) that we define Language Elements (more on this in Chapter 3) Preprocessor Comments Main Function An identifier must consist only of letters , digits , Reserved Words [a-zA-Z] [0-9] Program Style Executable and underscores. Statements Input/Output An identifier cannot begin with a digit (and shouldn't begin with an General Form underscore). Operators & Expressions A C reserved word cannot be used as an identifier. Formatting I/O An identifier defined in a C standard library should not be redefined. Running Programs Common 12 / 77 Pitfalls User-Defined Identifiers CSCE150A Examples: letter_1, Inches, KMS_PER_MILE Introduction Some compilers will only see the first 31 characters Language Elements Uppercase and lowercase are different Preprocessor Comments (Variable, variable, VARIABLE are all different) Main Function Reserved Words Choosing identifer names: Program Style Executable Statements Choose names that mean something Input/Output Should be easy to read and understand General Form Shorten only if possible Operators & Expressions Don't use Big, big, and BIG as they are easy to confuse Formatting I/O Identifiers using all-caps are usually used for preprocessor-defined Running constants (#define) Programs Common 13 / 77 Pitfalls Program Style CSCE150A Introduction Language Elements Preprocessor A program that \looks good" is easier to read and understand than one Comments Main Function that is sloppy (i.e. good spacing, well-named identifiers). Reserved Words Program Style Executable In industry, programmers spend considerably more time on program Statements Input/Output maintenance than they do on its original design or coding. General Form Operators & Expressions Formatting I/O Running Programs Common 14 / 77 Pitfalls Style Tips Rigorous Comments CSCE150A Introduction Language The number of comments in your program doesn't affect its speed or Elements size. Preprocessor Comments Main Function Always best to include as much documentation as possible in the Reserved Words Program Style form of comments. Executable Statements Input/Output Begin each program or function with a full explanation of its inputs, General Form outputs, and how it works. Operators & Expressions Include comments as necessary throughout the program Formatting I/O Running Programs Common 15 / 77 Pitfalls Style Tips Naming Conventions CSCE150A Introduction Language Give your variables meaningful names (identifiers) Elements Preprocessor Comments x,y may be good if you're dealing with coordinates, but bad in Main Function Reserved Words general. Program Style Executable myVariable, aVariable, anInteger, etc are bad: they do not Statements Input/Output describe the purpose of the variable. General Form tempInt,PI, numberOfStudents are good because they do. Operators & Expressions Formatting I/O Running Programs Common 16 / 77 Pitfalls Style Tips CamelCaseNotation CSCE150A Old School C convention: separate compound words with underscores Introduction Language number_of_students, interest_rate, max_value, etc. Elements Preprocessor Underscore (shift-hyphen) is inconvenient Comments Main Function Solution: camelCaseNotation - connect compound words with Reserved Words Program Style upper-case letters. Executable Statements Input/Output Example: numberOfStudents, interestRate, maxValue, etc. General Form Much easier to shift-capitalize Operators & Expressions Much more readable Formatting I/O Ubiquitous outside of programming: MasterCard, PetsMart, etc. Running Programs Common 17 / 77 Pitfalls Preprocessor Directives Reserved Words Variables Special Symbols Punctuation Anatomy of a Program CSCE150A 1 /* 2 * Converts distances from miles to kilometers.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    88 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us