APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Similar documents
PRINCIPLES OF OPERATING SYSTEMS

Kurt Schmidt. October 30, 2018

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

Programming refresher and intro to C programming

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CSE 333 SECTION 3. POSIX I/O Functions

CSE 333 SECTION 3. POSIX I/O Functions

CSE 303: Concepts and Tools for Software Development

Dynamic memory. EECS 211 Winter 2019

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

COMP s1 Lecture 1

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

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CSE 333 Midterm Exam July 24, Name UW ID#

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Array Initialization

C programming basics T3-1 -

Project 2: Shell with History1

211: Computer Architecture Summer 2016

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

APT Session 6: Compilers

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Dynamic Memory Allocation and Command-line Arguments

CSC231 C Tutorial Fall 2018 Introduction to C

Arrays and Pointers in C. Alan L. Cox

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CSE 303 Lecture 8. Intro to C programming

Memory Management in C (Dynamic Strings) Personal Software Engineering

Creating a Shell or Command Interperter Program CSCI411 Lab

Programming in C. What is C?... What is C?

CSC 1600 Memory Layout for Unix Processes"

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Lecture 07 Debugging Programs with GDB

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Recitation 2/18/2012

APT Session 7: Compilers

CSC258: Computer Organization. Functions and the Compiler Tool Chain

CSE 374 Programming Concepts & Tools

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Contents. A Review of C language. Visual C Visual C++ 6.0

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Warm-up sheet: Programming in C

Memory management. Johan Montelius KTH

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

C for C++ Programmers

CSC 2405: Computer Systems II

High Performance Programming Programming in C part 1

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CSC 2400: Computer Systems. Arrays and Strings in C

8. Characters, Strings and Files

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Lectures 5-6: Introduction to C

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Lectures 5-6: Introduction to C

Memory Corruption 101 From Primitives to Exploit

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CMPSC 311- Introduction to Systems Programming Module: Introduction to C

CSC 2400: Computer Systems. Arrays and Strings in C

CSE 12 Spring 2018 Week One, Lecture Two

CS61, Fall 2012 Section 2 Notes

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

COMP 2355 Introduction to Systems Programming

CSE 303 Midterm Exam

CSE 12 Spring 2016 Week One, Lecture Two

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

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

Dynamic Data Structures. CSCI 112: Programming in C

CSE 351. Introduction & Course Tools

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Friday, February 10, Lab Notes

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Final CSE 131B Spring 2004

CS 261 Fall Mike Lam, Professor. Structs and I/O

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

Processes. Johan Montelius KTH

Transcription:

APT Session 4: C Laurence Tratt Software Development Team 2017-11-10 1 / 14 http://soft-dev.org/

What to expect from this session 1 C. 2 / 14 http://soft-dev.org/

Prerequisites 1 Install either GCC or LLVM/clang onto your computer. Most Unixes will have either/both installed by default, or as easy-to-install packages. 3 / 14 http://soft-dev.org/

Prerequisites (Windows) Windows users may find these instructions (courtesy of Sam White) useful: 1 Download the MinGW web installer. 2 Launch the installer, hit Install, then Continue (leave the installation directory as the default, C:\MinGW). The installer will now download the files necessary. Once complete, hit Continue. 3 The MinGW Installation Manager will now launch. Right-click mingw32-base and select Mark for Installation. Now, select Apply Changes from the Installation menu. 4 Hit Apply. MinGW will now download and install the base package. This may take a minute or two. Once finished, you can close both windows. MinGW is now installed. 5 You can now find gcc in C:\MinGW\bin. You should add this directory to your PATH to make development easier (go to the Control Panel; search for "environment variables"; edit "PATH"; click "New" and add C:\MinGW\bin into the text box). 4 / 14 http://soft-dev.org/

Prerequisites (OS X) On recent OS X, executing xcode-select --install at the command-line should install GCC and clang. 5 / 14 http://soft-dev.org/

C C is a low-level programming language initially designed as the implementation language of Unix. First usable version debuted in 1973. 6 / 14 http://soft-dev.org/

C C is a low-level programming language initially designed as the implementation language of Unix. First usable version debuted in 1973. Still used for writing operating systems, programming languages critical utilities, embedded systems etc. etc. 6 / 14 http://soft-dev.org/

