Welcome to... CS113: Introduction to C

Similar documents
CS 113: Introduction to

CS 241 Data Organization. August 21, 2018

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

CS 241 Data Organization using C

Course Information and Introduction

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

EE 209: Programming Structures for Electrical Engineering

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

BLM2031 Structured Programming. Zeyneb KURT

EECE.2160: ECE Application Programming Spring 2017

CS111: PROGRAMMING LANGUAGE II

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

EECE.2160: ECE Application Programming Spring 2019

Algorithms, Data Structures, and Problem Solving

CSE 230 Intermediate Programming in C and C++ Functions

Introduction to Computer Systems

Fundamentals of Programming Session 12

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

EE 209: Programming Structures for Electrical Engineering. (Many Slides Borrowed from Princeton COS 217)

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CSE 303: Concepts and Tools for Software Development

EC 413 Computer Organization

Introduction to C An overview of the programming language C, syntax, data types and input/output

EL2310 Scientific Programming

Welcome! COMP s1. Programming Fundamentals

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

COMP s1 Lecture 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Fall, $ cat welcome.c #include <stdio.h>

CS113: Lecture 4. Topics: Functions. Function Activation Records

EL2310 Scientific Programming

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Computer Science II Lecture 1 Introduction and Background

Your first C and C++ programs

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

Welcome to CS 241 Systems Programming at Illinois

Software Design and Analysis for Engineers

High Performance Computing MPI and C-Language Seminars 2009

VE281 Data Structures and Algorithms. Introduction and Asymptotic Algorithm Analysis

Variables and literals

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Welcome! COMP s1. Programming Fundamentals

COS 217: Introduction to Programming Systems!

These are notes for the third lecture; if statements and loops.

CS 211 Programming I for Engineers

Software Lesson 1 Outline

COMP1917: Computing 1 1. Introduction

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

COMP1917: Computing 1 1. Introduction

Review! Lecture 5 C Memory Management !

Decision Making -Branching. Class Incharge: S. Sasirekha

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Sixth lecture; classes, objects, reference operator.

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS 4349 Lecture August 21st, 2017

Computer Science 210: Data Structures

CS Programming In C

Arrays in C++ Instructor: Andy Abreu

CSC 2400: Computing Systems!

CS 351 Design of Large Programs Coding Standards

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS3157: Advanced Programming. Outline

Announcements. 1. Forms to return today after class:

EECS2031 Software Tools

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Technical Questions. Q 1) What are the key features in C programming language?

Assignment 1. CSI Programming Practice, Fall 2011

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Introduction to Computer Systems

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

EC312 Chapter 4: Arrays and Strings

CSE 303 Lecture 8. Intro to C programming

Formatting & Style Examples

CS 3813/718 Fall Python Programming. Professor Liang Huang.

6.S096 Lecture 1 Introduction to C

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS61C : Machine Structures

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

C Programming for Engineers Introduction

Transcription:

Welcome to... CS113: Introduction to C Instructor: Erik Sherwood E-mail: wes28@cs.cornell.edu Course Website: http://www.cs.cornell.edu/courses/cs113/2005fa/ The website is linked to from the courses page of the CS department. Assignments, lecture slides, etc. will be posted on the course website. 1

Registration Details The course meets from August 26 - September 23, MWF, 12:20-1:10. The add/drop deadline is September 2. THIS IS SOON! 2

Administration Lecture slides and readings are posted to the course website, as are assignments, announcements, etc. There s also a course newsgroup, cornell.class.cs113. See the course website for more details. If you have questions of a general nature, please post them to the newsgroup. Also, check the newsgroup occasionally, because that s where I ll post stuff like corrections, hints for homeworks, etc. Class runs for four weeks, three meetings a week. Wednesday, 2:30-4, or by appoint- Office hours: ment Prerequisite: CS100 or equivalent experience (e.g., at a different college or in an advanced high school course) All auditors welcome Course goal: to cover all major features of the C programming language, to the extent that students can subsequently learn about any features not discussed by reading a standard reference such as The C Programming Language by Kernighan and Ritchie. 3

Assignments, etc. There will be three assignments, due Fridays by 5 p.m. The last assignment will be longer and count for more; you will have more time to complete it. Turn in a printout of your program (no sample data necessary), AND e-mail the source code (the.c files) to wes28@cs.cornell.edu DO NOT just e-mail me your source code. YOU MUST turn in a printout of your source code at Friday s class or in my mailbox in Rhodes 657. Solutions to (the first three) assignments will be discussed during the class meetings immediately following the due dates. We ll have one quiz. Please come to class! We ll cover new material every time, and there s a lot to get through in four weeks. If you can t make a lecture, let me know and I ll tell you if you missed anything big. Please let me know if you anticipate that you ll have problems attending lectures or turning assignments in on time. 4

More Course Info. Grades on Assignments: check plus (exceptional), check (acceptable), check minus, X (insufficient effort) Turning in a program that compiles and is neatly formatted can help you avoid an X. (But this is not necessarily sufficient!) Grading for course: S/U only. You are guaranteed an S if all assignments completed with a grade of check. If you get an X on two or more assignments, do poorly on the quiz, and have attendance problems, you ll probably get a U. In-between cases will be handled at my discretion. I ll let you know if there s a problem though, so you shouldn t worry. Two textbooks: Practical C Programming by Steve Oualline and K&R. Both are recommended; neither is strictly necessary, especially if you have high-speed Web access so you can look stuff up. You may use any C compiler. (I recommend gcc, which comes with UNIX/Linux/MacOSX.) Information for using CodeWarrior is on the website. 5

