COMP 2355 Introduction to Systems Programming

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

PRINCIPLES OF OPERATING SYSTEMS

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

Programming in C UVic SEng 265

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

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

COMP 2355 Introduction to Systems Programming

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Syntax and Variables

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

Fundamental of Programming (C)

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

Introduction to Programming Using Java (98-388)

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Fundamentals of Programming

Variables and literals

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

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

Topic 6: A Quick Intro To C

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

A brief introduction to C programming for Java programmers

CSE 374 Programming Concepts & Tools

C: How to Program. Week /Mar/05


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

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

Review of the C Programming Language for Principles of Operating Systems

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

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

Chapter 2 - Introduction to C Programming

A Fast Review of C Essentials Part I

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

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

Programming refresher and intro to C programming

Presented By : Gaurav Juneja

Flow Control. CSC215 Lecture

CS 61c: Great Ideas in Computer Architecture

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

CSE 303 Lecture 8. Intro to C programming

Arrays and Pointers in C. Alan L. Cox

C - Basics, Bitwise Operator. Zhaoguo Wang

High Performance Computing in C and C++

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

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

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

Project 1: How to Make One Dollar

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

PROGRAMMAZIONE I A.A. 2017/2018

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

OBJECT ORIENTED PROGRAMMING

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Programming in C - Part 2

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

Compiling and Running a C Program in Unix

BLM2031 Structured Programming. Zeyneb KURT

Kurt Schmidt. October 30, 2018

Review of the C Programming Language

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

Computer Systems Principles. C Pointers

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Programming Review CSC 4320/6320

ELEC 377 C Programming Tutorial. ELEC Operating Systems

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

Introduction to C++ with content from

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS61C : Machine Structures

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Programming in C and C++

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

EMBEDDED SYSTEMS PROGRAMMING Language Basics

C Syntax Out: 15 September, 1995

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

just a ((somewhat) safer) dialect.

CMPE-013/L. Introduction to C Programming

C-LANGUAGE CURRICULAM

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

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

IV Unit Second Part STRUCTURES

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Chapter 11 Introduction to Programming in C

Transcription:

COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1

Functions Similar to (static) methods in Java without the class: int f(int a, int b) { return a + b; } C always uses call-by-value. The C standard allows the compiler to evaluate arguments in any order. 2

main Any C program has one and only one main method: #include <stdio.h> int main(int argc, char ** argv) { printf("hello World!\n"); return 0; } 3

Types Primitive Types Compound Types Pointers and arrays (Lectures 4-6) Function Types (Lecture 10) 4

Syntax: BASE-TYPE * Pointers Similar to references in Java 5

Syntax: BASE-TYPE [] Arrays Simple example: int b[5]; b[3] = 42; 6

Primitive Types void char short int int long long long float double 7

void Used to indicate no return value or no argument Also used for pointers to data of unknown or arbitrary type using void * 8

char Used to represent characters In Java, 16 bits, in C always 8 bits! Character-set is not defined! Applications most often use either UTF-8 or system default char is also a number, so you can do a + 5 9

signed char Number between -128 and 127 Could be identical to char, depending on platform! Use if you need a char that is signed (rare) or a signed 7-bit number 10

unsigned char Number between 0 and 255 Could be identical to char, depending on platform! Use if you need to do arithmetic that relies on char being unsigned or unsigned 8-bit number 11

int signed number On all modern platforms 32 bit (but used to be 16 for some 80286 implementations) Range from 2 31 to 2 31 1 (2-complement) Always identical to signed int unsigned int also exists (very useful!) 12

signed number, 16 bits short int Range from 2 15 to 2 15 1 (2-complement) Always identical to signed short and signed short int and short unsigned short int also exists (useful) 13

long signed number, number of bits corresponds to address bus width of the platform So 32 bit on IA32, 64 bit on IA64 and AMD64 Useful if you need something of the size of your address space (maximum array length, storing pointers as integers, etc.) unsigned long also exists (can be good for array indices in scientific computing) 14

long long signed number, 64 bits Range from 2 63 to 2 63 1 (2-complement) unsigned long long also exists (64-bit, unsigned) 15

float 32-bit floating point number Similar to Java s float However, unlike Java, floating point operations are platform-dependent On most modern platforms for almost all operations the result is exactly the same as in Java 16

double 64-bit floating point number 17

enum struct union User-Defined and Compound Types 18

enum enum DayOfWeek { MO, TU, WE, TH, FR, SA, SU }; Introduces both new type enum DayOfWeek and constants MO, TU, WE, TH, FR, SA, SU Can be used in switch statement: switch (dow) { case SA: case SU: printf("freedom"); break; default: printf("work"); } 19

enums are ints enum Coin { CENT = 1, NICKEL = 5, DIME = 10, QUARTER = 25, DOLLAR = 100, }; int main(int main, char ** argv) { enum Coin c = DOLLAR + QUARTER + CENT; printf("%u\n", c); } 20

