CS 100Lecture 211 Announcements P5 due on Thursday FINAL EXAM Tuesday August 10, 8AM, Olin 155 Review sessions on Thursday and Friday Final grades posted.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Programming Languages and Paradigms The C Programming Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Kernighan/Ritchie: Kelley/Pohl:
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Chapter 10.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Introduction to C.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Introduction to Programming
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS 1704 Introduction to Data Structures and Software Engineering.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
CS 100Lecture 221 Announcements P5 due Thursday Final Exam: Tuesday August 10, 8AM - 10AM, Olin 155 Want references? Remember to drop off a picture and.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Data Types In Text: Chapter 6.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
The Machine Model Memory
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Programming Languages and Paradigms
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Paradigms
C Basics.
User-Defined Functions
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
7 Arrays.
CS100A Lecture November 1998 Prelim 3 Statistics Maximum 94
Chapter 11 Programming in C
Programming Languages and Paradigms
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

CS 100Lecture 211 Announcements P5 due on Thursday FINAL EXAM Tuesday August 10, 8AM, Olin 155 Review sessions on Thursday and Friday Final grades posted next Tuesday night/Wednesday morning. See me Wednesday, if you have questions, as I’m leaving town Wednesday night!

CS 100Lecture 212 Today’s Topics C -- another programming language Goal: acquire a basic reading knowledge of C control structures program organization Standard Reference: The C Programming Language by Kernighan and Ritchie -- fairly terse

CS 100Lecture 213 Java vs. C Java Advantages Direct support of classes, objects, and other tools for effectively organizing medium-to-large programs. Extensive standard class libraries (for I/O, GUIs, networking, etc.). Automatic memory management (garbage collection). Portable (write once, run everywhere), because of the Java Virtual Machine. Used for writing Applets for the world wide web

CS 100Lecture 214 Java vs. C Java Disadvantages Portability, garbage collection, and other features impose execution time and space overhead. Java runtime environment keeps Java program at a distance from the underlying machine. Therefore, Java programs are much slower than C programs.

CS 100Lecture 215 Java vs. C C Advantages Data types and constructs are close to those provided by the underlying hardware (microprocessor), so: Good for writing programs that squeeze maximum performance from hardware. Possible to have absolute control over machine resources --good for writing software that directly manipulates hardware devices. C is often thought of as a high-level assembly language.

CS 100Lecture 216 Java vs. C C Disadvantages Not objected oriented (but C++ is) No garbage collection. Programmer must manage the allocation and freeing of storage. This is highly prone to error. Care needed to write programs that are portable. As will be seen, programmer must manage “pointers” or references themselves, an extremely confusing and error-prone issue for many.

CS 100Lecture 217 Basic C Data types Standard types similar to those in Java – int, long (integers) – float, double (floating point) – char (characters, normally ASCII) No type boolean –0 represents false (like Matlab) –non-0 represents true –relations ( = >) and logical operations (!, &&, ||) yield 0 of false, 1 if true Size of arithmetic types depends on machine/compiler. Examples: –int: usually 16 or 32 bits –long: at least as many bits as int, usually 32, sometimes 64. Need to be careful if you want portable code.

CS 100Lecture 218 Basic C Statements Assignment as in C: –variable = expression ; Loops and conditionals just like in Java. –if (condition)if (condition) statementstatement else statement –while (condition) statement –for (initialize; test; step) statement

CS 100Lecture 219 Contrast with Java on declaration locations: Curly braces used to group statements, as in Java. But declarations cannot appear in a block, only in a method body. For example, the following is not legal C: if (x < y) {int tmp= x; x= y; y= tmp;}

CS 100Lecture 2110 C Functions C program is collection of functions No classes, so function declarations appear freely in source code No explicit public/private distinction Example: /* Yield maximum of x and y */ int max (int x, int y) { if (x >= y) return x; else return y;}

