From Java to C A Supplement to Computer Algorithms, Third Edition. Sara Baase Allen Van Gelder

Size: px
Start display at page:

Download "From Java to C A Supplement to Computer Algorithms, Third Edition. Sara Baase Allen Van Gelder"

Transcription

1 From Java to C A Supplement to Computer Alorithms, Third Edition Sara Baase Allen Van Gelder October 30, 2004

2 ii clcopyriht 2000, 2001 Sara Baase and Allen Van Gelder. All rihts reserved. This document may be copied reely or noncommercial purposes.

3 1 OVERVIEW This is a brie uide or convertin the subset o Java used in the text into C. The purpose is to help readers who want to implement some alorithms, but preer to work in C. Alternatively, i a prototype Java implementation has been developed and debued, readers miht wish to convert the proram to C to improve its perormance. Java has many constructs and eatures that the text does not use, and we do not address their conversion into C. Familiarity with C is a prerequisite or readin this uide. For excellent treatments o the C lanuae, see Gehani (1998) and Roberts (1995). This uide uses the terminoloy o declare and deine as set orth by Gehani. On Unix systems, C library unctions are documented in the man paes ; or example, man print elicits the speciications or print. It will also help i Section 1.2, Chapter 2 and the appendix o the text have been looked over (InputLib is in the appendix). Reerences in this uide reer to the text. Here are some basic correspondences between Java and C. Java C public static void main(strin[] arv) int main(int arc, char* arv[]) arv.lenth arc arv[i] arv[i+1] (arv[0] holds the proram name.) System.in, System.out, System.err stdin, stdout, stderr print or println print or print DYNAMIC ALLOCATION: newbecomes calloc Thenew operatorin Java correspondsmost closely to calloc in C becausecalloc initializes the allocated memory to binary zeros; malloc may also be used. Note that p below is technically a pointer in C. However, aside rom the constructor call to calloc, p can and should be treated as an array. Java C int[] p = new int[n]; int* p = calloc(n, sizeo(int)); int plenth = n; p.lenth plenth p[i] p[i] ORGANIZER CLASSES BECOME STRUCTS In eneral, oranizer classes in Java (a term introduced in the text) should be structs in C. Pointers to such structs are not routinely needed, but arrays o structs are appropriate wherever arrays o an oranizer class would be used in Java. In C the assinment statement operates on structs, copyin the contents o the struct, not just a reerence to it, so the copy methods o Java oranizer classes are not needed in C. We illustrate the ideas by translatin the Date class in Section 1.2 o the text, Exercise 1.1, into C. typede struct /* no nested typedes in C */ int number; int isleap; /* no boolean type in C */ Year; typede struct Year year; int month; int day; Date; ::: Date ::: duedate, noticedate; noticedate = duedate; noticedate.day = duedate.day - 7;