A note on Collaboration Collaboration: You may discuss ideas on a high level with others, but all code must be your own. You should understand everything that you turn in. You can help another student debug his or her code, but you should not write any code for anyone else, and no one else should write code for you. Information on the Code of Academic Integrity is available on the website. In a self-selecting, noncompetitive course like this, the potential payoffs from cheating are extremely low, even though the risks involved are fairly high. 6

A language that doesn t affect the way you think about programming is not worth knowing. Alan Perlis Why Learn C? Learning C will help you to master programming concepts that higher-level languages like Java don t require you to worry about, such as: Pointers: how do you know where your objects are stored in memory? Function invocations: what happens when you call a function? Dynamic memory allocation: where does the memory for a new object come from? These concepts all revolve around the issue of memory. C forces you to understand how your programs deal with run-time memory because if you don t, you ll regularly make programming errors that can crash your whole program in nasty ways. 7

C is quirky, flawed, and an enormous success. Dennis Ritchie Meet the C programming language More about C: Very good for writing fast code, especially code that needs to have very explicit control over how memory is used. Good for writing programs that do system-level tasks (e.g., drivers, operating systems, etc.) Least common denominator : good building block for learning other languages. Subset of C++, similar to Java. Portable - compilers available for most any platform! ANSI C standard - aim for ANSI C compliance. C is almost always compiled to machine code. (Contrast with Java.) 8

The canonical first C program #include <stdio.h> void main() { printf( "Hello, world!\n" ); Notice: Every program you ll write for this class will include the standard functions functions in stdio.h. Functions like printf (which is in stdio.h) aren t part of the core C language, so you need to include them explicitly. All C programs must have a main() function; this is the first function invoked. The program terminates when this function terminates. printf is a function that prints formatted output void indicates that the program itself returns no value. Don t worry about this too much now... But: note that some compilers insist on the declaration int main(), in which case the statement return 0; should be added to end of program, or anywhere else where you want the program to end. (Returning 0 indicates that the program ended successfully.) 9

#include <stdio.h> Another example void main() { int x = 1, y; int sum; y = 3; sum = x + y; /* evaluates right hand side, places value in variable sum */ printf( "%d plus %d is %d\n", x, y, sum ); 10

Some comments on comments and keywords Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can t be nested (according to ANSI C standard)! Be careful. Some development environments have an option that allows one to nest comments. A curiosity: one can actually write a program that detects whether or not comments are nested or not! Example: /* /* /* Hi, I m a comment */ Keywords Reserved words that cannot be used as variable names Examples: break, if, else, do, for, while, int, void (exhaustive list in K&R, p192) Can be used within comments 11

Reading integers from standard input #include <stdio.h> void main() { int x, y; int product; printf( "Enter an integer: " ); scanf( "%d", &x ); printf( "Enter another integer: " ); scanf( "%d", &y ); product = x * y; printf( "%d times %d is %d\n", x, y, product ); scanf is like printf, except it reads from standard input instead of writing to it scanf is a dangerous, bad function, but you can use it for now, in this limited way, to read integers &x is a reference to the variable x rather than the value of x itself. You need to pass a reference so that scanf can modify the variable x. (Much more on this later in the course don t worry about it now.) 12

Summing the numbers 1 through 10. #include <stdio.h> void main() { int i = 1, sum = 0; while( i <= 10 ) { sum = sum + i; /* shortcut: sum += i; */ i = i + 1; /* shortcut: i++; */ printf( "The sum is %d\n", sum ); Note that the function is split into two parts: the variable declarations (and sometimes initializations), and the rest of the function. All C functions require variable declarations at the start of the function. This is different from, say, Java. 13

Summing the numbers 1 through 10. #include <stdio.h> void main() { int i = 1, sum = 0; for( i = 1; i <= 10; i++ ) { sum = sum + i; printf( "The sum is %d\n", sum ); General form of a for loop: for( initial-stmt; condition; iteration-stmt ) body-stmt; What happens? 1. Initialization is performed. 2. Condition is checked; if false, loop terminates. Otherwise... 3. Body is performed, followed by the iteration statement. 4. Then, the condition is checked again, and so forth. 14

#include <stdio.h> void main() { int a, b; Equality testing. printf( "Enter a number: " ); scanf( "%d", &a ); printf( "Enter another: " ); scanf( "%d", &b ); if( a == b ) { printf( "They re equal!\n" ); else { printf( "They re not equal.\n" ); Note: Double equals used to compare ints else portion of if optional 15

Bracing Styles Four widely used bracing styles: 1TBS: One True Bracing Style - used by K & R, and my personal preference for( j = 0; j < 10; j++ ) { printf( "%d", j ); Allman for( j = 0; j < 10; j++ ) { printf( "%d", j ); Whitesmith for( j = 0; j < 10; j++ ) { printf( "%d", j ); GNU for( j = 0; j < 10; j++ ) { printf( "%d", j ); Most important rule of style: Be consistent. 16

Exponentiation void main() { int base, exponent, result; printf( "Enter the base:" ); scanf( "%d", &base ); printf( "Enter the exponent:" ); scanf( "%d", &exponent ); for( result = 1; exponent > 0; exponent-- ) { result *= base; printf( "%d\n", result ); 17

Exponentiation, the sequel void main() { int base, exponent, result = 1; printf( "Enter the base:" ); scanf( "%d", &base ); printf( "Enter the exponent:" ); scanf( "%d", &exponent ); while( exponent > 0 ) { if( exponent % 2 == 1 ) result *= base; base *= base; exponent /= 2; printf( "%d\n", result ); Note: Trickier: maintain invariant that result times base to the exponent power is the desired value Faster algorithm 18