EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

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

COMP1917: Computing 1 1. Introduction

PRINCIPLES OF OPERATING SYSTEMS

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

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

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

CS Prof J.P.Morrison

COMP1917: Computing 1 1. Introduction

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

Presented By : Gaurav Juneja

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

CSE 303 Lecture 8. Intro to C programming

EC 413 Computer Organization

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

1. The Mac Environment in Sierra Hall 1242

Programming. Data Structure

Work relative to other classes

1. The Mac Environment in SIE 1222

Programming in C and C++

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

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

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

BLM2031 Structured Programming. Zeyneb KURT

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Preview from Notesale.co.uk Page 6 of 52

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

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

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

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

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

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

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

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

A Fast Review of C Essentials Part I

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

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

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

Programming in C UVic SEng 265

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

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

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

Topic 6: A Quick Intro To C

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

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

CSC231 C Tutorial Fall 2018 Introduction to C

Programming in C and C++

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

CSI33 Data Structures

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Introduction to C Language

SWEN-250 Personal SE. Introduction to C

Problem Set 1: Unix Commands 1

Intermediate Programming, Spring Misha Kazhdan

CSE 351. Introduction & Course Tools

Chapter 1 & 2 Introduction to C Language

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

EECS2031 Software Tools

Lecture 3. More About C

2 Compiling a C program

CSC 1107: Structured Programming

PROGRAMMAZIONE I A.A. 2017/2018

The C language. Introductory course #1

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Parallel Programming Pre-Assignment. Setting up the Software Environment

CS240: Programming in C

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

Chapter 2. Basics of Program Writing

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

COMP s1 Lecture 1

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

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

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

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

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Computers Programming Course 5. Iulian Năstac

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CSc Introduction to Computing

Basic Types and Formatted I/O

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

Introduction to the C-Language and Programming Environment

CMSC 246 Systems Programming

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

6.S096 Lecture 1 Introduction to C

Lecture 03 Bits, Bytes and Data Types

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Parallel Programming Pre-Assignment. Setting up the Software Environment

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

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

Intro to Programming 2017

Introduction to C Programming

Intro to C and Binary Numbers 8/27/2007

C Language, Token, Keywords, Constant, variable

Fundamental of C programming. - Ompal Singh

Transcription:

(yaseminb@kth.se)

Overview Overview Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf

Schedule Introduction to C - main part of this course Deadline to submit your MATLAB project solutions: Thu, September 17th, 20:00. Submit a README file to run your code, if there is any issues with your submission, I will contact you and ask for details. Submit a brief report as well.

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: 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 example: https://www.youtube.com/watch?v=tcrfrvnngmu

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

Basic Datatypes and printf Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf

Basic Datatypes and printf C statement A statement in C can be a single line followed by semicolon, or many statements enclosed by braces { }

Basic Datatypes and printf Comments Multi-line comments The compiler will ignore everything between /* and */ Single-line comments (starting from C99) The compiler will ignore the rest of the line after // #include <stdio.h> main() { /* This is a nice comment, is it not! */ printf("hello world\n"); // This line prints }

Basic Datatypes and printf Data types There are only a few data types in C char: character - a single byte int: integer float: floating point number double: double precision floating point Can add qualifiers to get versions of these short int: fewer bytes integer (maybe, depends on platform) long int: integer with more bytes (maybe, depends on platform) unsigned int: unsigned version (i.e. min value 0) signed int: signed version (the default) More at http://en.wikipedia.org/wiki/c data types

Basic Datatypes and printf Variable declarations In Matlab we could just use a variable, but not in C In C you need to declare the variables before you use them Old C: typically at the head of the function (or block) C99: Can be as close to where they are used as possible Declaring: <type> <variable name> int some number; int anumber, anothernumber, yetanothernumber; int some number=3;

Basic Datatypes and printf printf You can use printf to print not only for strings but the value of variables Ex: printf("this is iteration %d and the error is %f\n", iter, err); To indicate that you want to print out a variable value you use the % character followed by a specification for what variable that is %d to print integer %f to print floating point

Basic Datatypes and printf printf cont d You can specify how many characters should be printed (at least) printf("the number of participants is %6d\n", dist) Will print at least 6 character Ex: The number of participants is 4 Can be used to align things

Basic Datatypes and printf printf cont d You can specify how many characters after a decimal point you want (at least) printf("the distance is %.2fm\n", dist) Will print 2 decimals Ex: The distance is 4.00m Can combine number of characters and number of decimals printf("the distance is %6.2fm\n", dist) Will print 6 characters and 2 decimals Ex: The distance is 4.00m Notice that the dot counts as a character Can pad with zeros printf("the distance is %06.2fm\n", dist) Ex: The distance is 004.00m

Basic Datatypes and printf printf cont d More switches to printf %o octal %x hexadecimal %c character %s character string %% to get % itself www.cplusplus.com/reference/clibrary/cstdio/printf/ or man 3 printf in Linux

Basic Datatypes and printf Task 1 Declare an integer and print this integer in decimal, octal and hexadecimal form

Basic Datatypes and printf sizeof Different types have different sizes The function sizeof can be used to get the size, i.e. number of bytes of a variable or data type Syntax: sizeof(<variable/data type>) Is an operator not a function Relates data types to the Machine type

Basic Datatypes and printf Task 2 Write a program that lists the number of bytes for some of the basic data types Is there a difference between short int, int and long int on your machine? Do NOT assume the size of a type

Basic Datatypes and printf Homework Homework: 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! Thursday: Continue with C

Basic Datatypes and printf Questions regarding the Matlab project?