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

Similar documents
Compiling and Running a C Program in Unix

PRINCIPLES OF OPERATING SYSTEMS

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

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

Lectures 5-6: Introduction to C

Topic 6: A Quick Intro To C

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

Chapter IV Introduction to C for Java programmers

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

Programming refresher and intro to C programming

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

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

Lectures 5-6: Introduction to C

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

CSE 303 Lecture 8. Intro to C programming

C Syntax Out: 15 September, 1995

COMP 2355 Introduction to Systems Programming

Lecture 3: C Programm

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

CGS 3460 Summer 07 Midterm Exam

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

Programming in C UVic SEng 265

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

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Problem Set 1: Unix Commands 1

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

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

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Introduction to Programming Using Java (98-388)

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

EL2310 Scientific Programming

Array. Prepared By - Rifat Shahriyar

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

COMP s1 Lecture 1

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

C for C++ Programmers

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

Kurt Schmidt. October 30, 2018

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

EMBEDDED SYSTEMS PROGRAMMING Language Basics

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

Programming in C and C++

CS50 Supersection (for those less comfortable)

EC 413 Computer Organization

ECEN 449 Microprocessor System Design. Review of C Programming

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

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

EL2310 Scientific Programming

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

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

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

CE221 Programming in C++ Part 1 Introduction

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

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Lecture 12 Modular Programming with Functions

Pace University. Fundamental Concepts of CS121 1

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Lecture 1: Overview of Java

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

CS 61c: Great Ideas in Computer Architecture

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

CS 231 Data Structures and Algorithms, Fall 2016

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

Course Outline. Introduction to java

Object Oriented Programming. Java-Lecture 6 - Arrays

Array Initialization

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

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

Programming in C - Part 2

CMSC 246 Systems Programming

Signals, Instruments, and Systems W3. C Programming & Memory Management in C

QUIZ on Ch.8. What is kit?

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

C and C++ I. Spring 2014 Carola Wenk

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

An overview of Java, Data types and variables

About this exam review

