Programming Fundamentals - Week 8 - PDF
Document Details
Uploaded by FearlessHammeredDulcimer1753
Singapore Institute of Technology (SIT)
Frank Guan, PhD
Tags
Summary
These lecture notes cover programming fundamentals, focusing on the introduction to the C programming language for undergraduate students at the Singapore Institute of Technology (SIT). The document includes topics on the history of C, differences between C and other languages, variable declarations, whitespace, and an agenda for the week's lectures.
Full Transcript
WELCOME A/PROF FRANK GUAN, PHD INF1002 – PROGRAMMING FUNDAMENTALS WEEK 8 ABOUT ME – Dr Frank Guan, PhD Associate Professor, SIT – Research interests: – Virtual Reality (VR) – Augmented Reality (AR) – Artificial Intelligence (AI) – Entrepreneu...
WELCOME A/PROF FRANK GUAN, PHD INF1002 – PROGRAMMING FUNDAMENTALS WEEK 8 ABOUT ME – Dr Frank Guan, PhD Associate Professor, SIT – Research interests: – Virtual Reality (VR) – Augmented Reality (AR) – Artificial Intelligence (AI) – Entrepreneurship & innovation – If any module related matter, please email me at: [email protected] 2 HOUSEKEEPING – Lecture online (Zoom) – Recorded – Be focused and limit distractions – Questions: – Voice up – Type in chat window – Other matters – Respect – Integrity 4 INTRODUCTION TO C DR FRANK GUAN INF1002 – PROGRAMMING FUNDAMENTALS WEEK 8 Agenda 1. History of C 2. From Python to C 3. A simple C program 4. Basic data types 5. C formatted I/O 6. Control structures 6 RECOMMENDED READING Paul Deitel and Harvey Deitel C: How to Program 9th Edition Pearson, 2021 7 HISTORY OF C QUESTION – Type in the Zoom Chat window of the result of adding 37 to 45 9 HOW ABOUT ASKING A COMPUTER – Define two integer variables – Assign values (35 and 47) for each variable – Calculate the sum value of 35 and 47 – Print out the result (82) on the screen HOW TO DO 10 WHY PROGRAMMING LANGUAGE https://usemynotes.com/assembly-language-and-machine-language/ 11 HISTORY OF C J. Presper Eckert and John Mauchly C Electronic Numerical Integrator and Computer B 1943 - 1946 Martin Richard created the Basic Combined Programming Language in 1966 and 1967. Ken Thompson developed BCPL B – based on BCPL in 1969 at Bell Lab. An early version of UNIX was written in B. 13 HISTORY OF C C was created by Dennis Ritchie at Bell Lab between 1972 - 1973. 1970s Traditional C 1989 – C89/ANSI C/Standard C 1990 – ANSI/ISO C 1999 – C99: an attempt to standardise many variations of C Dennis Ritchie (left) and Ken Thompson were the lead developers of UNIX. UNIX was re-written in C in 1973. 14 C VS. RELATED LANGUAGES More recent derivatives: C++, Objective C, C# Influenced: Java, Perl, Python (quite different) Exceptions C lacks: Range-checking Garbage collection Object-oriented programming Low-level language - faster code (usually) 15 FROM PYTHON TO C 1. COMPILERS VS. INTERPRETERS Python uses an interpreter to execute a program. In C, the compiler converts a program into machine code. The machine code can be executed directly by the CPU. 17 1. COMPILERS VS. INTERPRETERS Compiler 1 0001 1 program.c a.out 12 123 1234 12345 Source code Machine code Interpreter 18 1. COMPILERS VS. INTERPRETERS C programs can lead to faster runtimes. C is designed so the compiler can tell everything it needs to know to translate the program into machine code. 19 2. VARIABLE DECLARATIONS C requires variable declarations These tell the compiler about the variable before it is used. Once declared, you cannot change its type. if you happen to misspell a Good variable’s name 20 2. VARIABLE DECLARATIONS In Python: no declaration required You are responsible for checking your own code! More convenient. 21 3. WHITESPACE In Python, Identify new statements whitespace characters are important. Identify blocks C does not use Use a semi-colon to terminate statements whitespace except for Use braces {} to separating words. delimit blocks 22 4. FUNCTIONS – All C code must be put within functions. – The main() function is the “starting point” for a C program. 23 4. FUNCTIONS #include def gcd(a, b): if b == 0: int gcd(int a, int b) { return a else: if (b == 0) return gcd(b, a % b) return a; else print("GCD: " + str(gcd(24, 40))) return gcd(b, a % b); } Python Program int main() { printf("GCD: %d\n", gcd(24, 40)); return 0; } C Program From http://www.cs.toronto.edu/~patitsas/cs190/c_for_python.html 24 GOOD CODING PRACTICE IN C – Indent blocks of code as in Python – Format braces and whitespace consistently – Use comments to explain your code to other programmers – Use meaningful variable names int i;main(){for(;i["]