2 Introduction to C - CMSC 216 Introduction to Computer Systems Lecture 2 Introduction to C Jan Plane Pete Keleher cfw_jplane [email protected] Chapter

2 Introduction to C - CMSC 216 Introduction to Computer...

This preview shows page 1 - 6 out of 23 pages.

2/4/11 1 CMSC 216 Introduction to Computer Systems Lecture 2 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
Image of page 1
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
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
Image of page 3
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
Image of page 4
2/4/11 5 Compiling a C program C programs must be compiled to be executed Use the gcc program to build your programs – invocation: gcc options source-files common options -g
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 23 pages?

  • Fall '11
  • Plane
  • Binary numeral system, Bitwise operation, CMSC, Herman, Sussman

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture