Top Banner
Preprocessor and Underlying C laungage Chorn Charly Leang Bunrong Cheng Udam Kan Vichhai Srou Chanthorn Em Vy Seth Sambo Chea Socheat
50

Presentation 5th

Jan 16, 2017

Download

Documents

GL Finance Plc.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Presentation 5th

Preprocessor and Underlying C laungage

Chorn Charly

Leang Bunrong

Cheng Udam

Kan Vichhai

Srou Chanthorn

Em Vy

Seth Sambo

Chea Socheat

Page 2: Presentation 5th

Content#define

#import

#include

#undef

Conditional compilications

Array

Function

Block

structure

Pointers

Miscellaneous Language Features

How Things Work

Page 3: Presentation 5th

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 point3. condictionally compile code( use to prevent Program).4. Reduce coding

Oh ! It is in .h file.

Preprocessor

Compiler(GCC)

.h file

Page 4: Presentation 5th

Preprocessor(cont.)

Source programCompiled code

Objective – C Preprocessor GCC(objective-C compiler)

Objective – C Compiler(e.g gcc)

Page 5: Presentation 5th

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:

Page 6: Presentation 5th

Preprocessor(cont.) #import (#include in C language) is the preprocessor to insert the text of the file Header File.hInto 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).

Page 7: Presentation 5th

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).

Page 8: Presentation 5th

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).

Page 9: Presentation 5th

Preprocessor(cont.) To define Constant by configure Xcode.

Page 10: Presentation 5th

Preprocessor(cont.) To define Constant by configure Xcode. Double click on debug.and add new Constant

Identifier or constant

Page 11: Presentation 5th

Preprocessor(cont.)Example:

Page 12: Presentation 5th

Preprocessor(cont.) #undef is used to reset macro.

Syntax:#undef macroname

Page 13: Presentation 5th

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

Page 14: Presentation 5th

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.

10 5 -3 17 82

11 0 0 8 -7

32 20 1 0 14

0 0 8 7 6

Example in this case it like matrix 4 * 5 show as below

Int M[4][5] = {

{10 , 5 , -3 , 17 ,82 },

{9 , 0 , 0 , 8 , -7} ,

{32 , 20 , 1 , 0 , 14 },

{0 , 0 , 8 , 7 , 6}

};

Page 15: Presentation 5th

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

Page 16: Presentation 5th

Block(cont.)

• Syntax

return-type(^block-name)(parameter-type) = ^(parameter-type and name){

//block-body

}

Page 17: Presentation 5th

Block

• default of block or this way

\

• Accessing

• it is similar to function when we call block to use

Page 18: Presentation 5th

Block with parameter• we can use block with parameter and return type

• accessing

Page 19: Presentation 5th

Block with parameter(cont.)

• it can take multiple parameter and more

• accessing

Page 20: Presentation 5th

Access outside variable in block

• you can not access outside variable in the block if you try, you will get error like this

Page 21: Presentation 5th

Access outside variable in block(cont.)

• solution

• it mean that you declare variable for block can use it

Page 22: Presentation 5th

Block as function parameter

• we can use block as a parameter of a function

• file.h

Page 23: Presentation 5th

Block as function parameter(cont.)

• file.m

implement body

accessing method

Page 24: Presentation 5th

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)

Page 25: Presentation 5th

Block as function parameter(cont.)

• file.m

we do not need to define a name (optional)

Page 26: Presentation 5th

Block as function parameter(cont.)

• main method

we need to assign name to it

Page 27: Presentation 5th

Structures

• Structure is an entity that contains various types of variable in it

Page 28: Presentation 5th

Structures(Cont.)

• Initialize a structure

Page 29: Presentation 5th

Structures(cont.)

• Accessing variables in structures

Page 30: Presentation 5th

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 variableso 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

Let create other variable that using pointer. Example: int *b; b

10

Page 31: Presentation 5th

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:

Page 32: Presentation 5th

Pointer(cont.)

If we write int *d=10, it will look like:

For now let assign our pointer variable to other value.

Result

D

Page 33: Presentation 5th

Pointer(cont.)

• Let’s see how our pointer get value from variable a:

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.

10

0x103FFd

a

Page 34: Presentation 5th

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

x3100x3101

x3102

x3103x3104

address

*bx3100

Page 35: Presentation 5th

Pointer and structure

• We have learned about the structure already. So ,now let make it work with our pointer.

Page 36: Presentation 5th

Pointer and structure(cont.)

• So how can we access structure members in our structure?

• Let try to print out all the value:

Or

Or

Page 37: Presentation 5th

Pointer and Function

• Let create function with parameters as pointer :

Page 38: Presentation 5th

Pointer and Function(cont.)

• How to call and use it :

• Result:

Page 39: Presentation 5th

Pointer and Function(cont.)

• So let call swap for the second time:

• The result will be like

Page 40: Presentation 5th

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;

Page 41: Presentation 5th

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

Page 42: Presentation 5th

The Comma Operator

• The comma operator ( , ) allows you to use two or expressions where only one expression is expected. Look at the example:

• 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.

Output:

Page 43: Presentation 5th

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:

Page 44: Presentation 5th

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.

Page 45: Presentation 5th

Command-Line Argument (cont.)

• Click Product Scheme Edit Scheme then show this dialog, click add argument.

Page 46: Presentation 5th

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.

Page 47: Presentation 5th

• 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.

How Things Works (cont.)

Page 48: Presentation 5th

• 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.

How Things Works (cont.)

Page 49: Presentation 5th

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 (;)

Page 50: Presentation 5th

Thank You