C C is a low-level programming language initially designed as the implementation language of Unix. First usable version debuted in 1973. Still used for writing operating systems, programming languages critical utilities, embedded systems etc. etc. C++ is a separate language that adds many extra things to C; too complex for my tastes. C is still actively (if slowly) developed: new versions in 99 and 11. Few changes in C11 of interest to us, so we ll use C99. 6 / 14 http://soft-dev.org/

The basics C is syntactically very similar to Java. Major immediate differences: No classes or objects, just top-level functions and structs. Memory must be allocated and freed manually. 7 / 14 http://soft-dev.org/

The basics C is syntactically very similar to Java. Major immediate differences: No classes or objects, just top-level functions and structs. Memory must be allocated and freed manually. Other useful things to know: The main method in C is int main(int argc, char **argv); the return value is returned to the shell. return 0; means I finished successfully. #include <stdio.h> is similar to a Java import. It brings the printf function which prints a string to screen into scope. 7 / 14 http://soft-dev.org/

The basics C is syntactically very similar to Java. Major immediate differences: No classes or objects, just top-level functions and structs. Memory must be allocated and freed manually. Other useful things to know: The main method in C is int main(int argc, char **argv); the return value is returned to the shell. return 0; means I finished successfully. #include <stdio.h> is similar to a Java import. It brings the printf function which prints a string to screen into scope. Exercises: 1 Write a program which prints out Hello world! in C. Put it in a file hello.c and compile it with gcc -Wall --std=c99 hello.c. This will produce an a.out (Unix) or a.exe (Windows) file which can then be run. 7 / 14 http://soft-dev.org/

Basic types C has some similar basic types to Java: int and char. For any type, one can make a pointer type with *. char * is a pointer to a sequence of characters of unknown length (i.e. roughly equivalent to a Java string). 8 / 14 http://soft-dev.org/

Basic types C has some similar basic types to Java: int and char. For any type, one can make a pointer type with *. char * is a pointer to a sequence of characters of unknown length (i.e. roughly equivalent to a Java string). Other useful things to know: C can t concatenate strings with +. But printf takes a format string as its first argument. %s characters are replaced with strings passed as parameters. printf("hello %s", "world") prints out hello world. 8 / 14 http://soft-dev.org/

Basic types C has some similar basic types to Java: int and char. For any type, one can make a pointer type with *. char * is a pointer to a sequence of characters of unknown length (i.e. roughly equivalent to a Java string). Other useful things to know: C can t concatenate strings with +. But printf takes a format string as its first argument. %s characters are replaced with strings passed as parameters. printf("hello %s", "world") prints out hello world. Exercises: 1 Assign the string Hello world! to a variable of type char * then print out the contents of the variable. 8 / 14 http://soft-dev.org/

Pointers Pointers are everything to C. 9 / 14 http://soft-dev.org/

Pointers Pointers are everything to C. A pointer is an integer which references an address in memory. Pointers can be changed to other memory addresses. Think of a piece of string (the pointer) tied to a balloon (the bit of memory we re interested in). Multiple pieces of string can point to the same balloon. We can untie our string and tie it to another balloon if we want. 9 / 14 http://soft-dev.org/

Pointers Pointers are everything to C. A pointer is an integer which references an address in memory. Pointers can be changed to other memory addresses. Think of a piece of string (the pointer) tied to a balloon (the bit of memory we re interested in). Multiple pieces of string can point to the same balloon. We can untie our string and tie it to another balloon if we want. e.g. if we have a variable v pointing to a char * array, we can access the first character either by explicit dereferencing with *v or using array syntax v[0] (the two are equivalent). strlen returns the length of a string. #include <string.h> 9 / 14 http://soft-dev.org/

Pointers are everything to C. Pointers A pointer is an integer which references an address in memory. Pointers can be changed to other memory addresses. Think of a piece of string (the pointer) tied to a balloon (the bit of memory we re interested in). Multiple pieces of string can point to the same balloon. We can untie our string and tie it to another balloon if we want. e.g. if we have a variable v pointing to a char * array, we can access the first character either by explicit dereferencing with *v or using array syntax v[0] (the two are equivalent). strlen returns the length of a string. #include <string.h> Exercises: 1 Assign the string Hello world! to a variable of type char * then print out each character of the string on a new line. 9 / 14 http://soft-dev.org/

