C For Java Programmers - PowerPoint PPT Presentation

About This Presentation
Title:

C For Java Programmers

Description:

C For Java Programmers Tom Roeder CS415 2005sp – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 22
Provided by: tmroeder
Category:

less

Transcript and Presenter's Notes

Title: C For Java Programmers


1
C For Java Programmers
  • Tom Roeder
  • CS415 2005sp

2
Why C?
  • The language of low-level systems programming
  • Commonly used (legacy code)
  • Trades off safety for speed
  • Clear mapping from C statements to operations
  • Simple to understand, imperative language
  • Well-understood common optimizations

3
Why not C?
  • Explicit memory management
  • Memory leaks
  • Invalid pointers
  • No (built-in) exception handling
  • Not type safe
  • Poor separation of concerns
  • No good language support for modularization
  • Too close to assembly

4
Common Constructs in C and Java
  • Loops
  • if()
  • for()
  • while()
  • do while()
  • Functions
  • int call_me(float a) return (int)a
  • int temp call_me(3.14)

5
C Types Primitives
  • Often architecture-dependent
  • int, short, long
  • Can count on short lt int lt long
  • float, double
  • Can usually count on float lt double
  • char
  • One byte per character
  • Unicode WCHAR in Windows two bytes

6
C Types struct
  • Often need to define object-like storage units
  • C only encapsulates data, not methods
  • struct is the unit of encapsulation
  • struct pos
  • float x
  • float y
  • p
  • p.x 0.7
  • p.y 0.1

7
C Types enum
  • When you need to write code like
  • Color x RED
  • enum BLUE, RED, GREEN
  • Well get to how to define the Color symbol
  • enums are actually underlying ints.
  • Can have any value at all
  • enum BLUE 7, RED 137, GREEN
  • GREEN will have value 138.

8
C Types pointers
  • A pointer contains the starting address of the
    memory for a given value
  • int y 0
  • int x y
  • x 10 / y is now 10 /
  • Explicit dynamic memory management
  • int x malloc(sizeof(int)z)
  • free(x)

9
C Types arrays and strings
  • An array is just a pointer (0-based)
  • int x5 10, 20, 30, 40, 50
  • x3 (x 3)
  • A string is just an array of characters
  • int main(int argc, char argv)
  • printf(arg 1 s\n, argv0)
  • printf(char 1 c\n, argv00)
  • Remember to terminate your string with \0
  • See string.h for functions strcmp, strcpy,

10
C Types typedef
  • When you want to define a new type
  • typedef int bool
  • typedef enum FALSE 0, TRUE bool
  • typedef struct queue_t
  • void elt
  • queue_t next
  • queue
  • Can be ill-used (as in Microsoft)
  • typedef int INTP

11
C Types void
  • void is a pointer to any type
  • Extremely useful in generic data structures
  • typedef struct queue_t
  • void elt
  • queue_t next
  • queue
  • queue q
  • (int)(q-gtelt) 137

12
C Functions function pointers
  • Unlike in Java, we dont have any reflection
  • We can nonetheless pass functions around
  • int call_me(float a) return (int)a
  • int (fp)(float) call_me
  • printf(fp gives d\n, (fp)(3.0))
  • Simply passing the address of the function

13
C Functions parameter passing
  • Two methods in C
  • By value
  • By reference
  • int swap(int a, int b)
  • int swap(int a, int b)
  • int swap(int a, int b)
  • In Java, all reference types are passed by
    reference

14
C Functions prototypes
  • A function in C must be declared before used
  • Thus you often give the signature twice
  • int call_me(float a)
  • int main(int argc, char argv)
  • return call_me(3.0)
  • int call_me(float a) return 1

15
C Traps memory management
  • Dont forgot to free memory youve alloced
  • Arrays are not bounds checked in C
  • Set all pointers to NULL
  • when they are initialized
  • when they are freed
  • Requires strict discipline, and you will forget
  • Use Purify (if on NIX, use valgrind)
  • Always check to see if a pointer is NULL before
    using it.

16
C Traps local variables
  • Simple example
  • char get_name()
  • char tempNAME_LENGTH
  • / get something into temp /
  • return temp
  • temp is allocated on the stack you will have an
    invalid pointer.

17
C Preprocessor
  • Used for constants and simple replacements
  • define PI 3.14
  • define DEBUG 1
  • Also used for macros with arguments
  • define max(x,y) ((x)gt(y) ? (x)(y))
  • Conditional compilation
  • if DEBUG
  • endif
  • includes include ltstdio.hgt

18
Most Common C Libraries
  • ltstdio.hgt standard I/O
  • printf (format, )
  • printf(Hello there, s\n, Tom)
  • s strings
  • c characters
  • d integers
  • f float
  • lf double
  • ltstdlib.hgt useful functions
  • exit, malloc, free

19
Multi-file project layout
  • Divide the code into functional units
  • Each unit has a .h and a .c file
  • Put the prototypes in the .h and the functions in
    .c
  • To make sure that .h files arent included more
    than once, for myfile.h, we do
  • ifndef __MYFILE_H_
  • define __MYFILE_H_
  • / file contents /
  • endif / __MYFILE_H_ /

20
Multi-file project layout
  • Normally have a file main.c
  • Include the other .h files
  • Has a function
  • int main(int argc, char argv)
  • To compile, use make/nmake
  • create object files (.o) and then link with
    libraries
  • We wont go into make here.

21
Where to go from here?
  • This is not an exhaustive discussion of C
  • Read the older notes online
  • Write some sample programs Get going on the
    assignment so that if you have simple C problems,
    we can help you solve them quickly.
  • Look in
  • KR The C Programming Language
  • Visual Studios help facility
Write a Comment
User Comments (0)
About PowerShow.com