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

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

Dynamic memory allocation (malloc)

PRINCIPLES OF OPERATING SYSTEMS

Arrays and Pointers. CSE 2031 Fall November 11, 2013

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

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

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

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

Array Initialization

Dynamic memory allocation

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Quick review pointer basics (KR ch )

CS201 - Lecture 1 The C Programming Language

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

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Today s class. Review of more C Operating system overview. Informationsteknologi

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Class Information ANNOUCEMENTS

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

COMP 2355 Introduction to Systems Programming

Lectures 5-6: Introduction to C

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

Fundamental of Programming (C)

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Lectures 5-6: Introduction to C

CSE 333 Lecture 7 - final C details

Language comparison. C has pointers. Java has references. C++ has pointers and references

Advanced Pointer Topics

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

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

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

Assist. Prof. Dr. Caner ÖZCAN

ECE 250 / CS 250 Computer Architecture. C Programming

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

Kurt Schmidt. October 30, 2018

Memory Allocation in C

Common Misunderstandings from Exam 1 Material

211: Computer Architecture Summer 2016

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

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

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

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

Arrays and Memory Management

AMCAT Automata Coding Sample Questions And Answers

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Memory Allocation. General Questions

An Ungentle Introduction to C

Dynamic Data Structures. CSCI 112: Programming in C

Understanding Pointers

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

Final CSE 131B Spring 2004

Basic and Practice in Programming Lab7

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

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

COMP 2355 Introduction to Systems Programming

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Dynamic Memory Allocation and Command-line Arguments

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging


Functions & Memory Maps Review C Programming Language

High Performance Programming Programming in C part 1

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS24 Week 2 Lecture 1

CS61, Fall 2012 Section 2 Notes

CSE 333 Lecture 2 Memory

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lecture 3: C Programm

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

ECE 250 / CPS 250 Computer Architecture. C Programming

Final CSE 131B Spring 2005

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

CS 0449 Sample Midterm

CSE 374 Programming Concepts & Tools

Systems Programming and Computer Architecture ( )

SYSC 2006 C Winter 2012

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

just a ((somewhat) safer) dialect.

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Why learn C (after Java)? C for Java programmers. What does this C program do? Simple Example. Goals of this tutorial

Arrays and Pointers (part 2) Be extra careful with pointers!

High-performance computing and programming I. Introduction to C programming in *NIX

CSCI 171 Chapter Outlines

Principles of C and Memory Management

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

UNIT III (PART-II) & UNIT IV(PART-I)

Course organization. Course introduction ( Week 1)

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

Arrays and Pointers (part 2) Be extra careful with pointers!

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

EL2310 Scientific Programming

Pointers and Memory Management

Arrays and Pointers (part 1)

LSN 3 C Concepts for OS Programming

Chapter 11 Introduction to Programming in C

Transcription:

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

Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers and dynamic memory 2

Like Java, like C Operators same as Java: Arithmetic i = i+1; i++; i--; i *= 2; +, -, *, /, %, Relational and Logical <, >, <=, >=, ==,!= &&,, &,,! Syntax same as in Java: if ( ) { } else { } while ( ) { } do { } while ( ); for(i=0; i < 100; i++) { } switch ( ) {case 1: } continue; break; 3

Simple Data Types datatype size values char 1-128 to 127 short 2-32,768 to 32,767 int 4-2,147,483,648 to 2,147,483,647 long 4-2,147,483,648 to 2,147,483,647 float 4 3.4E+/-38 (7 digits) double 8 1.7E+/-308 (15 digits long) 4