4 2 Unlike the Java example in Section 1.2, the above assinments work as expected: Chanin noticedate.day does not also chane duedate.day because the previous assinment copied the contents o duedate, not a reerence to it. It is perectly proper or a unction to return a struct, and this is the recommended way or a unction to produce several outputs. Because certain common prorammin needs are much simpler to code with structs, we would not be surprised to see structs added to some uture version o Java, as an extension to the eiht built-in primitive types. ADT CLASSES Althouh oranizer classes should become structs, ADT classes in Java should almost always become pointer types in C. O course, the pointer type will point to some struct, but that struct will normally be hidden rom the ADT clients. The desin philosophy is explained in Chapter 2 o the text (see Fiure 2.1, e..). The separate compilation eature o C allows the privacy eatures o Java to be simulated to a limited extent. We ll walk throuh an example, usin the IntList ADT o Chapter 2. The eneral idea or a C implementation o an ADT is that the public declarations and deinitions o in a header ile, conventionally named IntList.h, while the private declarations and deinitions o in the ile IntList.c. Both IntList.c and the ADT client iles include IntList.h. A salient point is that the pointer type or the ADT is normally public (so appears in IntList.h), while the details o the struct to which it points are private (so appear in IntList.c). The name o the struct (its ta) is public, however. In line with the oreoin remarks, IntList.h will have a declaration like this near its beinnin: typede struct IntListStruct * IntList; This declares (names, but does not deine)astructnamedintliststruct and deines the type IntList to be a pointer to this type o struct, whatever it is. Now unction prototypes and lobal constants or the ADT may be declared in IntList.h. Notice that prototypes are very similar to the unction headers in Fiure 2.4 o the text, but the words public and static are absent. (The meanin o static in C is quite dierent rom its meanin in Java, and is discussed later in this uide.) int irst(intlist L); IntList rest(intlist L); IntList cons(int newelement, IntList oldlist); extern const IntList nil; A point that is not widely appreciated is that the keyword extern in the declaration o nil means that the type o nil is bein declared in this statement, but the actual deinition o nil is elsewhere, possibly in the same ile or compilation unit. In this example, IntList.c will include IntList.h and the above extern declaration, and it will also contain the statement that deines (reserves storae or) nil, which becomes a lobally accessible constant. This will be seen in a ew pararaphs. Finally, let us remark that all the documentation (javadoc) comments that appear in Fiure 2.4 o the text should appear in IntList.h (as opposed to IntList.c). The theme o ADT encapsulation and inormation hidin is that IntList.h contains the public interace inormation or the ADT clients, whereas IntList.c may well not be available to those clients (they would link in the separately compiled IntList.o). Now let us turn to the contents o IntList.c, whichimplements the ADT. Ater the customary #include statements, the irst order o business is to complete the declaration o IntListStruct. Then nil is deined. Aain, it is instructive to compare with Fiure 2.4 and observe the correspondences. #include #include <stdlib.h> "IntList.h" struct IntListStruct int element; IntList next; ; const IntList nil = NULL;

5 3 We miht also include the ollowin bizarre-lookin statement: typede struct IntListStruct IntListStruct; This makes IntListStruct a type as well as the name o a struct. It happens that the IntList ADT does not have a use or this type, but it miht be convenient in some more elaborate ADTs. Please reer to the C reerences cited in the overview or more inormation about this syntactic issue. The actual C code in the unction bodies is quite similar to Java (or the subset o Java used in the main part o the text). A very important dierence, however, is that pointer variables are ollowed by the arrow operator (->) in C, not the dot operator, to access instance ields o the object to which they point. Thus the C code will have L->element, L->next, etc. We include one example. IntList cons(int newelement, IntList oldlist) IntList newlist = calloc(1, sizeo(struct IntListStruct)); newlist->element = newelement; newlist->next = oldlist; return newlist; One o the advantaes o Java or lare projects that is not enjoyed by C is that unction names are qualiied when they are used outside their class. For example, i a proram has list ADTs o several types, in C it cannot use irst in all o them as a unction name. A common solution in C is to deine a two- or three-letter preix or each ADT and bein all public names with that preix. Even i name clashes are not a problem, it helps other prorammers to determine which unctions are in which ADTs, modules, or libraries. In translatin the IntList ADT to C, it miht be appropriate to rename irst, cons, and so on to somethin like il irst, il cons, andsoon. VISIBILITY OF FUNCTIONS AND DATA STRUCTURES Althouh we don t have an example o it in IntList, it oten happens that we want to deine a subroutine or data structure inside an ADT and have it not be visible to the outside world. I nothin else, havin every subroutine visible throuhout a lare proram invites name clashes and accidental misuse when a unction with a similar name was intended. The mechanism in C or limitin visibility to the unctions that comprise an ADT is to use the static keyword. This word is rather misleadin, but we are stuck with it in C. When static appears in ront o a declaration or deinition that would otherwise be lobally visible it directs the compiler and linker to make the name visible only in the current compilation unit or ile bein compiled. Thus by combinin static and separate compilation,a deree o privacy or an ADT can be achieved. The correspondences between Java and C are as ollows: 1. A nonpublic static Java method becomes a static C procedure. Its prototype appears in the.c ile, not in the.h ile, or the ADT. 2. A public static Java method becomes an extern (lobally visible) C procedure or unction. Its prototype appears in the.h ile and its body appears in the.c ile or the ADT. 3. A nonpublic static Java instance ield becomes a static C variable. Its deinition appears in the.c ile, not in the.h ile, or the ADT. 4. A public static Java instance ield becomes an extern C variable. Its declaration, with the keyword extern, appears in the.h ile and its deinition, without extern, appears in the.c ile or the ADT. I these technicalities seem too complicated, readers should remember that limitin visibility is not necessary or correct unctionin unless there are name clashes. In most moderate-sized prorams, name clashes can be avoided simply by usin unique names. READING INPUT C oers many perhaps too many ways o readin iles. Our purpose here is just to sketch the eatures o C that correspond to the method o readin input described in the appendix o the text, primarily in the class InputLib.

