3 Introduction to C

3 Introduction to C - 2/4/11 CMSC 216 Introduction to...

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

View Full Document Right Arrow Icon
2/4/11 1 CMSC 216 Introduction to Computer Systems Lecture 3 Introduction to C Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu A QUICK START WITH C Chapter 1, Reek CMSC 216 - Wood, Sussman, Herman 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
2/4/11 2 Comparison between C and Java • C is procedural, not object-oriented • C is fully compiled (to machine code), not to bytecode • C allows direct manipulation of memory via pointers • C does not have garbage collection • Many of the basic language constructs in C act in similar ways to the way they work in Java • C has many important, yet subtle, details CMSC 216 - Wood, Sussman, Herman 3 An example C program • The following is C's version of the "Hello world" program: #include <stdio.h> int main() { printf("Hello world\n"); return 0; } • How does it accomplish its goal? – includes library header file with function declarations (so call to printf() compiles OK) – provides definition of main() function, where all C programs begin – returns from main() to end program - the value returned can be checked by the program that invoked this one CMSC 216 - Wood, Sussman, Herman 4
Background image of page 2
2/4/11 3 Important caveats • You will be writing C code that conforms to the C90 standard - this means a few things have to be kept in mind: – All comments must be of the /* */ variety; C90 does not recognize // (single-line) comments – All variables must be declared at the beginning of a block (immediately after an opening brace); failure to do this will trigger "mixed declarations and code" error messages CMSC 216 - Wood, Sussman, Herman 5 Terrible style if (condition) do_something(); always_do_this(); if (x) y = j; while (condition) x++; if (c) { f(); g();} /* this funtion do stuf */ /* call f with value of 10 */ f(5); • These examples are all bad style and you will lose credit for using them – statements executed conditionally should be put on separate lines from the condition – closing braces should be first thing on a line – comments should be spelled properly, and have correct and useful information CMSC 216 - Wood, Sussman, Herman 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
2/4/11 4 C function declarations • Function prototype declarations provide information about a function's return type and parameters, but do not define the function • Using function declarations allows the compiler to check your function calls for correctness • Examples: void foo(int, int, double); int bar(double x, double y); CMSC 216 - Wood, Sussman, Herman 7 Declarations and the Preprocessor #include – provides ability to include declarations from other files • usually have the file name extension .h – generally only include function prototypes, and type and variable declarations – actual code is kept in a different file, usually with the extension .c • files with code are compiled individually to object code • and then linked together to create a file with executable code Two ways to use #include #include <stdio.h> • for standard system files #include "swap.h" • for user-written files CMSC 216 - Wood, Sussman, Herman 8
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 18

3 Introduction to C - 2/4/11 CMSC 216 Introduction to...

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

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