Download presentation
Presentation is loading. Please wait.
1
CS414 C Programming Tutorial Ben Atkin batkin@cs.cornell.edu
2
CS414 C Programming Tutorial2 Why use C? C is not "safe" It assumes you know what you're doing It has a lot of scope for bugs It's a good systems programming language Not much is hidden Faster than Java, more predictable performance It has all the low-level operations
3
CS414 C Programming Tutorial3 Types No Boolean type, use int : "false" is 0, true is non-zero Structures for grouping related data "Like Java classes, but no methods" typedef int pid_t; /* for clarity */ struct pcb { pid_t pid; file_t openfiles[MAXOPENFILES];... } typedef struct pcb pcb_t; /* rename */
4
CS414 C Programming Tutorial4 Constants #define KB 1024 /* constant */ #define MB (KB*1024) /* note brackets */ /* wrong! ';' causes compile error */ #define WARN(s) fprintf(stderr, s); /* alternative constant definition: PROC_READY==0, PROC_RUNNING==1,... */ enum { PROC_READY, PROC_RUNNING,... }; /* give it a name */ typedef enum { PROC_READY, PROC_RUNNING,... } proc_status_t;
5
CS414 C Programming Tutorial5 Dynamic memory allocation Memory must be explicitly allocated and freed #include int main() { /* declare and allocate memory */ int *ptr = (int *) malloc(sizeof(int)); *ptr = 4; /* use the memory */ free(ptr); /* free it for re-use */ }
6
CS414 C Programming Tutorial6 Example memory layout 0x0 0x2000 0x50000 code stack heap Dynamic allocation malloc(), free() Function calls, local variables Immutable
7
CS414 C Programming Tutorial7 Pointer initialisation Always initialise, even if only to NULL #include int main() { int x = 5; int *ptr; x = *ptr; /* unpredictable: initialise! */ ptr = &x; /* good initialisation */ *ptr = 4; /* bad initialisation */ *ptr = NULL; /* good 5 */ }
8
CS414 C Programming Tutorial8 Changing pointer types Pointers can be converted between types type_t *x = (type_t *) y; (void *) equivalent to any type, e.g. implementation of generic collections malloc() returns a void * Useful for low-level operations e.g. networking, memory allocation
9
CS414 C Programming Tutorial9 Parameter passing Functions can take pointer arguments int function(int *in, int *out); Two main purposes So that function can modify the variable (pass-by- reference) Efficiency: passing " big_struct_t x " is not efficient, passing &x is efficient Return values are often used to signal errors e.g. return value 0=="no error", non-zero==error
10
CS414 C Programming Tutorial10 Parameter passing void function(int *arg1, char **arg2) {... } int main(void) { int x = 5; int *y = &x; char string[] = "Hello, world!"; char *ptr = NULL; function(&x, NULL); function(y, &ptr); /* *ptr can be changed */ function(y, &string); /* probably wrong */ }
11
CS414 C Programming Tutorial11 Pointers and functions /* function returning integer */ int func(); /* function returning pointer to integer */ int *func(); /* pointer to function returning integer */ int (*func)(); /* pointer to function returning pointer to integer */ int *(*func)(); Hide this complexity with typedefs! e.g. typedef void (*interrupt_handler)(void *); void an_interrupt_handler(void *arg);
12
CS414 C Programming Tutorial12 Multiple modules #include #include "queue.h" int main() { queue_t queue; queue_init(&queue);... } #include "queue.h" void queue_init(queue_t* q) {... }... typedef struct {... } queue_t; void queue_init(queue_t* q); Standard library
13
CS414 C Programming Tutorial13 External definitions #include #include "defs.h" int main() {... if (tickcount > 5) set_timer(); } #include "defs.h" int tickcount = 0; void set_timer() { printf("Timer at %d.", tickcount); } /* defs.h */... extern int tickcount;...
14
CS414 C Programming Tutorial14 Hints and tips Avoid bugs in your program: don't index past the end of an array don't return pointers to local variables from functions initialise data properly, especially pointers check error codes (no exceptions in C) free() what you malloc() destroy all pointers to what you free() ... or else you might only find them when you least expect it!
15
CS414 C Programming Tutorial15 Hints and tips Program safely! Plan before you start Pay attention to compiler warnings Use assert() to check conditions are valid Indent your code to make it easy to read Beware of complicated #defines Use the debugger If you're serious about C, get K&R Lots of library functions for you to use
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.