6 4 The C unctions involved are all thorouhly documented as part o the stdio and strin libraries. No special library need be deined in C by the prorammer. The ollowin includes should be near the beinnin o a C ile that processes input. We also typede Cstrin or convenience. #include <stdio.h> #include <stdlib.h> #include <strin.h> typede char * Cstrin; A ile pointer (type FILE *) is returned by open, and the standard ile pointer stdin is automatically available to a C proram. Other input unctions will use the ile pointer as a parameter to indicate which ile is to be processed. We will desinate the ile pointer by inbu in the examples. For example, close(inbu) closes a ile previously opened by open. The primary input unction is ets, which ets one complete line o input into a buer (a character array, previouslyallocated). It is the analooetline in InputLib. TheetLine calls in Fiure A.2 would translate somethin like this: char line[1024]; /* A enerous overestimate or any one line */ char* etsretn; ::: etsretn = ets(line, 1024, inbu); while (etsretn == line) Ede e = parseede(line); ::: etsretn = ets(line, 1024, inbu); i (! eo(inbu)) /* Some error occurred */ Note that ets will not overlow line even i the input line is more than 1023 characters, so it is sae in this sense. PARSING A LINE OF INPUT AND OTHER STRING OPERATIONS At this point we want to point out that objects in the Strin class in Java are immutable; their contents cannot be chaned. So each etline call in Java necessarily returns a new strin. However, in C ets writesintoanarray allocated by the caller, and typically this array is used repeatedly or this purpose, as in the example above. Most o the strin library unctions in C work in a similar ashion; the caller is expected to allocate the space or the result, then use a parameter o the call to inorm the strin unction where that space is. An exception is strdup, which allocates the needed space, and returns its address to the caller. The ollowin table summarizes some correspondences between Java and C. We will see additional details about strin library unctions later. Java p = InputLib.open(name); 1 line = InputLib.etLine(p); nexttoken Inteer.parseInt Double.parseDouble Strin s s.lenth() Strin s = s1 + s2 + s3 tostrin [1] Assumes name 6= "-". C p = open(name, "r"); lptr = ets(line, len, p); strtok or strtok r (see discussion) atoi ato char s[n const ] or char s[] or Cstrin s (see discussion) strlen(s) Cstrin s = calloc(len, sizeo(char)); strncpy(s, s1, len); strncat(s, s2, len - strlen(s)); strncat(s, s3, len - strlen(s)); sprint Since C does not allocate space or strins like Java, the prorammer needs to be sure that strin operations occur only within allocated space. The library unctions strcpy, strcat, ets, and many others are unsae because

7 5 they miht write beyond the bounds o allocated space. Their counterparts, strncpy, strncat, ets, include a lenth parameter to limit the number o characters the unction will write. That parameter miht be set to any value by the caller, but the sensible value is the number o bytes actually available in the allocated space or the taret strin. As in the Java version o parseede in the text appendix (see Fiure A.3), it is the job o the C version o parseede to et the data ields out o line. First, we typede Ede as a struct, in accordance with the earlier discussion o oranizer classes. typede struct int rom; int to; double weiht; Ede; Now we ive two versions o parseede. The irst version is much simpler, and uses the knowlede o what is expected in the input line. It uses sscan with parameters that are desined or the line to contain two inteers, possibly ollowed by a loatin-point number. The second version is more complicated, and uses the eneral unction strtok, to accommodate input strins o unknown ormat. parseede with sscanfirst, we ll ive the code, then explain it. Ede parseede(char line[]) Ede newe; int numfields; numfields = sscan(line, " %d %d %l %*s", &newe.rom, &newe.to, &newe.weiht); i (numfields < 2 numfields > 3) print("bad ede: %s", line); exit(1); i (numfields == 2) newe.weiht = 0.0; return newe; The sscan unction extracts data items rom the char array that is its irst parameter (line in our example). The extraction is overned by the pattern iven in the second parameter, which is usually an explicit strin enclosed in double quotes, as in our example. Remainin parameters are addresses at which to store the extracted data items; the & preix operator can be read address o. A ull explanation o the scan amily may be ound in the man paes o your Unix system, or in a book that describes the C library unctions. We conine our explanation to this example. The pattern strin or extraction includes a space to indicate where an arbitrary number o white space characters may occur. A ield in this strin beinnin with % speciies a data item to be extracted rom the input. The codes we use are %d to extract an int, %l toextracta double, and %s to extract a strin. But we actually speciy %*s to speciy that, althouh the strin is extracted, it is discarded. That is, there is no parameter in the sscan call that speciies a storae location or the strin. So the technique here is a trick to see i there is extra unexpected stu on the line that was read and passed into parseede. I sscan does ind any siniicant characters ater extractin three ields, it will return the value 4; this will lead to an error messae. We emphasize that a ield %s must correspond to a later parameter that ives the address o a char array in which to store a strin ound in line. The prorammer must ensure that adequate space has been allocated to store that strin. For most applications where the input is oranized by lines, the combination o ets and sscan provides a sae and adequate method to read and parse the input data.

