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

Similar documents
Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

EL2310 Scientific Programming

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

EL2310 Scientific Programming

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

PRINCIPLES OF OPERATING SYSTEMS

Programming Language Basics

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

Chapter 11 Introduction to Programming in C

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Chapter 11 Introduction to Programming in C

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 11 Introduction to Programming in C

C introduction: part 1

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

Chapter 11 Introduction to Programming in C

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

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

CS61, Fall 2012 Section 2 Notes

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

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

Programming in C UVic SEng 265

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

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

CSE 374 Programming Concepts & Tools

EL2310 Scientific Programming

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

CS240: Programming in C

Algorithms and Programming I. Lecture#12 Spring 2015

COMP 2355 Introduction to Systems Programming

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

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

CS 11 java track: lecture 1

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

A Fast Review of C Essentials Part I

Programming in C. Pointers and Arrays

CSE 303 Lecture 8. Intro to C programming

CS 253: Intro to Systems Programming 1/21

CS 177 Recitation. Week 1 Intro to Java

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

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

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

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

Intermediate Programming, Spring Misha Kazhdan

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

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

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

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

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BSM540 Basics of C Language

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

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

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

Chapter 11 Introduction to Programming in C

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

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

EC 413 Computer Organization

COMP s1 Lecture 1

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

CS61C : Machine Structures

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

BLM2031 Structured Programming. Zeyneb KURT

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

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

Kurt Schmidt. October 30, 2018

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

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

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

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

Course Outline Introduction to C-Programming

Chapter 1 & 2 Introduction to C Language

Topics Introduction to Microprocessors

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

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

POINTER AND ARRAY SUNU WIBIRAMA

AN INTRODUCTION PROGRAMMING. Simon Long

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Work relative to other classes

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

CS 61c: Great Ideas in Computer Architecture

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

An overview of Java, Data types and variables

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Full file at C How to Program, 6/e Multiple Choice Test Bank

Intro to Programming 2017

HW1 due Monday by 9:30am Assignment online, submission details to come

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Transcription:

CS 220: Introduction to Parallel Computing Beginning C Lecture 2

Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 2

Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 3

Architectural Differences C is a bit different than Java or Python It is compiled to machine code Java runs on a virtual machine (JVM) Python is interpreted (translated to machine code on the fly) We can achieve better performance with C, but are also given more responsibility Memory management is up to us (no automatic garbage collection) 8/25/17 CS 220: Parallel Computing 4

C Advantages It is fairly simple: the language does not have a multitude of features Coming from Java, the syntax is familiar In cases where we operate close to the hardware, it can be much easier to implement than the equivalent Java/Python/etc. Wide use for systems programming Want to contribute to the Linux kernel? It s written in C (including the drivers) Performance 8/25/17 CS 220: Parallel Computing 5

C Disadvantages Much less functionality is available in the standard library than other languages Memory leaks Segmentation faults (invalid memory access) No objects If you re used to object-oriented programming in Java or Python, C will make you rethink your program flow 8/25/17 CS 220: Parallel Computing 6

Hello World in C #include <stdio.h> int main(void) { printf("hello, World!\n"); return 0; } 8/25/17 CS 220: Parallel Computing 7

Writing C Programs Using an IDE (like Eclipse, IntelliJ, etc) is less common in the C world Many C developers prefer to use a text editor and a terminal to write their programs Text editor: edit, save Terminal: compile, run There s a tutorial on the course schedule page for setting up your editor and C compiler 8/25/17 CS 220: Parallel Computing 8

Writing C Programs 8/25/17 CS 220: Parallel Computing 9

Testing Your Code Very Important: compile and test your code on the department machines before turning it in We can t grade it on your specific laptop C compilers can implement the C specification differently The standards committee releases new specifications periodically In fact, in olden times, there were several different, incompatible versions of C 8/25/17 CS 220: Parallel Computing 10

Windows One last tip: developing C programs on Windows can be tricky What works on Windows may not work at all on the department Linux machines The course website has information for setting up a Linux virtual machine on Windows There are also other options available use them at your own risk! 8/25/17 CS 220: Parallel Computing 11

Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 12