struct Structs are like Java classes without constructors or methods: #define TUITION 10000 struct Student { unsigned char age; int assets; unsigned short credit_hours; }; struct Student alice; void do_register() { alice.assets -= TUITION; alice.credit_hours += 4; } 21

Initializing structs struct Student { unsigned char age; int assets; unsigned short credit_hours; }; int main() { struct Student alice = { 20, 10000, 48 }; printf("alice owns $%d\n", alice.assets); return 0; } 22

Call-by-value & struct void do_register(struct Student s) { s.asset_value -= TUITION; s.credit_hours += 4; } int main(int main, char ** argv) { struct Student alice = { 20, 10000, 48 }; do_register(alice); printf("alice owns $%d\n", alice.asset_value); return 0; } 23

Syntactically like struct union Memory is shared between all members union Fun { unsigned int data; char name[8]; } int main(int argc, char** argv) { union Fun f; f.data = 0x46554E00; printf("unions are %s?\n", f.name); return 0; } 24

Fun! struct IntOrFloat { enum { IOF_INT, IOF_FLOAT } type; union { int i; float f; } value; }; int f() { struct IntOrFloat val; val.type = IOF_INT; val.value.f = 3.14; printf("%d\n", val.value.i); } 25

Bitfields Bitfields can be defined as members of a struct: struct Datum { unsigned int day:5, month:4, year:7; } 26

typedef typedef is a way to introduce a new name for a type. Examples: typedef unsigned int uint32; typedef float real; typedef struct complex { real re; real im; } complex; typedef struct { real re; real im; } complex; typedef enum { YES, NO } decision; 27

Very similar to Java. Operators Make sure you know: 5&3 vs. 1&&5 and 1 2 vs. 1 2, 2 << 4 vs. 4 >> 2 New operator sizeof returns size of a value, variable or type in bytes. C does not have new as a keyword. C has additional operators for pointers (*p, a->f, &v) which we will discuss in lecture 6. 28

sizeof union Fun { unsigned int data; char[8] name; } int main(int argc, char ** argv) { union Fun f; printf("sizeof(union Fun)=%u sizeof(unsigned int)=%u" " sizeof(f)=%u\n", sizeof(union Fun), sizeof(unsigned int), sizeof(f)); return 0; } 29

IF if (expression) s1; else s1; C has no booleans! expression can be a pointer: true not NULL expression can be a number: true not zero 30

The Ternary Operator?: Same as in Java, except C has no booleans int main() { int a = 4; int b = 5; printf((a > b)? "YES" : "NO"); return 0; } 31

Just like in Java SWITCH Use gcc -Wall to get a warning for switches on enums if some case is missing! switch (dow) { case SA: case SU: printf("freedom"); break; case MO, TU, TH, FR: printf("work"); } 32

WHILE int main(int argc, char ** argv) { while (1) { printf("1"); while (1) { printf("2"); if (0) continue; // NO labels allowed! if (1) break; // NO labels allowed! } if (1) break; // NO labels allowed! printf("3"); } printf("4"); return 0; } 33

Just like in Java DO-WHILE Commonly used in macros (lecture 3) int main(int argc, char** argv) { do { printf("1"); } while (0); } 34

FOR Syntax: for (INIT;COND;LOOP) BODY Examples: unsigned int i; for (i=0;i<10;i++) {... } for (i=9;i>=0;i--) {... } i = 10; for (;i!= 0;) {...; i--; } 35

for is not just for integers: FOR struct List { struct List * next; }; struct List * head =...; struct List * pos; for (pos=head;pos!=null;pos=pos->next) {... } for (pos=head;pos;) {... ; pos=pos->next; } 36

FOR and WHILE for and while are equivalent: struct List { struct List * next; }; struct List * head =...; struct List * pos; pos = head; while (pos!= NULL) {... pos = pos->next; } 37

FOR for(;;) {...} is the same as while(1) {...} It is better style to use while(1) Use for for iterations over arrays (forwards, backwards) Use while for almost everything else 38

GOTO int main(int argc, char** argv) { int fd; char buf[100]; int have; fd = open("/etc/passwd"); if (-1 == (have = read(fd, buf, sizeof(buf)))) goto DONE; process(buf, have); DONE: close(fd); return 0; } 39

GOTO Some programmers consider goto harmful to structured programming 1 Java reserves the goto keyword, but does not allow its use Use goto only for clean up code in C or to break out of nested loops 1 E. Dijkstra: Go To Statement Considered Harmful 40

In Java int f() { OUTER: while (true) { printf("1"); while (true) { printf("2"); if (true) break OUTER; } printf("3"); } return 0; } 41

In C int f() { while (1) { printf("1"); while (1) { printf("2"); if (1) goto EXIT; } printf("3"); } EXIT: return 0; } 42

Questions? 43