Computer Science & Engineering 150A Problem Solving Using

Computer Science & Engineering 150A Problem Solving Using

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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 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