Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Notes on C and UNIX Programming - Introduction to Software Engineering | CSCI 0320, Study notes of Software Engineering

Material Type: Notes; Class: Introduction to Software Engineering; Subject: Computer Science; University: Brown University; Term: Summer 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-524
koofers-user-524 🇺🇸

10 documents

1 / 13

Toggle sidebar

Related documents


Partial preview of the text

Download Lecture Notes on C and UNIX Programming - Introduction to Software Engineering | CSCI 0320 and more Study notes Software Engineering in PDF only on Docsity! CS032: Introduction to Software Engineering 1 Lecture 12: C and UNIX Programming Lecture 12: C and UNIX Programming CSCI0320: Introduction to Software Engineering July 13, 2009 Steven P. Reiss I. Learning a New Language A. What does it take to pick up a second language? 1. For natural languages a) Learn the grammar and vocabulary is sufficient b) But this takes a long time 2. For computer languages a) Grammar and vocabulary are trivial b) But they are not sufficient c) Must learn to “think” in the new language (1) Set of rules for a game d) Must understand how the language is used (1) How it is used most effectively (2) How it is used in practice (3) How it should be used for particular applications B. Learning C 1. Assume that you know Java a) You know how to design in terms of objects & interfaces b) You know how to build methods, classes, & packages 2. What do you nedd to know to code in C a) What does it mean to think in “C” b) What do C programs look like 3. C programs vary all over the book a) C as Pascal, Fortran, assembler b) Naming conventions have changed over the years c) But there is a C standard that has evolved d) That is what we will teach CS032: Introduction to Software Engineering 2 Lecture 12: C and UNIX Programming II. Procedural Programming A. The first difference between C & Java 1. Java deals with classes and their fields and methods 2. C deals with variables and functions a) Variables are similar to fields b) Functions are similar to methods 3. What is missing is the class organization a) Including class hierarchy, formal interfaces, etc. b) Also Java’s package structure and libraries 4. This doesn’t mean you can’t think in terms of objects B. For simple programs this doesn’t matter 1. Single class is equivalent to a C program 2. But for complex programs it does matter III.Write a LIFE program A. Basics of the game of life 1. Array of squares 2. Generations a) Count neighbors b) Full with 0,1 neighbor => die c) Full with 4+ neighbors => die d) Empty with 3 neighbors => create B. How would you design this in Java 1. Some of you already have (cs15?) 2. Classes, etc. C. C works procedurally 1. Overall structure of system 2. Does this mean there are no data structures? D. C thinks in terms of modules 1. Module == class without inheritance a) Separate interface and implementation b) But there is a 1-1 relationship between the two CS032: Introduction to Software Engineering 5 Lecture 12: C and UNIX Programming b) ctype.h c) stdio.h d) math.h e) errno.h f) stdlib.h g) signal.h h) string.h (copy ops) 4. Lower case names C. C Preprocessor (cpp) 1. Meta language outside of C 2. Cpp is run over the source file first a) The result is what is actually compiled b) Show this in the compilation diagram 3. Simple text processing language a) Based on macro assemblers (macros) 4. Statements a) #include <file> or #include “file” b) #define NAME string c) #define NAME(arg,arg) string_with_args d) #ifdef, #ifndef, ... #endif e) #if (expression) f) #undef 5. Want to minimize use of the preprocessor a) Why? b) Readability (macros used far from definition) c) Tool accessability d) Semantics : string substituion is not semantic substitution (1) #define VALUE 3 + 3.14159 (2) x = VALUE * 2 CS032: Introduction to Software Engineering 6 Lecture 12: C and UNIX Programming V. Lets write the game of life in C A. Start with life_local.h 1. Definitions for board size 2. Definitions for other modules entry points B. Notes 1. Definitions needed by other files a) Functions, types, constants, 2. Start with block comment 3. Then check for multiple inclusion a) #ifndef HEADER_FILE_ALREADY_INCLUDED b) #define HEADER_FILE_ALREADY_INCLUDED c) ... d) #endif 4. Then include other header files that are needed/used a) #include ... 5. Inside put the various definitions a) Types b) Functions c) Constants 6. Constants a) Traditionally done with #define b) Today should be done using const declarations (1) Can be tricky (don’t want to allocate storage) (2) Different compilers treat differently 7. Function definitions a) Like Java interface definitions b) extern <type> function(args...) c) Parameters don’t need to be named d) Extern is required CS032: Introduction to Software Engineering 7 Lecture 12: C and UNIX Programming VI.Types A. In Java types are relatively simple 1. Primitive types 2. Interfaces and classes 3. Arrays 4. C has a more complex (and simpler) type structure B. Primitive Types 1. C Primitives a) char, short [int], int, long [int], long long b) float, double, long double c) void d) unsigned char, signed char e) unsigned short, unsigned int, unsigned long, unsigned long long f) Note that sizes aren’t specified (1) int is natural integer for the machine (2) long is long enough to hold a pointer (3) char is long enough to hold a character (not unicode) g) const X, volatile X 2. What’s missing a) boolean :: any value != 0 (int most common) b) Object, String 3. typedef: user names for types a) typedef int Int32; b) Look like declarations, but the item being declared is a type (1) Same type as a variable declared in that situation c) Very useful construct (1) Provides abstractions for use types (2) Makes type definitions readable C. Structs 1. Java classes provide a way of grouping related data CS032: Introduction to Software Engineering 10 Lecture 12: C and UNIX Programming c) What does this do in C versus what it does in Java 5. Can have multidimensional arrays x[10][20] a) All but the last dimension must be specified explicitly (1) With constants! b) How to allocate a dynamic array? G. Enums 1. C provides enumerations to define constants a) Better than #define, often better than const b) Type checked at least in part 2. enum _Name { A, B = 5, C }; 3. Different from Java enums a) Integers, not objects b) Automatic cast back and forth to integer 4. Typically done using a typedef H. Unions 1. A structure is a block of memory 2. What do you do if you sometimes need a integer, sometimes a Point, sometimes a pointer? a) In Java you create separate subclasses b) Don’t have subclasses in C 3. C provides union types a) Look like structures b) Except that the memory for each field overlaps 4. Again, no checking for safe usage 5. Generally you want to avoid unions a) Memory is cheap: use a structure instead I. Functions 1. In Java we don’t often think of the type of functions a) The compiler does to check argument types b) The compiler does to handle overloading c) Function types often viewed as object types (Comparator) 2. How does Java handle virtual calls? CS032: Introduction to Software Engineering 11 Lecture 12: C and UNIX Programming a) Essentially there is a table of methods for each object b) When you make a virtual call, Java finds the corresponding entry in the table c) That entry tells where in the code the corresponding virtual routine exists d) But this where is just an address: a pointer 3. C lets you define this type of pointer a) Pointer to a function b) Want to tell C the argument types (1) So compiler can do checking (2) Not required 4. Usually done with a typedef a) typedef int (*IntFunction)(int); b) Not this can get quite confusing VII.Code Files A. Start with block comment 1. And comment throughout B. Start with #include for each header file required C. Then variable definitions 1. static <type> var [= value] for local variables 2. <type> var [= value] for global variables a) Even though a global is defined in an extern in a .h file b) Should be defined once in a code file c) Not always required (depends on loader) D. Local types should probably be avoided 1. Define a local (directory or project) .h file instead E. Foward function definitions 1. static <type> fct(<params>); 2. Needed so compiler can work correctly F. Then the actual function codes 1. Pretty much like Java CS032: Introduction to Software Engineering 12 Lecture 12: C and UNIX Programming 2. Use static if the function is local 3. Don’t use extern if it is global VIII.C Code A. Declarations first in the code 1. Actually declarations must be the first thing in a block 2. Can’t have declarations intermixed with code 3. Can’t declare inside for statement B. Storage model 1. Java objects appear on the heap a) Local variables on the stack (1) Only references to objects and primitives 2. C has a different storage model a) Local variables: stack and registers (1) register declaration pretty much ignored today b) Static/external storage in fixed data area memory c) Heap suppported, not part of language per se 3. Declarations a) At top level: (1) extern :: defined elsewhere (2) static :: defined in fixed area, accessible locally (3) <none> :: defined in fixed area, accessible globally b) Inside code (1) extern :: defined elsewhere (2) static :: defined in fixed area, accessible in code (3) <none> :: defined on stack or register (4) register :: defined on stack or register 4. Other language differences a) No exceptions b) Casts are not checked c) No automatic initialization d) No new statement for allocation
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved