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

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

View Full Document Right Arrow Icon
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 )
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
#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
Background image of page 3

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

View Full DocumentRight Arrow Icon
#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
Background image of page 4
#define (3) #define MAX_LENGTH 100 /* later. .. */ int i; /* later. .. */ if (i > MAX_LENGTH) { printf("Whoa there!\n"); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
#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?
Background image of page 6
#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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
#define macros ± #define can also be used to define short function-like macros e.g. #define MAX(a, b) \ (((a) > (b)) ? (a) : (b)) ± Like a short function that gets expanded everywhere it's used (a.k.a. an inline function) ± But pitfalls exist (won't discuss further)
Background image of page 9

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

View Full DocumentRight Arrow Icon
#define style ± #define defines new meaning for names ± Names that have been defined using #define are conventionally written with ALL_CAPITAL_LETTERS ± That way, they're easy to identify in code ± Conversely, don't use this style for regular variable names
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

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

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

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