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

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

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

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

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

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

COMP1917: Computing 1 1. Introduction

COMP1917: Computing 1 1. Introduction

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

program structure declarations and definitions expressions and statements more standard I/O

PROGRAMMAZIONE I A.A. 2017/2018

CS Prof J.P.Morrison

Programming in C and C++

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

by Pearson Education, Inc. All Rights Reserved.

Preview from Notesale.co.uk Page 6 of 52

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

EL2310 Scientific Programming

CS240: Programming in C

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

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

Programming I Laboratory - lesson 01

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

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

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

Programming. Data Structure

Problem Set 1: Unix Commands 1

Programming in C and C++

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

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

PRINCIPLES OF OPERATING SYSTEMS

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CSE 303 Lecture 8. Intro to C programming

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

Chapter 2, Part I Introduction to C Programming

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

Chapter 2. Basics of Program Writing

1. The Mac Environment in Sierra Hall 1242

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

Topic 6: A Quick Intro To C

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

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

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

CSC231 C Tutorial Fall 2018 Introduction to C

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

A college level course syllabus can be constructed around this book and its examples, suitable for both hardware and software engineers.

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

Programming in C UVic SEng 265

1. The Mac Environment in SIE 1222

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

Object-oriented programming in...

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

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

Hands-On Ethical Hacking and Network Defense 2 nd edition

6.S096 Lecture 1 Introduction to C

Course Information and Introduction

Object-Oriented Programming

CS 113: Introduction to

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

CS Programming In C

Introduction to Linux

CS 253: Intro to Systems Programming 1/21

EC 413 Computer Organization

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

Chapter 1 & 2 Introduction to C Language

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

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

Getting started with UNIX/Linux for G51PRG and G51CSA

2 Compiling a C program

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

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

CS201 - Lecture 1 The C Programming Language

C Language, Token, Keywords, Constant, variable

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

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

Assignment 1: Communicating with Programs

Motivation was to facilitate development of systems software, especially OS development.

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

Unit 13. Linux Operating System Debugging Programs

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

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Welcome to... CS113: Introduction to C

Lab 1 Introduction to UNIX and C

1007 Imperative Programming Part II

Computers Programming Course 5. Iulian Năstac

Introduction to C Programming

COMP s1 Lecture 1

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

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

EE 209: Programming Structures for Electrical Engineering

Lecture 03 Bits, Bytes and Data Types

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Motivation was to facilitate development of systems software, especially OS development.

Computer Programming

Transcription:

week 1 introduction Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20

class format 30 minutes (give or take a few) presentation 60 minutes (give or take a few) practice : finish the practical exercises the only way to learn a language is to use it recommended books Kernighan & Ritchie, The C Programming Language, second edition concise, no-nonsense explanation from the inventor of C Kernighan & Pike, The Practice of Programming deep wisdom and great advice about writing programs in C, C++ and Java resources on the EPL network, look for epl1server relevant material is in: Shared/Books/Programming/C Ian Piumarta 2 / 20

adjective denoting the mood of a verb that expresses a command or exhortation, as in come here! programs contain explicit commands about how to manipulate data are closely related to the way the machine language manipulates data embrace assignment and mutable state have simple and explicit control flow emphasise the interdependence of data structures and algorithms we will learn C, one of the most successful languages Ian Piumarta 3 / 20

of C once upon a time, operating systems were written entirely in machine code hard to maintain, harder to port to new hardware, slow to evolve UNIX OS (invented by Ken Thompson) first written in machine language new computers (with new machine code) were being developed rapidly porting was painful: invent a portable language for OS development? Ken Thompson Dennis Ritchie C (invented by Dennis Ritchie) was used to re-implement UNIX portably only a very small amount of non-portable machine code remained Linux, Darwin (Mac), Windows, etc., OS kernels are all written in C used often for embedded and systems compilers, interpreters, game platforms, libraries, databases,... Ian Piumarta 4 / 20

of C general-purpose, low-level language basic data types are the same as those implemented by hardware: characters, numbers, addresses operations on data closely resemble typical machine instructions simple control structures: conditionals, loops, functions no composite objects no strings, lists, arrays, dictionaries, etc. (if you want them, you must implement them yourself) no memory management the programmer must allocate and de-allocate memory explicitly no input/output facilities terminal, file, network I/O provided by libraries library functions implement OS services (system calls) directly Ian Piumarta 5 / 20

C is small language description can be brief basic language can be learned quickly competent programmers understand and use the entire language C is close to the OS good at file manipulation, network communication, etc. popular for writing servers, databases C is close to the machine hardware I/O devices directly desktop: operating system kernels, UI frameworks embedded: physical interfaces, timing, control, communication, configuration C is easy to mix with machine code for optimisation high-performance applications: Photoshop, AI/ML, etc. Ian Piumarta 6 / 20

: edit, compile, run, (debug,) repeat C is a compiled language between editing and running, you have to compile your program source file #include <stdio.h> int main() { prinf("yo dude\n"); return 0; } executable file 0110010100100100 1001010100110010 0100100101010010 1001010110010010 0101001010110101 1101100100010101 edit compile run debug cycle advantages: some kinds of error are caught at compile time undefined variables, missing return values, wrong number of arguments,... disadvantages: you cannot modify and resume a program while debugging Ian Piumarta 7 / 20