Arrays Pointers can be used as arrays. e.g. char ** is an array of strings. But C arrays don t know their length. That must always be passed around separately. 10 / 14 http://soft-dev.org/

Arrays Pointers can be used as arrays. e.g. char ** is an array of strings. But C arrays don t know their length. That must always be passed around separately. int main(int argc, char **argv) is a great example. argv will contain argc number of elements, each a char *. C strings are a pointer to a NUL-terminated region of memory. i.e. a sequence (of unknown length) of characters finishing with a char of value 0. strlen manually walks the string each time! 10 / 14 http://soft-dev.org/

Arrays Pointers can be used as arrays. e.g. char ** is an array of strings. But C arrays don t know their length. That must always be passed around separately. int main(int argc, char **argv) is a great example. argv will contain argc number of elements, each a char *. C strings are a pointer to a NUL-terminated region of memory. i.e. a sequence (of unknown length) of characters finishing with a char of value 0. strlen manually walks the string each time! Exercises: 1 Print out all the command-line arguments passed to your program. What is the first parameter? 2 Print out all the command-line arguments passed to your program along with the length of the arguments. 10 / 14 http://soft-dev.org/

Functions C functions have a return type and 0 or more parameters (similar to Java). 11 / 14 http://soft-dev.org/

Functions C functions have a return type and 0 or more parameters (similar to Java). Exercises: 1 Write a ROT13 function which takes in a single char and returns its ROT13 equivalent. Test it with these cases rot13( a ) n and rot13( n ) a. You may assume only lower and upper case characters a za Z will be passed. 2 Print out all command line arguments passed to your program after being ROT13ed. 11 / 14 http://soft-dev.org/

Memory Heap memory is allocated in n bytes with malloc(n). This returns void *, which can be cast to any pointer type you want (e.g. char * c = malloc(n)). Free memory with free(c). You re responsible for freeing memory you allocated. strcat(dst, cpy) appends cpy to dst. 12 / 14 http://soft-dev.org/

Memory Heap memory is allocated in n bytes with malloc(n). This returns void *, which can be cast to any pointer type you want (e.g. char * c = malloc(n)). Free memory with free(c). You re responsible for freeing memory you allocated. strcat(dst, cpy) appends cpy to dst. Exercises: 1 What file do you need to include for malloc? 2 Concatenate all the command line parameters passed to the program into one string in memory. Print out a ROT13 version of the string, then the original string afterwards. Make sure you account for line endings when allocating memory! 12 / 14 http://soft-dev.org/

IO Reading / writing to a file in C can be fiddly need to do your own error handling. 13 / 14 http://soft-dev.org/

IO Reading / writing to a file in C can be fiddly need to do your own error handling. Can read input from stdin with read(stdin_fileno, buf, len) (defined in <unistd.h>) where: STDIN_FILENO is a magic number (on Windows you might need to explicitly change this to 0); buf is a pointer to a buffer of len bytes. 13 / 14 http://soft-dev.org/

IO Reading / writing to a file in C can be fiddly need to do your own error handling. Can read input from stdin with read(stdin_fileno, buf, len) (defined in <unistd.h>) where: STDIN_FILENO is a magic number (on Windows you might need to explicitly change this to 0); buf is a pointer to a buffer of len bytes. Exercises: 1 Change your rot13 function so that it leaves spaces, newlines (etc.) untouched (i.e. it only applies rot13 to a-za-z). 2 Read input from stdin, rot13 it, and print it to stdout. 3 What happens if you chain your program twice? i.e. cat file rot13_stdin rot13_stdin? 13 / 14 http://soft-dev.org/

Post-session exercises Try these (no particular order): You might find this C for Java programmer guide useful. Writing insecure programs in C is easy: read a guide to secure programming in C (e.g. this). Some of the best written despite, oddly, having few comments C code can be found in Unix kernels. e.g. OpenBSD s kernel is a work of art. 14 / 14 http://soft-dev.org/