2 Introduction to C

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

Info iconThis preview shows pages 1–4. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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 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 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 2/4/11...
View Full Document

Page1 / 23

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

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

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