I. Why learn C? A. an important family of programming languages ------------------------------------------ C-LIKE PROGRAMMING LANGUAGES Many langauges derive their syntax and semantics from C: C++ Java C# and many others C-shell Objective-C Perl PHP Go R ------------------------------------------ B. used widely ------------------------------------------ C IS WIDELY USED Especially for: Operating systems Device drivers Scientific software Software on new processors Output of tools that generate code ------------------------------------------ C. Internet of Things (IoT) will increase usage ------------------------------------------ INTERNET OF THINGS WILL INCREASE USE The Internet of Things (IoT) consists of Most sensors and actuators are programmed in ------------------------------------------ II. C vs. Python A. lexical syntax ------------------------------------------ LEXICAL SYNTAX tokens Python C ========================================== comments # to eol... continuing STUFF \ STUFF \ a line + MORE + MORE names bool False, True literals int 0, 1, -1, ... 0, 1, -1, ... literals float 0.0, 0.271e1, 0.0, literals 5e-6 string "a str", literals 'another one' char "c", 'c', '\n' literals "\t" ------------------------------------------ B. context-free syntax 1. expressions ------------------------------------------ DIFFERENCES IN EXPRESSIONS expression Python C ========================================== parenthesis (E) (E) arithmetic A + B A + B A * B A * B A - B A - B A / B A // B A % B A % B A ** B comparisons A < B A < B A <= B A <= B A >= B A >= B A > B A > B A == B A != B A != B Boolean A and B operators A or B not B Ternary E1 if B else E2 operator function f(A1,A2) f(A1,A2) calls field R.G access string S[I] access string S1 = S1 + S2 ops len(S) S1 == S2 anon. (lambda X : E) funcion ------------------------------------------ 2. statements ------------------------------------------ DIFFERENCES IN STATEMENTS statement Python C ========================================== assignment X = E skip pass return return E return E; sequence S1 S1 S2 S2 if if E: S1 else: S2 loops while E: S for X in R: S loop ctrl break break; stmts continue continue; ------------------------------------------ What do you notice about the syntactic differences between Python and C compound statements? 3. declarations a. variable and function declarations ------------------------------------------ DIFFERENCES IN DECLARATIONS statement Python C ========================================== variable V = E function def F(arg): S ------------------------------------------ b. modules ------------------------------------------ DIFFERENCES IN MODULES AND USES In Python, files are modules, used via import M In C, Python C ======================================== import math import LispList ------------------------------------------ C. static semantics What does Python do to compute A+B ? 1. types and type checking ------------------------------------------ DIFFERENCES IN TYPE CHECKING Python C ======================================== type dynamically checks Examples: def implies(b1,b2): return not b1 or b2 def trianglearea(b,h): return 1/2 * b * h ------------------------------------------ 2. naming and declaration scope ------------------------------------------ SCOPE OF DECLARAIONS def: the *scope* of a declaration (or definition) is In Python, the scopes are: global (to a program) local: - to an object - to a function In C the scopes are: global (to a program) ------------------------------------------ ------------------------------------------ EXAMPLE OF LOCAL SCOPES IN C #include void printit(int i) { printf("i is %d\n", i); } int main() { int i = 0; printit(i); { int i = 2; printit(i); i += 1; printit(i); } printit(i); if (i == 0) { int i = 4; printit(i); } else { int i = 5; printit(i); } printit(i); } ------------------------------------------ What does this print? 3. modules ------------------------------------------ DIFFERENCES IN MODULES In Python, imports affect the global dictionary, dynamically In C #includes are ------------------------------------------ 4. documentation conventions ------------------------------------------ DOCUMENTATION CONVENTIONS In Python: - your name goes in a comment at the top of a file - a triple-quoted documentation string follows the function header In C - your name goes in a comment at the top of the file - put ------------------------------------------ 5. programs ------------------------------------------ DIFFERENCES IN PROGRAMS In Python, a program runs the statement in the __main__ module In C, a program runs the function int main(int argc, char *argv[]) ------------------------------------------ 6. working with programs (editing, compiling) ------------------------------------------ WORKING ON C PROGRAMS In C, a program is a collection of files, with a main function Steps in writing and running a program ------------------------------------------