Docsity

CMSC 212 - F05 (lect 3): Make, Data Types, Arrays, Scope, Storage and Linkage, Study notes of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 โ€“ F05 (lect 3)
Announcements
โ—Program #1
โ€“ due one week from today (next Thursday)
โ—Submit
โ€“ be careful of spelling of things like โ€œInvalidโ€ and โ€œInstructionโ€
โ—Environment
โ€“ must use Linux
โ—Reading
โ€“ Should have already read Chapter 1-4
โ€“ Chapter 7 [skip 7.6] (by next class)
โ—Email
โ€“ send mail from a umd account only
โ€“ spam filters discarding aol, hotmail, etc. e-mails
2
CMSC 212 โ€“ F05 (lect 3)
Make: A Tool to Compile Programs
โ—Problem: Compiling programs is often tedious,
accident prone and wasteful of time
โ—๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜†๎˜‚๎˜‡๎˜ˆ๎˜‰๎˜Š๎˜„๎˜…๎˜‚๎˜‹๎˜Œ๎˜…๎˜๎˜‰๎˜†๎˜…๎˜Ž
โ—๎˜ ๎˜‚๎˜‡๎˜๎˜‰๎˜†๎˜‡๎˜‰๎˜ ๎˜‡๎˜†๎˜‘๎˜‰๎˜’๎˜“๎˜‰๎˜…๎˜”๎˜๎˜‰๎˜‹๎˜Œ๎˜•๎˜๎˜‰๎˜–๎˜‚๎˜‹๎˜‹๎˜Œ๎˜‡๎˜—
๎˜˜ ๎˜ ๎˜™๎˜๎˜™๎˜‰๎˜Œ๎˜‰๎˜š๎˜†๎˜ƒ๎˜๎˜‰๎˜–๎˜Œ๎˜ƒ๎˜ƒ๎˜๎˜—๎˜‰๎˜› ๎˜Œ๎˜•๎˜๎˜š๎˜†๎˜ƒ๎˜
๎˜˜ ๎˜Š๎˜‰๎˜‹๎˜Œ๎˜•๎˜๎˜š๎˜†๎˜ƒ๎˜ ๎˜†๎˜™๎˜‰๎˜Œ๎˜‰๎˜š๎˜†๎˜ƒ๎˜๎˜‰๎˜œ๎˜™๎˜–๎˜๎˜†๎˜ž๎˜…๎˜Ÿ๎˜‰๎˜–๎˜‚๎˜‡๎˜…๎˜Œ๎˜†๎˜‡๎˜†๎˜‡ ๎˜‰๎˜ˆ
๎˜˜ !๎˜๎˜‚"๎˜๎˜–๎˜…๎˜‰๎˜™๎˜…๎˜๎˜„๎˜–๎˜…๎˜„๎˜๎˜ ๎˜œ๎˜š๎˜†๎˜ƒ๎˜๎˜™#๎˜‰๎˜—๎˜๎˜ž๎˜๎˜‡๎˜—๎˜๎˜‡๎˜–๎˜†๎˜๎˜™๎˜Ÿ
๎˜˜ $๎˜‡๎˜™๎˜…๎˜๎˜„๎˜–๎˜…๎˜†๎˜‚๎˜‡๎˜™๎˜‰๎˜š๎˜‚๎˜๎˜‰๎˜š๎˜†๎˜ƒ๎˜๎˜‰๎˜–๎˜๎˜๎˜Œ๎˜…๎˜†๎˜‚๎˜‡
โ—๎˜› ๎˜Œ๎˜•๎˜๎˜‰๎˜†๎˜™๎˜‰๎˜‡๎˜‚๎˜…๎˜‰๎˜ƒ๎˜†๎˜‹๎˜†๎˜…๎˜๎˜—๎˜‰๎˜…๎˜‚๎˜‰% ๎˜‰๎˜ž๎˜๎˜‚ ๎˜๎˜Œ๎˜‹๎˜™
Adapted from: www.cs.tau. ac.il/~dcor/Software1/Makefile.ppt
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CMSC 212 - F05 (lect 3): Make, Data Types, Arrays, Scope, Storage and Linkage and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 โ€“ F05 (lect 3)^1

Announcements

l Program

  • due one week from today (next Thursday)

l Submit

  • be careful of spelling of things like โ€œInvalidโ€ and โ€œInstructionโ€

l Environment

  • must use Linux

l Reading

  • Should have already read Chapter 1-
  • Chapter 7 [skip 7.6] (by next class)

l Email

  • send mail from a umd account only
  • spam filters discarding aol, hotmail, etc. e-mails

CMSC 212 โ€“ F05 (lect 3)^2

Make: A Tool to Compile Programs

l Problem: Compiling programs is often tedious,

accident prone and wasteful of time

l    

l          

l         %   

Adapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile. ppt

CMSC 212 โ€“ F05 (lect 3)^3

Describing Files to Make

l      "      