CS 100Lecture 2111 C Procedures In C, a procedure is a function with return type void /*Print n lines of stars; line i, for 1<= i <= n, has i stars on it */ void print_stars (int n) { int j, k; for (k= 1; k <= n; k++) { /* Print line k, with k stars */ for (j= 1; j <= k; j++) printf(“*”); printf(“\n”); }

CS 100Lecture 2112 Source files (.c files) C function definitions are placed in source files that have names ending in “.c”. All functions normally may be accessed (called) by any other function in any file. A function definition may be preceded by “static” to restrict access so that it may be called only by other functions defined in the same file. External (“global”) variables may be defined in a.c file outside any function. –External variables are created when program execution begins; –they retain their values until execution of the program terminates. –External variables may also be defined static in order to restrict access to them to the file containing the variable definition.

CS 100Lecture 2113 Source files (.h files) Every function (and external variable) must be defined exactly once by giving a full definition in some.c source file. A function (or external variable) may be called (or referenced) in other files provided that there is a declaration of the function (or external variable) in the other files. The declaration of a function gives the function name and parameters, but not the function body. Example: int max(int x, int y); Related declarations are often grouped in header files (name ending in “.h”). The declarations in a head file xyz.h can be incorporated in another file by the directive –#include “xyz.h” (for user files) or –#include (for standard C libraries)

CS 100Lecture 2114 Example: math library File math.h contains declarations of the standard C math library routines: –double sqrt (double x); –double sin (double x); –double cos (double x); –etc. … A client program can access the definitions using the appropriate include directive: #include /* yield twice the square root of x */ double sqrt2 (double x) { return 2 * sqrt(x); }

CS 100Lecture 2115 Standard Output The library includes basic routines to –read formatted data from the standard input (usually the keyboard) and –print on the standard output (usually a window on the monitor). The basic output routine is printf. A call to printf has one or more arguments. –The first is the “format string”, which contains literal text to be printed interspersed with formatting instructions for printing the remaining arguments.

CS 100Lecture 2116 Example with printf #include /* Print a table of Fahenehit-Celsius values from 0 to 300 in increments of 20 */ void main (void) { int d; printf(“Fahrenheit Celsius\n”); for (d= 0; d <= 300; d= d+20) printf(“%3d %6.1f\n”, d, (5.0/9.0) * (d-32) ); }

CS 100Lecture 2117 Output of previous

CS 100Lecture 2118 Parameters and Arguments As in Java, the same four steps apply on a function call as on a method invocation Example: Consider execution of #include void f (int x, int y) { int tmp; tmp= x; x= y; y= tmp; } void main(void) { int j= 17; int k= 42; f(j, k); printf(“j= %d, k= %d\n”, j, k); }

CS 100Lecture 2119 Pointers and References In Java, one can’t write a procedure to swap the values of two int variables x and y. The following method and call does not change x and y: public void notaSwap(int p1, int p2) {int t= p1; p1= p2; p2= t;} int x= 5; int y= 6; notaSwap(x,y); But one can change the values of fields of a class: public class Coord {int x; int y;} // Swap x fields of p1 and p2 public static void Swap(Coord p1, Coord p2) {int t= p1.x; p1.x= p1.y; p1.y= t;} Swap(p1, p2);

CS 100Lecture 2120 Pointers in C C provides operator & to create a pointer to a variable int x= 5; int & p= &x; int & y; The type of y is “int &”, read as “address of int) C provides operator a “dereferencing” operator If p is a pointer to an int variable, then * is the variable. * p = * p + 1; int * x; The type of x is “int *”, read as “pointer to int” 5 x p 6 x p

CS 100Lecture 2121 Swap two integers void swap(int * x, int * y) { int tmp; tmp= * x; *x= *y; *y= tmp; } The parameters are no long of type int, but instead “point to int”.

CS 100Lecture 2122 Standard Input Function scanf in library reads from the standard input device and deciphers the input characters according to the format string given as the first argument of a call. The remaining arguments of a call to scanf are pointers to variables where the input values should be stored. As with printf, C compilers generally can’t (or don’t) check that the types of the variables match the format codes in the format string. A mismatch, usually a mistake, can lead to mysteriously scrambled bytes in memory or a program or machine crash.

CS 100Lecture 2123 stdin Example void main(void) { int j, k; printf(“Please type in two integers”); // read integers into j and k. scanf(“%d %d”, &j, &k); printf(“the larger of the two is\n %d”, max(j,k)); Note that scanf f requires pointers to variables as arguments!