C_lecture_2

C_lecture_2 - CS 11 C track lecture 2 Last week basics of C...

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

Unformatted text preview: CS 11 C track: lecture 2 Last week: basics of C programming compilation data types ( int , float , double , char , etc.) operators ( +- * / = == += etc.) functions conditionals loops preprocessor ( #include ) This week Preprocessor ( #define) Operators and precedence Types and type conversions Function prototypes Loops ( while, do/while ) More on input/output and scanf() Commenting Using the make program This week Preprocessor ( #define) Operators and precedence Types and type conversions Function prototypes Loops ( while, do/while ) More on input/output and scanf() Commenting Using the make program #define (1) So far, only preprocessor command we know is #include Lots of other ones as well will see more later in course One major one: #define Used in almost all C header files #define (2) #define usually used to define symbolic constants: #define MAX_LENGTH 100 Then preprocessor substitutes the number 100 for MAX_LENGTH everywhere in program NOTE: Just a textual substitution! no type checking #define (3) #define MAX_LENGTH 100 /* later... */ int i; /* later... */ if (i > MAX_LENGTH) { printf("Whoa there!\n"); } #define (4) /* That code expands into: */ if (i > 100) { printf("Whoa there!\n"); } Note that all occurrences of MAX_LENGTH replaced with 100 Why not just write 100 in the first place? #define (5) Why not just write 100 in the first place? If you decide you want to change MAX_LENGTH to another number instead only have to change one #define statement and all occurrences of MAX_LENGTH will be changed to the new number Hard-coded numbers like 100 are called magic numbers usually repeated many times in a program would have to change many lines to change the number throughout the program Digression: ? : operator C has one ternary operator (three arguments), the ? : ("question mark") operator Like an if statement that returns a value: int i = 10; int j; j = (i == 10) ? 20 : 5; /* note 3 args */ /* "(i == 10) ? 20 : 5" means: * "If i equals 10 then 20 else 5." */ Not used very often #define macros #define can also be used to define short function-like macros e.g....
View Full Document

This note was uploaded on 03/21/2012 for the course CS 11 taught by Professor Pinkston,d during the Spring '08 term at Caltech.

Page1 / 47

C_lecture_2 - CS 11 C track lecture 2 Last week basics of C...

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

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