02-C-SurvivalGuide

02-C-SurvivalGuide - Copyright : University of Illinois CS...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Copyright : University of Illinois CS 241 Staff 1 C Survival Guide Good news: Writing C code is easy! void* myfunction() { char *p; *p = 0; return (void*) &p; } Copyright : University of Illinois CS 241 Staff 6 Bad news: Writing BAD C code is easy! void* myfunction() { char *p; *p = 0; return (void*) &p; } Copyright : University of Illinois CS 241 Staff 7 Bad news: Writing BAD C code is easy! void* myfunction() { char *p; *p = 0; return (void*) &p; } Copyright : University of Illinois CS 241 Staff 7 Bad news: Writing BAD C code is easy! void* myfunction() { char *p; *p = 0; return (void*) &p; } Copyright : University of Illinois CS 241 Staff 7 How do I write good C programs? Fluency in C syntax Stack vs. Heap Key skill: read code for bugs Do not rely solely on compiler warnings, if any, and testing C is powerful - it's the System Programmer's choice language Copyright : University of Illinois CS 241 Staff 8 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient and portable Does not protect the programmers from themselves. Rationale: programmers know what they are doing. UNIX and most serious system software (servers, compilers, etc) are written in C. Can do everything Java and C++ can. But complex tasks could look ugly in C. Copyright : University of Illinois CS 241 Staff 9 C vs. C++ Problem Object oriented languages provided nice features to programmers, but were very, very slow Solution The development of C++ C enhanced with objects Programming Challenge All syntax you use in this class is valid for C++ Not all C++ syntax youve used, however, is valid for C Copyright : University of Illinois CS 241 Staff 10 Key Differences between C and C++ Input/Output C does not have iostreams C: printf("hello world\n); C++: cout<<"hello world<<endl; Heap memory allocation C: malloc()/free() int *x = malloc(8 * sizeof(int)); free(x); C++: new/delete int *x = new int[8]; delete(x); Copyright : University of Illinois CS 241 Staff 11 Compiler gcc Preprocessor Compiler Linker See manual man for options: man gcc "Ansi-C" standards C89 versus C99 C99: Mix variable declarations and code (for int i=) C++ inline comments //a comment make a compilation utility Google 'makefile' Copyright : University of Illinois CS 241 Staff 12 Programming in C C = Variables + Instructions Copyright : University of Illinois CS 241 Staff 13 What well show you You already know a lot of C from C++: int my_fav_function(int x) { return x+1; } Key concepts for this lecture: Pointers Memory allocation Arrays Strings 10 What well show you You already know a lot of C from C++: int my_fav_function(int x) { return x+1; } Key concepts for this lecture: Pointers Memory allocation Arrays Strings 10 Theme: how memory really works Pointers 11 Variables...
View Full Document

Page1 / 99

02-C-SurvivalGuide - Copyright : University of Illinois CS...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online