8 6 parseede with strtokfirst, we ll ive the code, then explain it. Ede parseede(char line[]) Ede newe; Cstrin linetmp = strdup(line); Cstrin w1 = strtok(linetmp, " ntnn"); Cstrin w2 = strtok(null, " ntnn"); Cstrin w3 = strtok(null, " ntnn"); Cstrin w4 = strtok(null, " ntnn"); i (w1 == NULL w2 == NULL w4!= NULL) print("bad ede: %s", line); exit(1); newe.rom = atoi(w1); newe.to = atoi(w2); i (w3!= NULL) newe.weiht = ato(w3); else newe.weiht = 0.0; ree(linetmp); return newe; The nexttoken unction in Java translates enerally to the strtok unction in the C strin library. First we make anewcopyoline in linetmp because strtok will chane the contents o its irst arument. The irst call to strtok requests (the address o) the irst word or token in the Cstrin linetmp. Because linetmp 6= NULL, strtok remembers it and returns the address o the irst token. The second throuh ourth calls to strtok have NULL as the irst parameter, indicatin that the caller wants subsequent tokens rom the same Cstrin that it is rememberin, i.e., linetmp. The second parameter o strtok speciies a list o characters that are to be treated as delimiters or the token requested. In this case we speciied space, tab, and newline, a popular set o delimiters. Extra delimiters are discarded. For example, there miht be spaces beore the irst token. I no characters remain in linetmp or the only characters that have not been converted to tokens are delimiters, strtok returns NULL. Thus the irst i test in the code is true i linetmp contains ewer than two tokens or more than three tokens. The second i is true i there is a third token. Whenever strtok inds the requested token, it writes a binary zero character immediately ater the end o it in linetmp and returns the address within linetmp where the token beins. Thus the returned address is a pointer to a 0-terminated strin. We copied line into linetmp so line would remain intact. I we need to remember the strin returned by strtok or any lenth o time, we must make a sae copy o it with strdup. In the example, we use atoi and ato to interpret these tokens as ints and doubles, ater which we do not need them. As a rule, you want to process all tokens returned by strtok with one o the unctions strdup, atoi, orato, dependin on whether it is a strin, int, or double (doubles miht be converted to loats). Then linetmp can be saely reed so its space can be reused. Notice that it is not necessary to make a dynamic allocation o the struct newe in C; the return statement copies its contents back into the caller s storae area. Notice how this is dierent rom the construction o an ADT object, as illustrated in the example o IntList.c.

From Java to C A Supplement to Computer Algorithms, Third Edition. Sara Baase Allen Van Gelder

From Java to C A Supplement to Computer Algorithms, Third Edition. Sara Baase Allen Van Gelder From Java to C A Supplement to Computer Algorithms, Third Edition Sara Baase Allen Van Gelder September 28, 2016 ii c Copyright 2000, 2001, 2015 Sara Baase and Allen Van Gelder. All rights reserved. This

More information

Status. We ll do code generation first... Outline

Status. We ll do code generation first... Outline Status Run-time Environments Lecture 11 We have covered the ront-end phases Lexical analysis Parsin Semantic analysis Next are the back-end phases Optimization Code eneration We ll do code eneration irst...

More information

A SUIF Interface Module for Eli. W. M. Waite. University of Colorado