l (   

sum (executable)

main.c sum.h sum.c

main.o sum.o

CMSC 212 โ€“ F05 (lect 3)^4

Makefile for Previous Example

sum: main.o sum.o

gcc โ€“o sum main.o sum.o

main.o: main.c sum.h

gcc โ€“c main.c

sum.o: sum.c sum.h

gcc โ€“c sum.c

Notes About Makefiles:

  • The pairs of lines shown here are each called a dependency.
  • A dependency usually contains
    • A target
    • A dependency list
    • An action (or directive)
  • Target is always the first item in a dependency
  • Items after โ€œ:โ€ are prerequisites required to build that target
  • Actions must have a tab at the beginning of the line
  • First target in file is the default build
  • Names must be exact

Target Dependency list

Tab

Action

CMSC 212 โ€“ F05 (lect 3)^7

Additional Make Features

l Macros

  • =
  • Example: CC = gcc CFLAGS = -g -Wall Foo.o: foo.c foo.h $(CC) $(CFLAGS) โ€“c foo.c Foo.o: foo.c foo.h gcc -g โ€“Wall โ€“c foo.c

l Default or Implied Action

  • $(CC) โ€“c $(CFLAGS)

l Default or Implied Dependencies

  • xxx.o must be created from a source file named xxx.?
  • in our case xxx.o comes from xxx.c
  • and created using -c option on compiler

l Actions can do any action stated (not only compilation)

  • Example: clean: rm *.o

CMSC 212 โ€“ F05 (lect 3)^8

More Complex Makefile

Created for Class Example

all: intCount table

clean: rm -f *.o *.bb *.bbg *.da *.png

intCount: intCount.o $(CC) -o intCount intCount.o

table: table.o main.o hashTable.o $(CC) -o table table.o main.o hashTable.o

table.o: table.c table.h hashTable.o: hashTable.c table.h main.o: main.c table.h intCount.o: table.h

CC = gcc CFLAGS = -g -Wall

Two top-level programs

Links together several files Uses Default Rule

Macro Definitions

Not part of main tree built by typing โ€œmake cleanโ€

CMSC 212 โ€“ F05 (lect 3)^9

Data Types in C

l Integer Family name minimum by ANSI standard ours

  • char 8 bits 8 bits
  • short 16 bits 16 bits
  • int 16 bits 32 bits
  • long 32 bits 32 bits
  • long long 64 bits 64 bits l Floating Point
  • float 32 bits 32 bits
  • double 64 bits 64 bits
  • long double 80 bits 96 bits l Signed by default, but all can be made unsigned
  • integer types are defined in the ANSI standard: unsigned int or unsigned long
  • unsigned floating types are not in the ANSI standard but are on some compilers l Literals (examples)
  • Decimal 255
  • negative signed decimal int -
  • Hex 0xff
  • floating point types: 3.14159, 1E10, 25., 6.023e
  • Characters: โ€˜aโ€™, โ€˜\nโ€™
  • C-Strings: โ€œa long dull stringโ€, โ€œ\nโ€, โ€œโ€

CMSC 212 โ€“ F05 (lect 3)^10

Data Types in C (cont.)

l Pointers

  • holds an address
  • points to a specific storage space
  • usually declared to point to a space of a certain type
  • space pointed at can be dynamically allocated (later) but this is

not necessary

#include int main(){ double a; double b; printf("size of a is %d and size of b = %d\n",sizeof(a), sizeof(b)); a = 5; b = &a; printf("a = %.2f and b indicates %.2f\n",a,b); a = 21; printf("a = %.2f and b indicates %.2f\n",a,b); return 0; }*

CMSC 212 โ€“ F05 (lect 3)^13

Variable Declarations

l , , โ€ฆ ;

  • int a, b, c;
  • short d; or short int d;

l Variables names

  • May not be reserved words
  • May contain alphanumeric characters and underscore
    • (A-Z, a-z, 0-9, and โ€œ_โ€)
    • examples:
      • aValid_Variable_Name
      • aValid_Variable_Name_
      • a
  • Must start with non-numeric: A-Z,a-z or _

l May initialize variables as part of declaration

  • int a = 4;

l Implicit Declarations (Book section 3.2.4)

  • Skip section book and donโ€™t ever use

CMSC 212 โ€“ F05 (lect 3)^14

Simple Arrays

l Array dimensions must be known at compile time

l Array Declaration

  • int a[10]; /* array of 10 integers */
  • really one big chuck of space โ€“ big enough for 10 integers

l Accessing array elements

  • Arrays start from 0 and go to n-1 elements
  • Compiler will let programmer access invalid elements
    • Can lead to many problems that are hard to find
    • mathematically calculates the โ€œoffsetโ€
  • Examples of array references (assume: int i = 2);
    • a[0]
    • a[i]
    • a[i+3]
      • what if int i = 7; ??

CMSC 212 โ€“ F05 (lect 3)^15

Typedef

l Sometimes you want to name your own type

  • Makes program easier to read
  • Makes it easier to change types later

l typedef double dollars;

CMSC 212 โ€“ F05 (lect 3)^16

Variable Scope

l File Scope

  • Entire file
  • Not declared inside any function

l Block Scope

  • Before any executable statement in a block of code { .. }

l Repeating same variables in nested scope is allowed

but should be avoided!!

main() {

int a;

{ int a; a = 3; }