Java programmer gotchas (1) { int i; for(i = 0; i < 10; i++) NOT { for(int i = 0; i < 10; i++) 5

Java programmer gotchas (2) Uninitialized variables catch with Wall compiler option Well, that s almost correct... #include <stdio.h> int main(int argc, char* argv[]) { int i; factorial(i); return 0; } 6

Java programmer gotchas (3) Error handling No exceptions Must look at return values 7

Good evening #include <stdio.h> int main(int argc, char* argv[]) { /* print a greeting */ printf("good evening!\n"); return 0; } $./goodevening Good evening! $ 8

Breaking down the code #include <stdio.h> Include the contents of the file stdio.h Case sensitive lower case only No semicolon at the end of line int main( ) The OS calls this function when the program starts running. printf(format_string, arg1, ) Prints out a string, specified by the format string and the arguments. 9

format_string Composed of ordinary characters (not %) Copied unchanged into the output Conversion specifications (start with %) Fetches one or more arguments For example char %c char* %s int %d float %f For more details: man 3 printf 10

C Preprocessor #define CAU \ "Christian Albrechts Universitaet\n" int main(int argc, char* argv[]) { printf(cau); return 0; } 11

After the preprocessor (gcc E) int main(int argc, char* argv) { printf("christian Albrechts Universitaet\n"); return 0; } 12

Conditional Compilation #define CAU int main(int argc, char* argv) { #ifdef CAU printf("christian Albrechts Universitaet\n"); #else printf("some other university\n"); #endif return 0; } 13

After the preprocessor (gcc E) int main(int argc, char* argv) { printf("christian Albrechts Universitaet\n"); return 0; } 14

Command Line Arguments (1) int main(int argc, char* argv[]) argc Number of arguments (including program name) argv Array of char*s (that is, an array of c strings) argv[0]: = program name argv[1]: = first argument argv[argc-1]: last argument 15

Command Line Arguments (2) #include <stdio.h> int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n", i, argv[i]); return 0; } 16

Command Line Arguments (3) $./cmdline Christian Albrechts Universitaet 4 arguments 0:./cmdline 1: Christian 2: Albrechts 3: Universitaet $ 17

Arrays char foo[80]; An array of 80 characters sizeof(foo) = 80 sizeof(char) = 80 1 = 80 bytes int bar[40]; An array of 40 integers sizeof(bar) = 40 sizeof(int) = 40 4 = 160 bytes 18

Structures Aggregate data #include <stdio.h> struct name { char* name; int age; }; /* <== DO NOT FORGET the semicolon */ int main(int argc, char* argv[]) { struct name jensen; jensen.name = "Jens Jensen"; jensen.age = 25; } printf("%s is %d years old\n", jensen.name, jensen.age); return 0; 19

Pointers Pointers are variables that hold an address in memory. That address contains another variable. 20

Memory layout and addresses int x = 5, y = 10; float f = 12.5, g = 9.8; char c = c, d = d ; 5 10 12.5 9. 8 4300 4304 4308 4312 4317 4316 21

Using Pointers (1) float f; /* data variable */ float *f_addr; /* pointer variable */ f f_addr?? any float? 4300 4304 any address f_addr = &f; /* & = address operator */ f f_addr? 4300 4300 4304 22

Pointers made easy (2) *f_addr = 3.2; /* indirection operator */ f f_addr 3.2 4300 4300 4304 float g = *f_addr;/* indirection: g is now 3.2 */ f = 1.3; /* but g is still 3.2 */ f f_addr 1.3 4300 4300 4304 23

Function Parameters Function arguments are passed by value. What is pass by value? The called function is given a copy of the arguments. What does this imply? The called function can t alter a variable in the caller function, but its private copy. Three examples 24

Example 1: swap_1 void swap_1(int a, int b) { int temp; temp = a; a = b; b = temp; } Q: Let x=3, y=4, after swap_1(x,y); x =? y=? A1: x=4; y=3; A2: x=3; y=4; 25

Example 2: swap_2 void swap_2(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Q: Let x=3, y=4, after swap_2(&x,&y); x =? y=? A1: x=3; y=4; A2: x=4; y=3; 26

Example 3: scanf #include <stdio.h> int main() { int x; scanf( %d\n, &x); printf( %d\n, x); } Q: Why using pointers in scanf? A: We need to assign the value to x. 27

Dynamic Memory Java manages memory for you, C does not C requires the programmer to explicitly allocate and deallocate memory Unknown amounts of memory can be allocated dynamically during run-time with malloc() and deallocated using free() 28

Not like Java No new No garbage collection You ask for n bytes Not a high-level request such as I d like an instance of class String 29

malloc Allocates memory in the heap Lives between function invocations Example Allocate an integer int* iptr = (int*) malloc(sizeof(int)); Allocate a structure struct name* nameptr = (struct name*) malloc(sizeof(struct name)); 30

free Deallocates memory in heap. Pass in a pointer that was returned by malloc. Example int* iptr = (int*) malloc(sizeof(int)); free(iptr); Caveat: don t free the same memory block twice! 31