A SUIF Interface Module for Eli. W. M. Waite. University of Colorado A SUIF Interace Module or Eli W. M. Waite Department o Electrical and Computer Enineerin University o Colorado William.Waite@Colorado.edu 1 What is Eli? Eli [2] is a domain-specic prorammin environment

More information

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x); C Notes Relation Between Pointers and Arrays Space for static arrays in C is allocated on the stack when the function that defines them is called and is automatically deleted when the function ends, just

More information

Composite functions. [Type the document subtitle] Composite functions, working them out.

Composite functions. [Type the document subtitle] Composite functions, working them out. Composite unctions [Type the document subtitle] Composite unctions, workin them out. luxvis 11/19/01 Composite Functions What are they? In the real world, it is not uncommon or the output o one thin to

More information

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36 Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar

More information

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this

More information

Compiler construction

Compiler construction This lecture Compiler construction Lecture 5: Project etensions Magnus Mreen Spring 2018 Chalmers Universit o Technolog Gothenburg Universit Some project etensions: Arras Pointers and structures Object-oriented

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

C for C++ Programmers

C for C++ Programmers C for C++ Programmers CS230/330 - Operating Systems (Winter 2001). The good news is that C syntax is almost identical to that of C++. However, there are many things you're used to that aren't available

More information

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS 261 Fall Mike Lam, Professor. Structs and I/O CS 261 Fall 2018 Mike Lam, Professor Structs and I/O Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful

More information

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d)

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d) Tiger source program Tiger Semantic Analysis lexical analyzer report all lexical errors token get next token parser construct variable deinitions to their uses report all syntactic errors absyn checks

More information

Tips for C Prorammers Use const instead of #dene to declare proram constants, e.., { C #dene PI #dene MAX INT 0x7FFFFFFF #dene MAX UNSIGNED 0x

Tips for C Prorammers Use const instead of #dene to declare proram constants, e.., { C #dene PI #dene MAX INT 0x7FFFFFFF #dene MAX UNSIGNED 0x The C++ Prorammin Lanuae C++ Tips and Traps Outline Tips for C Prorammers C++ Traps and Pitfalls Eciency and Performance 1 Tips for C Prorammers Use const instead of #dene to declare proram constants,

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Appendix: Instruments

Appendix: Instruments Appendix A Appendix: Instruments This appendix presents the instruments used in the experiment. These instruments can be used verbatim to repeat the experiment; a electronic copy is available upon request.

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Improving Computer Security using Extended Static Checking

Improving Computer Security using Extended Static Checking Improvin Computer Security usin Extended Static Checkin Brian V. Chess Department o Computer Enineerin University o Caliornia, Santa Cruz Abstract We describe a method or indin security laws in source

More information

OCC and Its Variants. Jan Lindström. Helsinki 7. November Seminar on real-time systems UNIVERSITY OF HELSINKI. Department of Computer Science

OCC and Its Variants. Jan Lindström. Helsinki 7. November Seminar on real-time systems UNIVERSITY OF HELSINKI. Department of Computer Science OCC and Its Variants Jan Lindström Helsinki 7. November 1997 Seminar on real-time systems UNIVERSITY OF HELSINKI Department o Computer Science Contents 1 Introduction 1 2 Optimistic Concurrency Control

More information

2. Getting Started with the Graphical User Interface

2. Getting Started with the Graphical User Interface February 2011 NII52017-10.1.0 2. Getting Started with the Graphical User Interace NII52017-10.1.0 The Nios II Sotware Build Tools (SBT) or Eclipse is a set o plugins based on the popular Eclipse ramework

More information