everyone s first program: hello world the first C program that everyone writes is (traditionally) always the same: print the string hello, world on the terminal #include <stdio.h> int main() { printf("hello, world\n"); return 0; } C programs contain just two things: variables and functions variables store values used during computation functions contain statements specifying what that computation is our program has no variables, and just one function (called main) functions can have any name you like, but the name main has special significance when you run the program, it starts executing at main 1 1 Ian Piumarta compare to Java, which begins execution at the method called main 8 / 20

everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } our main function uses a library function called printf printf prints a string on the terminal it is part of the standard I/O library the first line of our program, #include <stdio.h> tells the C compiler to read the contents of the file stdio.h the stdio.h file contains information about the standard I/O library including how to call the printf function (without that information, printf cannot be used safely) Ian Piumarta 9 / 20

everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } the second line declares our main function first comes the kind of value the function returns in the case of main, an integer result is returned next comes the name of the function followed by a list of parameters that the function expects the parameter list appears inside parentheses in this case, main expects no parameters and so the list is empty Ian Piumarta 10 / 20

everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } the statements of a function appear inside curly braces: {... } there is only one statement in our function printf("hello, world\n"); this is a function call the function name appears first a parenthesised list of arguments follows (if there are no arguments, an empty list must still be written) the semicolon terminates the function call statement in this example, a single argument is passed to printf the string that we want to print within that string is an escape sequence \n these two characters represent a single newline character in the string Ian Piumarta 11 / 20

everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } main returns an integer result the result of main() is always an integer the result returned from main indicates if there were any errors returning 0 from main() indicates there were no errors during execution returning non-0 indicates there was an error the value can indicate what kind of error Ian Piumarta 12 / 20

compiling the C compiler is called cc just tell it the name of the file to be compiled $ cc hello.c if there are no errors, the C compiler remains completely silent no errors, so no output from cc the executable file produced by the C compiler is called a.out once upon a time, a.out stood for assembler output $ cc hello.c $ ls a.out hello.c Ian Piumarta 13 / 20

running the compiler accepts many command-line options e.g., to change the output filename use: cc -o filename $ cc hello.c -o hw $ ls hw hello.c to run a program, type its name you may have to tell the OS to look for the program in the current directory the current directory is called., so your program is./hw $ cc hello.c -o hw $ ls hw hello.c $./hw hello, world Ian Piumarta 14 / 20

the form (and quality) of vary between compilers on Darwin (MacOS), the compiler gives message that look like this line column number number error message program.c:5:27: error: expected ; after expression printf("hello, world\n") problematic line of source code ˆ marker showing where the error occurred ; possible hint(s) about how to fix it each error message contains: the line number in the source file where the compiler became upset the column number within that line an error message explaining why the compiler is unhappy the actual line of source code that the compiler disapproves of a small marker showing the position where the problem begins corresponding to the column number shown above and, occasionally, a suggestion how to fix your error Ian Piumarta 15 / 20

English translations of some common compiler messages: program.c:3:3: warning: implicitly declaring library function printf you forgot to #include the file declaring printf in this case, the file you forgot to include was stdio.h use the man (manual) command to find out about other functions program.c:3:1: warning: type specifier missing, defaults to int you forgot to indicate the type of a function result or a variable program.c:5:27: error: expected ; after expression you forgot to terminate a statement with a semicolon program.c:5:3: warning: implicit declaration of function print is invalid you are trying to call a function that you did not declare maybe because you misspelled a library function name program.c:6:1: warning: control reaches end of non-void function you forgot to return a value at the end of a function Ian Piumarta 16 / 20

one error in a program can cause a torrent of the first error message points to the real problem the rest are, all caused by the first error I forgot to give this function a return type... f() { } which causes a relevant error message program.c:3:1: warning: type specifier missing, defaults to int f() ˆ and a secondary error message, for a problem that really does not exist program.c:5:1: warning: control reaches end of non-void function } ˆ Ian Piumarta 17 / 20

another example: misspelling printf as print program.c:5:3: warning: implicit declaration of function print is invalid Undefined symbols for architecture x86_64: "_print", referenced from: secondary, caused by my typing mistake _main pro-tip: pay most attention to the first error message only many of the errors following it may be secondary I suggest you ignore (literally) all C compiler after the first one: 1 edit 2 compile 3 fix the first error only 4 repeat immediately from step 2, until there are no more errors Ian Piumarta 18 / 20

specifying an output file name cc sourcefile.c -o outputfile turn on all warning messages (make a habit of doing this!) cc -Wall sourcefile.c -o outputfile include information for the run-time debugger (can be useful, and does no harm) cc -Wall -g sourcefile.c -o outputfile when the program works perfectly, optimise it (for speed or for space) cc -Wall -g -O sourcefile.c -o outputfile -O1 optimises a bit (hardly ever useful) -O2 optimises the normal amount (makes the executable faster) -O3 optimises as much as possible (e.g., for lots of heavy mathematics) -Os optimises for space (makes the executable as small as possible) to see precisely what the compiler is doing, tell it to be verbose cc -v -Wall -g -O sourcefile.c -o outputfile Ian Piumarta 19 / 20

reading The C Programming Language introduction (page 5) section 1.1 (pages 5 8) the rest of chapter 1, if you like it will help you prepare for next week exercises questions 1 8 plus the further exploration questions, if possible they will help you prepare for next week Ian Piumarta 20 / 20