A Program in C Spot the Differences #include <stdio.h> void say_hello(int times); int main(int argc, char *argv[]) { say_hello(6); return 0; } Output: Hello world! (#1) Hello world! (#2) Hello world! (#3) Hello world! (#4) Hello world! (#5) Hello world! (#6) /* Say Something */ void say_hello(int times) { int i; for (i = 1; i <= times; ++i) { printf("hello world! (#%d)\n", i); } } 8/25/17 CS 220: Parallel Computing 13

Differences from Java/Python Whitespace is mostly ignored Semicolons are required Comments: /* */ and // Including libraries looks a bit different No public/private etc. access modifiers Forward declarations (prototypes) But, there are a lot of similarities 8/25/17 CS 220: Parallel Computing 14

Similarities Arithmetic is mostly the same We use &&,, and!= instead of and, or and not If, then, else Loops Switches 8/25/17 CS 220: Parallel Computing 15

Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 16

Compilation Something you may not be familiar with is compiling your programs Who has used javac and java from the command line? Who presses the Run button in Eclipse/IntelliJ? With C, the compiler is very important It takes your C code and transforms it into machine code to produce a program binary Runs natively on the hardware no VM/interpreter 8/25/17 CS 220: Parallel Computing 17

Program Binaries After you ve compiled your program and produced an executable binary, you can run it! You can even copy your program to other similar machines and it will run Unlike Java/Python, you don t have to install anything first However, note similar above the binaries are platform- and architecture-specific 8/25/17 CS 220: Parallel Computing 18

Platform Differences Your compiled C program will generally only run on its target architecture and platform If you compiled on a Mac, then the binary won t work on Linux If you compile on an x86-based processor (Intel, AMD), the binary won t work on ARM (Qualcomm, Apple, Samsung mobile CPUs, Raspberry Pi ) Java/Python don t have this limitation! 8/25/17 CS 220: Parallel Computing 19

Phases of C Compilation 1. Preprocessing: perform text substitution, include files, and define macros. The first pass of compilation. Directives begin with a # 2. Translation: preprocessed code is converted to machine language (also known as object code) 3. Linking: your code likely uses external routines (for example, printf from stdio.h). In this phase, libraries are added to your code 1/24/18 CS 220: Parallel Computing 20

The C Preprocessor We ve seen include statements: #include <stdio.h> Another common use case is constants: #define PI 3.14159 Note: no equals sign. This is just simple text replacement! You can also define macros that essentially cut and paste reusable code snippets into your work 1/24/18 CS 220: Parallel Computing 21

Include Paths There are two types of includes: #include <blah> #include blah When angle brackets are used, the system-wide library paths are searched With quotes, you are specifying a local path (in the same folder as your code) In this class, you ll only need to worry about the system libraries 1/24/18 CS 220: Parallel Computing 22

Compiling from the Command Line gcc my_code.c./a.out Produces and runs a binary file called a.out You can also turn on error messages: gcc -Wall my_code.c And give your program a name: gcc -Wall my_code.c -o my_prog 1/24/18 CS 220: Parallel Computing 23

Making Diagnostics Readable The last command line option to gcc I recommend is -fdiagnostics-color. gcc -fdiagnostics-color -Wall my_code.c -o out.exe my_code.c:9:6: warning: conflicting types for say_hello [enabled by default] void say_hello(int times) { ^ first.c:5:5: note: previous implicit declaration of say_hello was here say_hello(6); ^ 1/24/18 CS 220: Parallel Computing 24

Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 25

Basic Input/Output Requires the standard I/O library: #include <stdio.h> Printing text: printf("hi there!\n"); We can also print out variables with format strings 8/25/17 CS 220: Parallel Computing 26

Format Strings (1/2) Let s look at a print example: printf("<format>", var1, var2,, varn); The variable list is optional: printf("hello world!\n"); Note that we need to provide the newline character This style of I/O tells the C compiler what and where you want to read or write 1/24/18 CS 220: Parallel Computing 27

Format Strings (2/2) The C compiler looks through your format string to determine the order to print and in what format: printf("hello %s, it is January %d.", "Alice", 24); Hello Alice, it is January 24. There are several format specifiers available: %d or %i integer %s string %f floating point And many more 1/24/18 CS 220: Parallel Computing 28

GitHub If you haven t already, register for an account on GitHub Visit the course website for homework instructions See: Assignments à Homework The schedule page also has some information about using git 8/25/17 CS 220: Parallel Computing 29