Computer Components. Software{ User Programs. Operating System. Hardware

CSC231 C Tutorial Fall 2018 Introduction to C

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;


Midterms Save the Dates!

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1

C++ for Java Programmers

CS 106 Introduction to Computer Science I

APCS Semester #1 Final Exam Practice Problems

Topics Introduction to Microprocessors

CS 11 java track: lecture 1

CS 253: Intro to Systems Programming 1/21

ECE 250 / CPS 250 Computer Architecture. C Programming

1007 Imperative Programming Part II

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Transcription:

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

Part I Starting out Mark G. Eramian, Ian McQuillan CMPT 115 2/32

Outline 1 C Mark G. Eramian, Ian McQuillan CMPT 115 3/32

Overview: C vs. Java Java is an Object Oriented Language. C uses a different programming paradigm called procedural programming. There are no objects in C. Only built-in data types and the facility to build other data types using built-in types as building blocks. C doesn t allow for packaging of code and data. There are no methods, only functions which exist as separate entities. Mark G. Eramian, Ian McQuillan CMPT 115 4/32

C vs. Java C is a compiled language. After compilation, you get machine code which is run directly on the hardware (machine dependent). Compiling Java code creates bytecode, which gets run on the Java Virtual Machine (which then gets converted to machine code by the JVM). Mark G. Eramian, Ian McQuillan CMPT 115 5/32

Our first program Here is our first program. Type it into an editor and save the contents in a file called hello.c. #include <stdio.h> /* Our first C Program */ int main(void) { printf("hello world!\n"); Mark G. Eramian, Ian McQuillan CMPT 115 6/32

Compiling and Running Open the command prompt and type the following: > gcc -Wall hello.c > ls a.out* hello.c >./a.out Hello world! > gcc -Wall -o hello hello.c > ls a.out* hello* hello.c >./hello Hello world! > Mark G. Eramian, Ian McQuillan CMPT 115 7/32

Variables Primitive types are identical in C and Java. There are int, float, char, double, etc.. Also, void is used when there is no return type. Assignment into primitive types is the same. int i; char c; i = 1; c = c ; Mark G. Eramian, Ian McQuillan CMPT 115 8/32

Boolean With one exception: there is no boolean type. People use int, and interpret 1 as true and 0 as false. int i; i = (1 == 1); printf("the integer %d is true ",i); i = (1 == 0); printf("the integer %d is false ",i); The first statment prints 1 while the second prints 0. Mark G. Eramian, Ian McQuillan CMPT 115 9/32

To note In C, all variables must be declared at the start of a function, before any executable code. /* OK */ int main(void) { int k = 1, l = 10; char c = X ; for(k=0; i < j; i++) { printf("%i\n", i); printf("%c\n", c); /* ILLEGAL! Compiler error. */ int main(void) { int j = 10; for(int i=0; i < j; i++) { printf("%i\n", i); char c = X ; printf("%c\n", c); Printing and reading from the command line will be dealt with extensively in lab 1. Mark G. Eramian, Ian McQuillan CMPT 115 10/32

Similarities Arithmetic operators +, -, *, /, % are identical. Conditionals >, <, >=, <=, ==,!= are identical. For loops are identical, but you have to watch out that you declare the counter at the top of the function instead of in the for loop (variables must be declared before executable code. int main(void){ int i, x = 100; for(i = 0; i < x; i++){ printf("i is %d ",i); Mark G. Eramian, Ian McQuillan CMPT 115 11/32

Similarities While loops are the same, as are do-while loops int i = 0, end = 50; while(i < 50){ //do something i++; do{ //do something i--; while(i >= 0); Mark G. Eramian, Ian McQuillan CMPT 115 12/32

Arrays Arrays are pretty much the same as in Java. int a[50]; // Declares an array of 50 ints char s[100]; // Declares an array of 100 characters int n[] = {1, 2, 3, 4, 5; //declares and initializes an array int main(void) { int Array[10], i; for(i=0; i < 10; i++) { Array[i] = i*i; Arrays will be studied in more depth in topic 3 of the notes. Mark G. Eramian, Ian McQuillan CMPT 115 13/32

Strings There are no strings in C. Only character arrays. There are many functions available in pre-made libraries to help with standard string operations. They will be dealt with in Lab 2. Mark G. Eramian, Ian McQuillan CMPT 115 14/32

Compound data types C allows you to combine basic data types together using something called a struct. A struct is like a Java class, except it contains only data (ie. the instance variables), but no methods. struct tagname { int x; int y; ; We can now declare a variable whose type is this structure: struct tagname point; The attributes of a struct are similar to instance variables in Java. Mark G. Eramian, Ian McQuillan CMPT 115 15/32

Compound data types We can access the fields of struct tagname using the. operator: struct tagname point; point.x = 10; point.y = 20; Mark G. Eramian, Ian McQuillan CMPT 115 16/32

Compound data types This pretty much worked the same in Java, but usually the instance variables were set to private meaning you needed to use instance methods to retrieve and manipulate the attributes. For example, you might write a method in the point class (in Java) called setx as follows; void setx(int i){ x = i; point.setx(10); Here we just manipulate the attributes directly with point.x = 10;. Mark G. Eramian, Ian McQuillan CMPT 115 17/32

Compound data types We can give better names to struct s using type definitions. A type definition is given as: typedef <known_type> <new type> Example: typedef struct optional_tagname { int x; int y; Point;... Point p; p.x = 10; p.y = 20; Mark G. Eramian, Ian McQuillan CMPT 115 18/32

Nested structures We can even have nested structures (just like in Java where in one class we could have instance variables of another class). typedef struct{ int x; int y; Point; typedef struct{ Point start; Point end; Line; int main(void){ Line newline; newline.start.x = 0; newline.start.y = 0; newline.end.x = 5; newline.end.y = 5; Mark G. Eramian, Ian McQuillan CMPT 115 19/32

Functions C functions look very much like Java methods. There is the function header, followed by the body of the function in curly braces. Syntax: <return type> <name>(arg1, arg2,...) Functions can have 0 or more arguments. If no return type is specified, the default is int. If no arguments, use void. Mark G. Eramian, Ian McQuillan CMPT 115 20/32

Functions - an example #include <stdio.h> int exp( int n, int m ){ int count = 0, answer = 1; for (count = 0; count < m; count++){ answer = answer * n; return answer; int main(void){ int exponent = 0, answer = 0; while (exponent < 20){ answer = exp(2,exponent); printf("two to the power of %d is %d \n", exponent, answer); exponent++; Mark G. Eramian, Ian McQuillan CMPT 115 21/32

Functions - Declarations vs Definitions A function must be declared before it is called. That is why the main function is at the bottom (it is calling on other functions). There are ways around that (we will go through this in class as well). A function declaration consists of just the function header, (also called a prototype), followed by a semicolon: int fib(int n); Once the function is declared, it can be called in other functions, even if the function body hasn t be defined yet. A function definition is the header plus the body. Mark G. Eramian, Ian McQuillan CMPT 115 22/32

An example #include <stdio.h> int exp(int n, int m); //function declaration int main(void){ int exponent = 0, answer = 0; while (exponent < 20){ answer = exp(2,exponent); printf("two to the power of %d is %d \n", exponent, answer); exponent++; int exp( int n, int m ){ //function definition int count = 0, answer = 1; for (count = 0; count < m; count++){ answer = answer * n; return answer; Mark G. Eramian, Ian McQuillan CMPT 115 23/32

Anatomy of a C program Include header files (yours or library headers). Make a habit to include these headers: #include <stdio.h> #include <stdlib.h> #include <string.h> Declare types/structs/constants. Declare and define functions. Declare and define main(). Mark G. Eramian, Ian McQuillan CMPT 115 24/32

Translate Java to C Take a look at the files Point.java and Ian.java for a java implementation of simple program which implements a point class, and uses a few methods from it. We will try to translate this to C. Mark G. Eramian, Ian McQuillan CMPT 115 25/32

Example First, we would like to translate the Point type into an equivalent data type in C using a struct. What should the attributes of the struct be? Mark G. Eramian, Ian McQuillan CMPT 115 26/32

Example In the main program, we declare a Point variable p, and we need to initialize the x and y coordinates to that of the origin, just like the Java constructor. How do we manipulate the x and y coordinate of p? How do we retrieve the x and y coordinates of p? Mark G. Eramian, Ian McQuillan CMPT 115 27/32

Example It would be nice to keep movediagonal in its own method, as it is a little bit more complex, and we may want to call it over and over again. We have to pass in p as a parameter to movediagonal. In C, when you pass in a variable as a parameter to a method, a copy of the variable contents is made and passed as a parameter. Therefore, if you changed po inside movediagonal, the contents of p in the main method would be left unchanged. However, we can return po back to the main function, and assign it to our original variable, p. We will implement the same program to C, in a more direct way starting on the next slide. Mark G. Eramian, Ian McQuillan CMPT 115 28/32

Pointers The most difficult difference between C and Java is in the use of so-called pointers. Pointers store addresses of variables. Every instance of a class in java is actually a pointer (ie. an address). In the Java line Person p = new Person(), the variable p is actually the address of a Person object, rather than the object itself. In C, if you have a struct called Person, and you declare a new variable Person p, the variable p actually contains the struct contents. Pointers were not discussed in 111, but we will discuss them in detail in Topic 3 of the class. Mark G. Eramian, Ian McQuillan CMPT 115 29/32

Pointers After we do Topic 3: References in the lectures, please return to the remainder of these slides. In what is to follow, we will translate the same Java program into C using a more direct approach. It will really help to understand exactly what Java is doing behind the scenes. Java hides a lot of the details of pointers, but it is important to understand what is actually happening. Mark G. Eramian, Ian McQuillan CMPT 115 30/32

Example See the files Point.java and Ian.java for the same java implementation of a simple program which implements a Point class, and uses a few methods from it. See the file PointApp.c for the equivalent implementation in C. Notice that for all methods in the Point class, the equivalent C implementation takes in the Point element as first parameter. Instead of saying p.setx(i), it is necessary in C to pass in p as the first parameter. The corresponding call in C would be setx(p, newx). This is because there are no instance methods in C. Mark G. Eramian, Ian McQuillan CMPT 115 31/32

Example The biggest change is that in the main program, we create a pointer to a Point. In Java, all object variables are actually pointers to the objects. For the methods setx, sety etc., we need to pass in a pointer to a Point as parameter, otherwise it would modify a copy of the Point, instead of the actual Point. This is just like the swap example in the Topic 3 notes. Mark G. Eramian, Ian McQuillan CMPT 115 32/32