EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

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

COMP1917: Computing 1 1. Introduction

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

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

COMP1917: Computing 1 1. Introduction

CSCI 2132 Software Development. Lecture 8: Introduction to C

EL2310 Scientific Programming

Intermediate Programming, Spring Misha Kazhdan

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

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

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

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

1. The Mac Environment in Sierra Hall 1242

PRINCIPLES OF OPERATING SYSTEMS

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

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

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

Programming in C and C++

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

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

EECS2031 Software Tools

2 Compiling a C program

1. The Mac Environment in SIE 1222

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

CS Prof J.P.Morrison

6.S096 Lecture 1 Introduction to C

Spring 2018 NENG 202 Introduction to Computer Programming

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

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

CSE 303 Lecture 8. Intro to C programming

by Pearson Education, Inc. All Rights Reserved.

CS240: Programming in C

ECE 471 Embedded Systems Lecture 4

Problem Set 1: Unix Commands 1

CS 113: Introduction to

Getting started with UNIX/Linux for G51PRG and G51CSA

Introduction to Cygwin Operating Environment

Introduction to the C-Language and Programming Environment

EC 413 Computer Organization

Introduction to Basis and Practice in Programming

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

Programming in C UVic SEng 265

Parallel Programming Pre-Assignment. Setting up the Software Environment

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

PROGRAMMAZIONE I A.A. 2017/2018

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

CSI33 Data Structures

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Parallel Programming Pre-Assignment. Setting up the Software Environment

IS12 - Introduction to Programming. Lecture 7: Introduction to C

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

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

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

CS 211 Programming I for Engineers

Programming in C and C++

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Structure of this course. C and C++ Past Exam Questions. Text books

The C Programming Language

Course Information and Introduction

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

Chapter 2. Basics of Program Writing

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

CS 253: Intro to Systems Programming 1/21

Fundamentals of Programming. Lecture 1: Introduction to C Programming

Programming the DMCC in C

NYU Poly s Policy on Academic Misconduct:

CSCI 2132 Software Development. Lecture 2: Introduction to UNIX and Unix-like Operating Systems

Presented By : Gaurav Juneja

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

CSC231 C Tutorial Fall 2018 Introduction to C

MEIN 50010: Python Introduction

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

Preview from Notesale.co.uk Page 6 of 52

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018

CS356: Discussion #1 Development Environment. Marco Paolieri

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

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Programming. Data Structure

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Introduction to C Programming

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Assignment 1. CSI Programming Practice, Fall 2011

Systems Programming. The Unix/Linux Operating System

CMSC 246 Systems Programming

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Lecture 1. A. Sahu and S. V. Rao. Indian Institute of Technology Guwahati

CS240: Programming in C

CSE 351. Introduction & Course Tools

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

EE 209: Programming Structures for Electrical Engineering

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

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

Transcription:

Lecture 6: Introduction to C (pronobis@kth.se)

Overview Overview Lecture 6: Introduction to C Roots of C Getting started with C Closer look at Hello World Programming Environment

Schedule Last time (and before): MATLAB Today: Introduction to C - main part of this course Wed, September 19th: Deadline to submit your MATLAB project solutions Thu, September 20th: Project exam

Announcements New materials online: Online courses Books Reference manuals Forums Coding convention guides Linux and Emacs Virtual machine for C/C++ projects is online Homework until Wednesday: Install and run the virtual machine (or use Linux...) Start Emacs Type, compile and run a Hello-world program Check out coding conventions!

Roots of C The roots of C First compiler developed by Dennis Ritchie at Bell Labs (1969-1973) Was based on two languages: BCPL, written by Martin Richards at University of Cambridge B, written by Ken Thompson at Bell Labs in 1970 for the first UNIX system Original C language was known as K&R C (Kernigan & Ritchie C) since the K&R book was the only language specification

Roots of C ANSI C American National Standards Institute (ANSI) formed a committee Aim: to define an unambiguous and machine-independent definition of the language C Committee formed in 1983 Work completed in 1988 Resulted in ANSI C standard Extensions to the standard: C99, C11

Roots of C The C language Developed for UNIX The system and most programs written in C System programming language Constructs map efficiently to machine instructions A replacement for the assembly language Many later languages borrow from C: C#, D, Go, Java, JavaScript, Perl, PHP, Python, Unix C Shell Considered low level language (in contrast to e.g. MATLAB)

