Lectures 5-6: Introduction to C

Similar documents
Lectures 5-6: Introduction to C

Chapter IV Introduction to C for Java programmers

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

PRINCIPLES OF OPERATING SYSTEMS

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

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

C programming basics T3-1 -

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 61c: Great Ideas in Computer Architecture

Arrays and Pointers (part 1)

Kurt Schmidt. October 30, 2018

CSCI 171 Chapter Outlines

EL2310 Scientific Programming

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Arrays and Pointers (part 1)

Programming refresher and intro to C programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CMPE-013/L. Introduction to C Programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

[0569] p 0318 garbage

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

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

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

C Programming Review CSC 4320/6320

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

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

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

Contents. A Review of C language. Visual C Visual C++ 6.0

Pointers, Dynamic Data, and Reference Types

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

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Arrays and Pointers in C. Alan L. Cox

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

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

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

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

For instance, we can declare an array of five ints like this: int numbers[5];

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Procedural programming with C

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Topic 6: A Quick Intro To C

COP 3223 Final Review

Compiling and Running a C Program in Unix

ECEN 449 Microprocessor System Design. Review of C Programming

Computer Systems Lecture 9

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

C Syntax Out: 15 September, 1995

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

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

211: Computer Architecture Summer 2016

Lecture 3: C Programm

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Computational Methods of Scientific Programming Fall 2007

COP 3223 Final Review

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

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

Fundamental of Programming (C)

A Fast Review of C Essentials Part I

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

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

Lecture 03 Bits, Bytes and Data Types

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Lecture 2: C Programm

C introduction: part 1

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Homework #3 CS2255 Fall 2012

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

Advanced C Programming and Introduction to Data Structures

EMBEDDED SYSTEMS PROGRAMMING Language Basics

CSC231 C Tutorial Fall 2018 Introduction to C

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

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

Model Viva Questions for Programming in C lab

Memory, Arrays & Pointers

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

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS 61c: Great Ideas in Computer Architecture

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

EL6483: Brief Overview of C Programming Language

edunepal_info

CS349/SE382 A1 C Programming Tutorial

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

Transcription:

Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most of the syntax is the same Most statements, expressions are the same Inf2C (Computer Systems) - 2009-2010 1

Outline Major differences with Java A simple program; how to compile and run Data-types and variables The preprocessor Composite data structures Arrays and strings Pointers Inf2C (Computer Systems) - 2009-2010 2

Major differences with Java C is not object oriented C programs are collections of functions, the equivalent of Java methods Execution starts from function main C is not interpreted A C program is compiled into an executable machine code program, which runs directly on the processor Java programs are compiled into a byte code, which is read and executed by the Java interpreter, another program Inf2C (Computer Systems) - 2009-2010 3

C is less safe Run-time errors are not caught in C The Java interpreter catches these errors before they are executed by the processor C run-time errors happen for real and the program crashes The C compiler trusts the programmer! Many mistakes go un-noticed, causing run-time errors Inf2C (Computer Systems) - 2009-2010 4

Memory management is different Java uses dynamic memory management for all objects E.g. arrays can change size while program runs Memory space is released automatically by garbage collection C data structures are allocated statically, i.e. before execution starts The programmer must write extra code to manage dynamically-changing data structures Memory space released explicitly Inf2C (Computer Systems) - 2009-2010 5

C has pointers Pointers are special variables that reference (or point to) another variable Similar to Java references We have already seen pointers in assembly: lw $t1,0($s2) $s2 is a pointer C pointers are the same thing! (more later) Inf2C (Computer Systems) - 2009-2010 6

The hello world program #include<stdio.h> /* This is a (multi-line) comment */ int main(void) { // This is a comment too } Linux/DICE shell commands Compile: gcc hello.c printf("hello world!\n"); return 0; Run:./a.out Inf2C (Computer Systems) - 2009-2010 7

Built-in data types The usual basic data types are there: char 8 bits short 16 int 16, 32, 64 (same as machine word size) long 32, 64 float 32 Bit sizes are machine dependent Unlike Java where an int is always 32 bits Normally signed, unsigned available too No boolean type exists for any number (int, char, ): 0 false, other true Inf2C (Computer Systems) - 2009-2010 8

Categories of variables Global variables Declared outside a function Accessible by any function in the same file Accessible in other files if declared external in those files Declare static to hide from other files Local variables Declared inside a function (before the statements) Not available outside function Different calls of the same function have separate variables Inf2C (Computer Systems) - 2009-2010 9

The C pre-processor: cpp Includes imports header files Declarations for variables, functions, Text substitution, e.g. define constants #define NAME value Macros (inline functions) #define MAX(X,Y) (X>Y? X:Y) Conditional compilation #ifdef DEBUG printf( Debugging message ) #endif Inf2C (Computer Systems) - 2009-2010 10

Composite data structures - struct Structures objects without methods: struct point { int x, y; } p1; struct point p2; To access a structure component, use the struct member operator. p1.x = 2; Structures are not objects Whole struct is copied when passed to a function Java passes a reference in this case Inf2C (Computer Systems) - 2009-2010 11

Composite data structures - union Unions declared and used similarly to structures: union geomobject { struct circle; struct rectangle; } g_obj; But all variables inside a union overlap in memory, Space is reserved for the largest of them, not all The same memory space can be interpreted in multiple ways Inf2C (Computer Systems) - 2009-2010 12