RE2C { A More Versatile Scanner Generator. Peter Bumbulis Donald D. Cowan. University of Waterloo. April 15, Abstract

RE2C { A More Versatile Scanner Generator. Peter Bumbulis Donald D. Cowan. University of Waterloo. April 15, Abstract RE2C { A More Versatile Scanner Generator Peter Bumbulis Donald D. Cowan Computer Science Department and Computer Systems Group University o Waterloo April 15, 1994 Abstract It is usually claimed that

More information

Lecture 8: C language

Lecture 8: C language Lecture 8: C lanuae History of C Structure of a C proram C data types Variable declaration and scope C operators Loops and iterations Pointers Structures in C C and assembly lanuae Microprocessor-based

More information

A Cell Burst Scheduling for ATM Networking Part II: Implementation

A Cell Burst Scheduling for ATM Networking Part II: Implementation A Cell Burst Schedulin or ATM Networkin Part II: Implementation C. Tan, A. T. Chronopoulos, Senior Member, IEEE Computer Science Department Wayne State University email:ctan, chronos@cs.wayne.edu E. Yaprak,

More information

MetaTeD A Meta Language for Modeling. Telecommunication Networks. Kalyan S. Perumalla and Richard M. Fujimoto

MetaTeD A Meta Language for Modeling. Telecommunication Networks. Kalyan S. Perumalla and Richard M. Fujimoto MetaTeD A Meta Lanuae or Modelin Telecommunication Networks Kalyan S. Perumalla and Richard M. Fujimoto (kalyan@cc.atech.edu and ujimoto@cc.atech.edu) Collee o Computin Georia Institute o Technoloy Atlanta,

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C C strings (Reek, Ch. 9) 1 Review of strings Sequence of zero or more characters, terminated by NUL (literally, the integer value 0) NUL terminates a string, but isn t part of it important for strlen()

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

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

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Using VCS with the Quartus II Software

Using VCS with the Quartus II Software Using VCS with the Quartus II Sotware December 2002, ver. 1.0 Application Note 239 Introduction As the design complexity o FPGAs continues to rise, veriication engineers are inding it increasingly diicult

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. The string-handling library () provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

10. SOPC Builder Component Development Walkthrough

10. SOPC Builder Component Development Walkthrough 10. SOPC Builder Component Development Walkthrough QII54007-9.0.0 Introduction This chapter describes the parts o a custom SOPC Builder component and guides you through the process o creating an example

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

The New C Standard (Excerpted material)

The New C Standard (Excerpted material) The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved. 165 5.1.2.2.1 Program startup 5.1.2.2.1 Program

More information

FPGA Technology Mapping: A Study of Optimality

FPGA Technology Mapping: A Study of Optimality FPGA Technoloy Mappin: A Study o Optimality Andrew Lin Department o Electrical and Computer Enineerin University o Toronto Toronto, Canada alin@eec.toronto.edu Deshanand P. Sinh Altera Corporation Toronto

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Implementing an abstract datatype. Linked lists and queues

Implementing an abstract datatype. Linked lists and queues Computer Programming Implementing an abstract datatype. Linked lists and queues Marius Minea marius@cs.upt.ro 19 December 2016 Review: compilation basics Briefly: Compiler translates source code to executable

More information

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

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14 APT Session 4: C Laurence Tratt Software Development Team 2017-11-10 1 / 14 http://soft-dev.org/ What to expect from this session 1 C. 2 / 14 http://soft-dev.org/ Prerequisites 1 Install either GCC or

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs

More information

Computer Data Analysis and Plotting

Computer Data Analysis and Plotting Phys 122 February 6, 2006 quark%//~bland/docs/manuals/ph122/pcintro/pcintro.doc Computer Data Analysis and Plotting In this lab we will use Microsot EXCEL to do our calculations. This program has been

More information

CSC209 Review. Yeah! We made it!

CSC209 Review. Yeah! We made it! CSC209 Review Yeah! We made it! 1 CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files 2 ... and C programming... C basic syntax functions

More information

ARCS LIGHTNING PROGRAMMER S REFERENCE, VERSION February 2000 Stephentown, New York

ARCS LIGHTNING PROGRAMMER S REFERENCE, VERSION February 2000 Stephentown, New York ARCS LIGHTNING PROGRAMMER S REFERENCE, VERSION 1.0 3 February 2000 Stephentown, New York cl Copyriht 2000 by Advanced Realtime Control Systems, Inc. All Rihts Reserved ii Contents 1 Gettin Started 1 1.1

More information

MP 3 A Lexer for MiniJava

MP 3 A Lexer for MiniJava MP 3 A Lexer for MiniJava CS 421 Spring 2010 Revision 1.0 Assigned Tuesday, February 2, 2010 Due Monday, February 8, at 10:00pm Extension 48 hours (20% penalty) Total points 50 (+5 extra credit) 1 Change

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

CS 237 Meeting 19 10/24/12

CS 237 Meeting 19 10/24/12 CS 237 Meeting 19 10/24/12 Announcements 1. Midterm: New date: Oct 29th. In class open book/notes. 2. Try to complete the linear feedback shift register lab in one sitting (and please put all the equipment

More information

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017) UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall 2017 Programming Assignment 1 (updated 9/16/2017) Introduction The purpose of this programming assignment is to give you

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam July 24, Name UW ID# Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

Image Fusion for Enhanced Vision System using Laplacian Pyramid

Image Fusion for Enhanced Vision System using Laplacian Pyramid Imae Fusion or Enhanced Vision System usin aplacian Pyramid Abhilash G, T.V. Rama Murthy Department o ECE REVA Institute o Technoloy and Manaement Banalore-64, India V. P. S Naidu MSDF ab, FMCD, CSIR-National

More information

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

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation

More information

9.8 Graphing Rational Functions

9.8 Graphing Rational Functions 9. Graphing Rational Functions Lets begin with a deinition. Deinition: Rational Function A rational unction is a unction o the orm P where P and Q are polynomials. Q An eample o a simple rational unction

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Coarse Grained Parallel Maximum Matching In Convex Bipartite Graphs

Coarse Grained Parallel Maximum Matching In Convex Bipartite Graphs Coarse Grained Parallel Maximum Matchin In Convex Bipartite Graphs P. Bose, A. Chan, F. Dehne, and M. Latzel School o Computer Science Carleton University Ottawa, Canada K1S 5B6 jit,achan,dehne,mlatzel@scs.carleton.ca

More information

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY ISA 563: Fundamentals of Systems Programming Announcements Homework 2 posted Homework 1 due in two weeks Typo on HW1 (definition of Fib. Sequence incorrect)

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

9. Reviewing Printed Circuit Board Schematics with the Quartus II Software

9. Reviewing Printed Circuit Board Schematics with the Quartus II Software November 2012 QII52019-12.1.0 9. Reviewing Printed Circuit Board Schematics with the Quartus II Sotware QII52019-12.1.0 This chapter provides guidelines or reviewing printed circuit board (PCB) schematics

More information

Decaf Language Reference Manual

Decaf Language Reference Manual Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented

More information

mga in C: A Messy Genetic Algorithm in C Kalyanmoy Deb and David E. Goldberg University of Illinois at Urbana-Champaign Urbana, IL 61801

mga in C: A Messy Genetic Algorithm in C Kalyanmoy Deb and David E. Goldberg University of Illinois at Urbana-Champaign Urbana, IL 61801 mga in C: A Messy Genetic Alorithm in C Kalyanmoy Deb and David E. Goldber Department o General Enineerin University o Illinois at Urbana-Champain Urbana, IL 61801 IlliGAL Report No. 91008 September 1991

More information

M.CS201 Programming language

M.CS201 Programming language Power Engineering School M.CS201 Programming language Lecture 9 Lecturer: Prof. Dr. T.Uranchimeg Agenda The char Data Type Using Character Variables Printing extended ASCII characters Arrays of Characters

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation Review: Constants Modules and s CS 217 C has several ways to define a constant Use #define #define MAX_VALUE 10000 Substitution by preprocessing (will talk about this later) Use const const double x =

More information

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

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Subject 1.- Introduction to Scheme language. First part: Scheme. Second part: Prolog

Subject 1.- Introduction to Scheme language. First part: Scheme. Second part: Prolog CÓRDOBA UNIVERSITY SUPERIOR POLYTECHNIC SCHOOL DEPARTMENT OF COMPUTER SCIENCE AND NUMERICAL ANALYSIS DECLARATIVE PROGRAMMING COMPUTER ENGINEERING COMPUTATION ESPECIALITY FOURTH YEAR FIRST FOUR-MONTH PERIOD

More information

Linear Network Coding

Linear Network Coding IEEE TRANSACTIONS ON INFORMATION THEORY, VOL. 49, NO. 2, FEBRUARY 2003 371 Linear Network Codin Shuo-Yen Robert Li, Senior Member, IEEE, Raymond W. Yeun, Fellow, IEEE, Nin Cai Abstract Consider a communication

More information

Reflection and Refraction

Reflection and Refraction Relection and Reraction Object To determine ocal lengths o lenses and mirrors and to determine the index o reraction o glass. Apparatus Lenses, optical bench, mirrors, light source, screen, plastic or

More information

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O Introduction The WRITE and READ ADT Operations Case Studies: Arrays Strings Binary Trees Binary Search Trees Unordered Search Trees Page 1 Introduction

More information

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Computer Data Analysis and Use of Excel

Computer Data Analysis and Use of Excel Computer Data Analysis and Use o Excel I. Theory In this lab we will use Microsot EXCEL to do our calculations and error analysis. This program was written primarily or use by the business community, so

More information

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2

More information

CS 237 Meeting 18 10/22/12

CS 237 Meeting 18 10/22/12 CS 237 Meeting 18 10/22/12 Announcements 1. Midterm: New date: Oct 29th. In class open book/notes. 2. Duane Bailey has volunteered to do a chips and breadboards lab this week. Will any of you volunteer

More information

Section III. Advanced Programming Topics

Section III. Advanced Programming Topics Section III. Advanced Programming Topics This section provides inormation about several advanced embedded programming topics. It includes the ollowing chapters: Chapter 8, Exception Handling Chapter 9,

More information

Chapter 8 - Characters and Strings

Chapter 8 - Characters and Strings 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions 8.5 Standard Input/Output Library

More information

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files CSC209 Review CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files ... and systems programming C basic syntax functions arrays structs

More information

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs. CSC209 Review CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files... and systems programming C basic syntax functions arrays structs

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Dynamic Allocation in Classes Review of CStrings Allocate dynamic space with operator new, which returns address of the allocated item. Store in a pointer:

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:

More information

C Libraries. Bart Childs Complementary to the text(s)

C Libraries. Bart Childs Complementary to the text(s) C Libraries Bart Childs Complementary to the text(s) 2006 C was designed to make extensive use of a number of libraries. A great reference for student purposes is appendix B of the K&R book. This list

More information

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University ommunication Networks (0368-3030) / Sprin 0 The lavatnik School o omputer Science, Tel-viv University llon Waner urose & Ross, hapters 5.5-5.6 (5 th ed.) Tanenbaum & Wetherall, hapters 4.3.4 4.3.8 (5 th

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

AN 459: Guidelines for Developing a Nios II HAL Device Driver

AN 459: Guidelines for Developing a Nios II HAL Device Driver AN 459: Guidelines or Developing a Nios II HAL Device Driver November 2008 AN-459-2.0 Introduction This application note explains the process o developing and debugging a hardware abstraction layer (HAL)

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems

More information

Chapter 8 C Characters and Strings

Chapter 8 C Characters and Strings Chapter 8 C Characters and Strings Objectives of This Chapter To use the functions of the character handling library (). To use the string conversion functions of the general utilities library

More information

Pizza ëordersky 1997ë. However, we felt that Pizza èand other similar lanuaesè suered from one fault that we had taken pains to avoid in the oriinal d

Pizza ëordersky 1997ë. However, we felt that Pizza èand other similar lanuaesè suered from one fault that we had taken pains to avoid in the oriinal d Multiparadim Extensions to Java Timothy A.Budd Department of Computer Science Oreon State University Corvallis, Oreon, USA November 1, 2000 Abstract In 1995 my students and I developed Leda, a multiparadim

More information

MP 3 A Lexer for MiniJava

MP 3 A Lexer for MiniJava MP 3 A Lexer for MiniJava CS 421 Spring 2012 Revision 1.0 Assigned Wednesday, February 1, 2012 Due Tuesday, February 7, at 09:30 Extension 48 hours (penalty 20% of total points possible) Total points 43

More information

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

String constants. /* Demo: string constant */ #include <stdio.h> int main() { Strings 1 String constants 2 /* Demo: string constant */ #include s1.c int main() { printf("hi\n"); } String constants are in double quotes A backslash \ is used to include 'special' characters,

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

Chapter 9 Strings. With this array declaration: char s[10];

Chapter 9 Strings. With this array declaration: char s[10]; Chapter 9 Strings 9.1 Chapter Overview There is no data type in C called ʻstringʼ; instead, strings are represented by an array of characters. There is an assortment of useful functions for strings that

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming Procedural Programming & Fundamentals of Programming Lecture 3 - Summer Semester 2018 & Joachim Zumbrägel What we know so far... Data type serves to organize data (in the memory), its possible values,

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam 5/10/13 Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed

More information