230 likes | 351 Views
Introduction to Computers and Programming. Class 2 Introduction to C Professor Avi Rosenfeld. For Those Who Missed it…. Course home page is at www.cs.nyu.edu/courses/fall01/V22.0002-001/index.htm Syllabus is accessible from the home page
E N D
Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld Introduction to Computers and Programming - Class 2
For Those Who Missed it… • Course home page is at www.cs.nyu.edu/courses/fall01/V22.0002-001/index.htm • Syllabus is accessible from the home page • There will be seven homeworks, two midterms and a final • There will be OPTIONAL homeworks given more frequently • Office hours are on Wednesday, 8:30-9:30 A.M., room 419 CIWW, and by appointment Introduction to Computers and Programming - Class 2
Style vs. Syntax • Syntax – elements that are needed • Grammar • Style – elements that improve human comprehension • Comments, indenting, and other elements Introduction to Computers and Programming - Class 2
A Sample Program /* our first program in C */ #include <stdio.h> /* start of the main program */ int main() { printf( “Hello World!\n” ); return 0; } /* end program */ Introduction to Computers and Programming - Class 2
Escape Characters • The backslash (\) is called an escape character • Indicates that printf is supposed to do something unusual • When encountering a backslash, printf looks to the next character and combines it with the backslash to form an escape sequence Introduction to Computers and Programming - Class 2
Other escape sequences • \t • horizontal tab • \a • alert- will make the computer beep • \\ • prints a backslash character in a printf statement • \” • prints a double quote character in a printf statement Introduction to Computers and Programming - Class 2
Printing Several Lines • One printf statement can print several lines by using newline characters. • e.g. printf(“Welcome\nto\nC!\n”); • But that’s a stylistic horror! A better way: printf( “Welcome \n” “to \n” “C!\n” ); Introduction to Computers and Programming - Class 2
Printing Long Lines • Can use two or more printf statements (note the first has no newline) printf( “The quick brown fox jumped ” ); printf( “over the lazy dog.\n” ); • This will print one line like so: The quick brown fox jumped over the lazy dog. Introduction to Computers and Programming - Class 2
Why does this work like this? • Because C ignores most white space characters in your editor (spaces, tabs, carriage-returns (“enters”), etc.) • C is looking for the \n character to tell it when to go to the next line Introduction to Computers and Programming - Class 2
Declaration • Creating an identifier and associating it with a type and (behind the scenes) a location in memory. A couple of examples: • int integer1; • int integer1, integer2, sum; • integer1, integer2 and sum are all variables Introduction to Computers and Programming - Class 2
Variables • Variables hold data values which can change • They must be declared with a data type and a name, immediately after a left brace, before they can be used • A variable name in C is any valid identifier Introduction to Computers and Programming - Class 2
Data Types • C (as well as many other programming languages) are very sensitive to data type. • The int family • short, long • The float family • Double • Characters (strings) Introduction to Computers and Programming - Class 2
Identifier • An identifier is a series of characters consisting of letters, digits and underscores “_” that does not begin with a digit or include several special characters such as math signs, $, and others • Can be any length but only the first 31 characters are required to be recognized by ANSI C compilers • Keep identifiers 31 characters or less for portability and fewer problems Introduction to Computers and Programming - Class 2
C Is Case Sensitive • Upper case and lower case letters are different in C • E.g., lower case a1 and capital A1 are different identifiers Introduction to Computers and Programming - Class 2
Good Programming Practices • Choose meaningful variable names to help make a program self-documenting (fewer comments will be needed). • First letter of an identifier used as a variable name should be a lower case letter. • Multiple-word variable names can help make a program more readable. • Use mixed-cases to help make the word stand out. • E.G. totalCommissions. Introduction to Computers and Programming - Class 2
Printf Example #3 #include <stdio.h> int main() { int num1 = 3, num2 = 2; printf("%d plus %d equals %d\n", num1, num2, num1+num2); return 0; } Introduction to Computers and Programming - Class 2
Conversion Specifiers • %d – integer • %f – float • %c – character • %s - string Introduction to Computers and Programming - Class 2
Arithmetic in C • The C arithmetic operators are + for addition - for subtraction * for multiplication / for division, and % for modulus Introduction to Computers and Programming - Class 2
Binary Operators • Operators that take two operands • e.g. “+” is a binary operator and “a + b” has two operands (a and b) • Note integer division will yield an integer result • e.g. 5 / 2 = 2 (not 2 1/2) and 17 / 5 = 3 • modulus is the remainder after an integer division • e.g. 5 % 2 is 1 and 17 % 5 is 2 Introduction to Computers and Programming - Class 2
Big (Very Common) Error • Divide by Zero • e.g. x = y / 0 • Normally undefined by computer systems and generally results in a fatal error • Usually shows up at run time Introduction to Computers and Programming - Class 2
Rules of Operator Precedence • C evaluates arithmetic expressions in a precise sequence determined by the rules of operative precedence • Expressions within parentheses are evaluated first (highest level of precedence) • For nested or embedded parentheses, the expression in the innermost pair is evaluated first Introduction to Computers and Programming - Class 2
Rules of Operator Precedence cont’d • Multiplication, division and modulus operations are evaluated next • If more than one evaluated from left to right • Addition and subtraction operations are evaluated last • If more than one evaluated from left to right Introduction to Computers and Programming - Class 2
Parentheses • Are your friends • Are your really good friends • Because with them you can ensure expressions are evaluated as you expect • Can avoid mistakes with operator precedence (one less thing to think about) • e.g. y = m * x + b ; y = (m * x) + b; • e.g. y = a * b * b + c * b – d; y = ((((a * b) * b) + (c * b)) – d); Introduction to Computers and Programming - Class 2