In memory: structures v. unions struct x{ int var1; int var2; } sx; union x{ int var1; int var2; } ux; var1 var2 sx var1/var2 ux sizeof(sx) 8 sizeof(ux) 4 Inf2C (Computer Systems) - 2009-2010 13

User-defined types Define names for new or built-in types typedef <type> <name>; Example: typedef unsigned char byte; typedef struct { struct point p; int rad; } circle;... circle c1, c2; Inf2C (Computer Systems) - 2009-2010 14

Arrays Syntax of C arrays similar to Java, but the rules are different Size is fixed at declaration, when memory space is allocated for the array, e.g.: int n[] = {5, 8, 10}; // size fixed to 3 circle c[4]; // array of structs Array bounds are not checked Functions cannot return arrays But can be passed as parameters Must use pointers to do any dynamic memory allocation in C Inf2C (Computer Systems) - 2009-2010 15

Strings C strings are simply arrays of type char Encoded in 8bits using ASCII They end with '\0', the null character char s[10]; // up to 9 characters long String initialisation char s[10] = string ; // '\0 implied Char s1[] = another string ; Usual C rule for arrays apply: Cannot store more chars than reserved at declaration But bounds are not checked! Inf2C (Computer Systems) - 2009-2010 16

Strings common operations Assignment: strcpy(s, string ); Length: strlen(s) To get the 6 th character: s[5] First char at position 0, as in Java arrays Comparison, strcmp(s1, s2) returns: 0 when equal Negative number when lexicographically s1 < s2 Positive when s1>s2 Must #include<string.h> to call the functions Type: man string to see what s available Inf2C (Computer Systems) - 2009-2010 17

Pointers We have seen pointers in assembly: lw $t1,0($s2) $s2 points to the location in memory where the real data is kept $s2 is a register, but there s nothing stopping us to have pointers stored in memory like normal variables Address 0x104 0x100 data 0x100 reg $s2 0x100 Inf2C (Computer Systems) - 2009-2010 18

C pointers A C pointer is a variable that holds the address of a piece of data Declaration: int *p; // p is a pointer to an int The compiler must know what data type the pointer points to Basic pointer usage: p = &i; // p points to i now *p = 5; // *p is another name for i & - address of, * dereference operator Inf2C (Computer Systems) - 2009-2010 19

Pointers as function arguments In C (and Java, for primitive types) function arguments are passed by value Function gets own copy of the arguments values How could then a function modify the original argument variables? Pass pointers to the variables Inf2C (Computer Systems) - 2009-2010 20

Example the swap function void swap_wrong(int a, int b) { int t=a; a=b; b=t; } swap_wrong swaps the local variables a,b which are unknown outside of the function void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; } Function call: swap(&x, &y); Inf2C (Computer Systems) - 2009-2010 21

Pointer arithmetic and arrays C allows arithmetic on pointers: int a[10]; int *p; p = &a[0]; // p points to a[0] p+1 points to a[1] Note that &a[1] = &a[0]+4 The compiler multiplies +1 with the data type size In general: p+i points to a[i], *(p+i) is a[i] Even *(a+i) p[i] are allowed but cannot change what a points to. It s not a variable Inf2C (Computer Systems) - 2009-2010 22

More pointer arithmetic Common expressions: *p++ use value pointed by p, make p point to next element *++p as above, but increment p first (*p)++ increment value pointed by p, p is unchanged Special value NULL used to show that a pointer is not pointing to anything NULL is typically 0, so statements like if (!p) are common Dereferencing a NULL pointer is a very common cause of C program crashes Inf2C (Computer Systems) - 2009-2010 23

Example pointer arithmetic Return the length of a string: int strlen(char *s) { char *p=s; while (*s++!='\0') ; return s-p; } Argument/variable s is local, so we can change it Pointer increment, dereference and comparison all in one! No statement in the loop body Note pointer subtraction at return statement Inf2C (Computer Systems) - 2009-2010 24

Dynamic memory allocation Pointers are not much use with statically allocated data Library function malloc allocates a chunk of memory at run time and returns the address int *p; if ((p = malloc(n*sizeof(int))) == NULL) { // Error }... free(p); // release the allocated memory Inf2C (Computer Systems) - 2009-2010 25

Pointers to pointers Consider an array of strings: char *strtable[10]; The strings are dynamically allocated any size But the table size is fixed to 10 strings How can we have both dynamically changing in size at runtime? char **strtable; Since a pointer is a variable, we could have another pointer pointing to it: pointer to pointer! Inf2C (Computer Systems) - 2009-2010 26

Pointers to pointers - details Space must be allocated both for the table and the strings themselves char **strtable; strtable = malloc(n*sizeof(char *)); for (i=0; i < n; i++) {... // s gets a string of length l *(strtable+i) = malloc(l*sizeof(char)); strcpy(strtable[i], s); } // strtable[i][j] == *(*(strtable+i)+j) Inf2C (Computer Systems) - 2009-2010 27

That s all folks Not all C features have been covered, but this introduction should be enough to get you started Useful things to learn on your own: Standard input/output: printf, scanf, getc, File handling: fopen, fscanf, fprintf, Inf2C (Computer Systems) - 2009-2010 28