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