1. Preprocessor and Underlying C laungage
Chorn Charly
Leang Bunrong
Cheng Udam
Kan Vichhai
Srou Chanthorn
Em Vy
Seth Sambo
Chea Socheat
2. Content
#define
#import
#include
#undef
Conditional compilications
Array
Function
Block
structure
Pointers
Miscellaneous Language Features
How Things Work
3. Preprocessor
Preprocessor is the initial program. the first step in compile files it need through initial the
program.(note preprocessor not finish with “ ; ”).
Preprocessor is begin with #.
Preprocessor advantages:
1. Define constants(UpperCase All for name convention).
2. Import files to files at specifice point
3. condictionally compile code( use to prevent Program).
4. Reduce coding
Oh ! It is in
.h file.
Preprocessor
Compiler(GCC)
.h file
5. Preprocessor(cont.)
Preprocessor are: #define,#include,#import and conditional compilation ..etc.
1. #define is used to define constants.it also is used to define macro to make easy for develper
that can use own keyword to develop own project( reduce line of codes).
Syntax: #define constantName value
Example:
6. Preprocessor(cont.)
#import (#include in C language) is the preprocessor to insert the text of the file Header File.h
Into file being processed at the point of the #import line.
Syntax1: #import “headerName.h”
Syntax2:#import <package/headerName.h>
Example:
Syntax1 is used to import files (class,category...etc) in own project (new class that implement
by new generation).
Syntax2 is used to import files (package,class...etc) in othe package to our project(build in
classes , libraries ..etc.(Foundation.h,UIKit...etc).
7. Preprocessor(cont.)
#if,#ifdef,#ifndef proprocessor is like if statement. but it execute during compile
time(condictionally during compile time).
Syntax1:
#if identifier(macro or constant)
//statements(macro )
#else
// statements(macro)
#endif
Example:
Note: #define is no space . In #if preprocessor between # and define keyword is got a space
character.(#define => # define).
8. Preprocessor(cont.)
#ifdef....#endif,#ifndef .......#endif is executed as #if the same.objective – C new version
recomment that should not use #if .it can duplicate include file in compile time.
Syntax1:
#ifndef identifier(macro or constant)
//statements(macro )
#else
// statements(macro)
#endif
Note:identifier(macro or constant) we can configure Xcode to create it.
#if , #ifdef is executed when identifier=1(true) but #ifndef is executed when identifier=0(false).
13. Array
• Objective-c array represent an entire set of values. Then each value store in the
different element of the set using a number called an index number. Index number start
from 0 to n-1. you can use array anywhere the variable dose. It similar to variable you
need to declare before use. Declare an array involves declaring the type of element that
will be contained in the array.
Int fibonacci [15]; //declare an array named fibonacci with the 15 size
Int integers[5] = {0,1,2,3,4}; // declare an array with initialize
Char letters[5] = {‘a’,’b’,’c’,’d’,’e’}; //declare an array with character
Int a[10] = { [9] = 1 , [2]=3 , [1]=2 , [0]=1}; //declare an array with initialize base the specific type
14. Array cont….
• The array we used in the last example was a one dimensional array. Arrays can have more than one
dimension, these arrays-of-arrays are called multidimensional arrays. They are very similar to
standard arrays with the exception that they have multiple sets of square brackets after the array
identifier. A two dimensional array can be though of as a grid of rows and columns.
This section will take a look at the two dimension array.
Example in this case it like matrix 4 * 5 show as below
10 5 -3 17 82
9 0 0 8 -7
32 20 1 0 14
0 0 8 7 6
Int M[4][5] = {
{10 , 5 , -3 , 17 ,82 },
{9 , 0 , 0 , 8 , -7} ,
{32 , 20 , 1 , 0 , 14 },
{0 , 0 , 8 , 7 , 6}
};
15. Block(cont.)
• it can have parameter also
• it can be the parameter of function
• we use it as a parameter, it mean that we want to get the value of the processing of
block in the parameter
• it can not use global variable
• it can only use its own variable in block
20. Access outside variable in block
• you can not access outside variable in the block if you try, you will get error like this
21. Access outside variable in block(cont.)
• solution
• it mean that you declare variable for block can use it
22. Block as function parameter
• we can use block as a parameter of a function
• file.h
23. Block as function parameter(cont.)
• file.m
implement body
accessing method
24. Block as function parameter(cont.)
• block as parameter can take two or more parameter in it and return value also
• note parameter-name for block is not importance we do not need to declare the name
but when we call it we need to assign name to it
file.h
we do not need to define a name (optional)
25. Block as function parameter(cont.)
• file.m
we do not need to define a name (optional)
26. Block as function parameter(cont.)
• main method
we need to assign name to it
30. Pointer
• What is pointer?
A variable that is able to store the address of another variable is called a "pointer”.
Allows us to indirectly access variables
o In other words, we can talk about its address rather than its value
• Syntax to create pointer:
Datatype *pointer_name.
example: int *a;
Simple int or float or double variables, they are like a box which can store a single int or float
or double value. Example: int a=10; a
10
Let create other variable that using pointer. Example: int *b; b
31. Pointer(cont.)
Can we write int *d = 10 ? NO WE CANNOT. As I told you above pointer variable can only store address
not value. If we write it in Xcode it will show the warning for us.
And if we still want to print out the *d value we will get this result.
Result:
32. Pointer(cont.)
If we write int *d=10, it will look like:
For now let assign our pointer variable to other value.
Result
D
33. Pointer(cont.)
• Let’s see how our pointer get value from variable a:
10
a
d 0x103FF
Variable d is only store address that reference to the variable a.
• Array and pointer;
I will create an array with 5 elements within it.
And I will create a pointer that point to it.
34. Pointer(cont.)
• Let see how it work in memory
• Let print out the value of b:
• So, how can we print out the value of 20?
It will print out 20.
• What does *(d+1) mean?
It means that now we are moving the address of d to one block forward.
a[5]
10
20
30
40
50
value
address
x3100
x3101
x3102
x3103
x3104
*b
x3100
35. Pointer and structure
• We have learned about the structure already. So ,now let make it work with our pointer.
36. Pointer and structure(cont.)
• So how can we access structure members in our structure?
• Let try to print out all the value:
Or
Or
40. The goto Statement
• goto statement cause a direct to specified point in the program.
• To identify where the program to be made you have to create label.
• This label can be located anywhere in the function before or after the goto.
• Syntax:
goto labelName;
labelName: statement;
41. The Null Statement
• A null statement consist of only a semicolon ( ; ) and perform no operation. When you
place a semicolon ( ; ) wherever a normal program statement appear, known as the null
statement.
• Sometime in the program you place it at the end statement but you can place it after
while, for, do statements to terminate loop expression.
• Example:
null statement
42. The Comma Operator
• The comma operator ( , ) allows you to use two or expressions where only one
expression is expected. Look at the example:
Output:
• When you evaluate between two operands while assign value to variable a it’ll take the
result and discard the result of variable b.
• One more thing is using in for statement expression.
43. The sizeof Operator
• Although you never make clear about size of data type in your program, sometimes you
need to know this information.
• Now you can use the sizeof operator to determines sizes.
• Example:
44. Command-Line Arguments
• Similar to Java when you want to pass some value from the command line to your
Objective-C program when they are executed. These value are called command line
arguments.
• The command line are handle using main() function arguments.
46. How Things Works
Because of Objective-C language has the C language underneath so this section
will explain how relationship between Objective C and C language and there are four
factors that we will talk about it.
Fact 1 : Instance Variables Are Store in Structures
In C language to manipulate instance variables or to defines a physically grouped
list of variables to be placed under one name. On the other hand, In Objective C we use
class to manipulate instances and objects.
47. How Things Works (cont.)
• Fact 2 : An Object Variable Is Really a Pointer
Objective-C has pointers because it is an evolution of C, which used pointers
extensively. In C you may usually use pointer with array or structure and so on. Objective-
C provides convenient access to memory addresses through the use of pointers to
particular types. The main advantage of pointer types is that they allow you to interpret
any memory address as the potential location of a variable.
48. How Things Works (cont.)
• Fact 3 : Methods Are Functions, and Message Expressions Are Function Calls
Methods are really function. In Objective C, there are sender and receiver that
sender send the message and receiver will receive the message from sender. The way
that we send or receive the message is call sending message that mean calling function.
• Fact 4 : The id Type is Generic Pointer Type
Objective-C provides support for dynamic typing via the id data type. The id type is
a unique Objective-C type that can hold a pointer to any type of Objective-C object,
regardless of its class.
49. Block
• Block is use like a function but it have some difference from function
• Block can declare out of class it is not follow by class, it can declare everywhere
• Block can not create in file.h, it can create only file.m
• if we create in file.h it is not error at compile time but it is error at runtime
• it follow by Caret sign (^)
• it follow by the semi-colon (;)
This case , I focus on Structures not Struct keyword.. That mean in C to manipulate instance we use struct{}
But in Objective-C we use class to manipulate it.
That mean in C language we use pointer to declare array pointer variable and so on cos pointer reference to address in memory
SO Object variable in Objective C is a Pointer too.