Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee

Similar presentations


Presentation on theme: "The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee"— Presentation transcript:

1 The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee

2 The Preprocessor

3 The #define Statement Analogous to doing a search and replace with a text editor; in this case, the preprocessor replaces all occurrences of the defined name with its associated text. It is convention that all defined names be capitalized. #define TRUE 1 #define FALSE 0 #define PI You can reference other defined values in your definitions as long as everything is defined at the time the defined name is used in the program. #define PI #define TWO_PI 2.0 * PI or #define TWO_PI 2.0 * PI #define PI

4 #define IS_LEAP_YEAR year % 4 == 0 && year % 100 != 0 \ || year % 400 == 0... if ( IS_LEAP_YEAR )... Normally, the preprocessor assumes that a definition is contained on a single line of the program. If a second line is needed, the last character on the line must be a backslash character. This character signals a continuation to the preprocessor and is otherwise ignored. The same holds true for more than one continuation line; each line to be contin- ued must end with a backslash character.

5 Macros Definitions are frequently called macros. This terminology is more often applied to definitions that take one or more arguments. This macro, called SQUARE, simply squares its argument: #define SQUARE(x) x*x

6 The #import Statement #import "metric.h" The double quotation marks around the header filename instruct the preprocessor to look for the specified file in one or more file directories (typically, first in the directory that contains the source file, but the actual places the preprocessor searches can be specified in Xcode). Enclosing the filename within the characters instead, as in #import causes the preprocessor to look for the include file only in the special “system” header file directory or directories; the current directory will not be searched.

7 Conditional Compilation The #ifdef, #endif, #else, and #ifndef Statements #ifdef IPAD # define #else # define #endif

8 Underlying C Language Features

9 Arrays int x[50]; x[0] = 1; x[49] = 1; Initializing Array Elements int integers[5] = { 0, 1, 2, 3, 4 }; char word[] = { 'H', 'e', 'l', 'l', 'o', '!' };

10 Multidimensional array int M[4][5] = { { 10, 5, -3, 17, 82 }, { 9, 0, 0, 8, -7 }, { 32, 20, 1, 0, 14 }, { 0, 0, 8, 7, 6 } }; Indexing elements M[slow][fast] M[1][4]

11 Blocks Blocks are a recent extension to the C language. They are not part of the standard ANSI C definition and were added to the language by Apple, Inc. Blocks look and act a lot like functions. The syntax takes some getting used to. You can pass arguments to blocks, just like you can to functions. You can also return a value from a block. Unlike a function, a block can be defined inside a function or method, and gets to access any variables defined outside the block that are within its scope. In general, such variables can be accessed, but their values cannot be changed.There is a special __block modifier (that’s two under- score characters that precede the word block) that enables you to modify the value of a variable from inside the block.  Blocks can be passed as arguments to functions and methods, and in Part II,“The Foundation Framework,” you’ll learn about some of the methods that expect to see a block passed as an argument. One of the advantages of blocks is that they can be dispatched by the system for execution by other processors or by other threads within your application.

12 Example void printMessage (void) { NSLog is fun."); } Here’s a block that accomplishes the same task: ^(void) { NSLog is fun."); } A block is identified by a leading caret ^ character. It’s followed by the parenthesized argument list that the block takes. In our case, our block takes no arguments, so we write void just as we did in the function definition.

13 You can assign this block to a variable called printMessage, as long as the variable is properly declared: void (^printMessage)(void) = ^(void) { NSLog is fun."); }; To the left of the equal sign we specify that printMessage is a pointer to a block that takes no arguments and returns no value. Note that the assignment statement is terminated by a semicolon. Executing a block referenced by a variable is done the same way a function is called: printMessage ();

14 A block can access variables within the scope in which it’s defined. The value of that variable is the value it has at the time the block is defined. You can’t by default modify the value of a variable defined outside a block. int main (int argc, const char * argv[]) { int foo = 10; void (^printFoo)(void) = ^(void) { NSLog = %i", foo); }; foo = 15; printFoo (); } return 0; } :33: Chapt13[4148:707] foo = 10

15 __block modifier int main (int argc, const char * argv[]) { __block int foo = 10; void (^printFoo)(void) = ^(void) { = %i", foo); foo = 20; // ** THIS LINE GENERATES A COMPILER ERROR }; foo = 15; printFoo (); NSLog = %i", foo); } return 0; } :41: Chapt13[4213:707] foo = :41: Chapt13[4213:707] foo = 20

17 Pointers Pointers enable you to effectively represent complex data structures, change values passed as arguments to functions and methods, and more concisely and efficiently deal with arrays. int x,count = 10; int *intPtr; intPtr = &count; x = *intPtr; *intPtr = 5; indirection operator address operator

18 Pointers and Structures struct date { int month; int day; int year; }; struct date today, *datePtr; datePtr = &today; datePtr->month = 9; datePtr->day = 25; datePtr->year = 2011; NSLog date is %i/%i/%.2i.", datePtr->month, datePtr->day, datePtr->year % 100); x->y == (*x).y

19 Pointers and Arrays #import int arraySum (int* array, int n); int arraySum (int* array, int n) { int sum = 0, *ptr; int *arrayEnd = array + n; for ( ptr = array; ptr < arrayEnd; ++ptr ) sum += *ptr; return (sum); } int main (int argc, const char * argv[]) { int values[10] = { 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 }; NSLog sum is %i", arraySum (values, 10)); } return 0; } declaration of function :51: Chapt13[2941:707] The sum is 21

20 The sizeof Operator Objective-C provides an operator called sizeof that you can use to determine the size of a data type or object. The sizeof operator returns the size of the specified item in bytes. sizeof (int) sizeof (struct data_entry)

21 Command-Line Arguments int main (int argc, char *argv[]) { } argc tells number of arguments + 1 each arguments can be reference by argv[0] upto argv[argc-1]


Download ppt "The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee"

Similar presentations


Ads by Google