Roots of C Types Types: Classify type of data e.g. integer, char, string, etc. Determine the possible type values Machine data types: bits, words (32-bit/64-bit) Compiler maps language data types to machine data types Operators: Interaction between objects of certain types (e.g. +,-)

Roots of C Types Typing systems differ between programming languages Strongly / Weakly typed Unclear definition Restrictions on interaction between data types MATLAB weakly typed C/C++ strongly typed Statically / Dynamically typed Type checking during compile time or run time. Strongly, statically typed languages are more likely to catch errors at compile time while weakly typed languages allow further flexibility.

Getting started with C Learning C Practice! Practice! Practice! Practice! Practice! A very good idea: Define your own little project.

Getting started with C Steps to a running program Write Compile Link Execute From: http://www.physics.drexel.edu/courses/comp Phys/General/C basics/compile.html

Getting started with C Compiling the code Parsing of the statements for syntax Translation of the statements into machine language Setting up the addresses of all variables Optimization of the code (if desired)

Getting started with C Linking Assembles the routines produced during the compilation Resolves missing calls to either language-specific libraries or system-wide functions

Getting started with C Optimization You can tell the compiler to optimize the code Better NOT to optimize until the program runs as expected Optimization changes the code internally for better efficiency Makes debugging much harder! Can typically specify different levels of optimization Optimization can in same cases change behavior of code

Getting started with C Hello world The Hello world program Typically the first program written in all languages First one written in B Input: nothing Output: prints Hello world on the screen

Getting started with C Hello world in C #include <stdio.h> main() { printf( Hello world\n ); }

Getting started with C The gcc compiler GNU - Unix-like OS developed by the GNU Project GNU offers a freely available compiler gcc

Getting started with C Compiling a program gcc hello.c If the program is correct, will produce a binary file: a.out GNU/Linux naming controversy

Getting started with C Running the program in Linux./a.out The prefix./ instructs the system to run the program a.out in the current directory Just like in MATLAB there is a PATH variable that tells the system where to look for programs to run In Unix/Linux systems this PATH does normally not contain the current directory.

Getting started with C Compiler arguments Compiler takes many arguments -o <output filename> -Wall - enable all warnings -O, -O1, -O2, -O3 - optimization level -c <filename.c> - only compile filename.c (not link) -lname - link to library called libname -L<directory> - tell the linker where to find libraries For now let us focus on -o

Getting started with C Compiling a program cont d To create executable hello from hello.c gcc -o hello hello.c

Closer look at Hello World Analysis of the program #include <stdio.h> main() { printf("hello world\n"); } A C program consists of functions and variables (like in MATLAB) Functions are built using statements (like in MATLAB) Program execution starts in the function main Each program must have a main function

Closer look at Hello World Analysis of the program Program starts with #include <stdio.h> Instructs the compiler to include information from the standard library for input and output (I/O) These lines are typically found at the top fo the file The main function can, but does not have to have arguments The statements within a function should be placed between braces

Closer look at Hello World The printf function printf is a command used to print to standard output The argument is a string (enclosed in double quotes) Will see later that it can take more arguments The last character in the string is \n which is C style for the newline character Other hidden characters can be obtained with an escape sequence (\) \t is a tab character

Programming Environment Virtual Machine Can be downloaded from the course materials page Ubuntu Linux guest running inside VirtualBox Preinstalled: gcc/g++/sdl/emacs VirtualBox can be installed in any host OS Go to: www.virtualbox.org, download and install Unpack the VM and use Machine-Add, then Start Use Shared Folders to exchange files with your host OS

Programming Environment Editing files We will use simple text editors, not full IDEs Emacs preferred, but you can use any text editor (e.g. if you prefer to edit text in Windows) Avoid rich text editors (e.g. Word) and save the file as text only Emacs pre-installed inside the VM and can be installed natively in Windows A short introduction to Emacs available from the course materials Use the interactive Emacs tutorial inside Emacs

Programming Environment Compiling in Linux Open the terminal Go to the folder containing source files (cd <path>) Run the compiler (gcc -o hello hello.c) Linux beginner tutorials available in the course materials

Programming Environment Homework Homework until Wednesday: Install and run the virtual machine, or use: Native Linux on your laptop CSC computers Start Emacs Type, compile and run a Hello-world program Check out coding conventions! Wednesday: Continue with C