0% found this document useful (0 votes)
31 views24 pages

7 Fundamental

The document provides an overview of some fundamental C programming concepts needed for iOS development, including why C is used, compiler history, variable declaration and data types like structs and pointers, operators, flow control, functions, and files. It explains that Objective-C is a superset of C and many iOS APIs use C. Understanding basic C concepts is important for iOS programming even when using Objective-C.

Uploaded by

Isti Qomah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views24 pages

7 Fundamental

The document provides an overview of some fundamental C programming concepts needed for iOS development, including why C is used, compiler history, variable declaration and data types like structs and pointers, operators, flow control, functions, and files. It explains that Objective-C is a superset of C and many iOS APIs use C. Understanding basic C concepts is important for iOS programming even when using Objective-C.

Uploaded by

Isti Qomah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

iOS 7 Programming Fundamental

(Just enough C)

RESUME BY ISTIQOMAH
C Programming Language

The reasons why using C programming:


Most of the iOS API involves the Objective-C
language, and most of your iOS programming will be
in the Objective-C language. Objective-C is superset
of C.
Some of the iOS Api involves C rather than
Objective-C. Even in Objective-C code, you often
need to use C data Structures and C function calls.
Compailer History

Originally, Xcodes compailer was the freeopen source


GCG. Eventually, Apple introduced its own free open
source compailer,LLVM, also referred to as Clang, thus
allowing for improvements that were impossible with
GCC. Changing compailer is scary, so Apple proceeded
in stage:
Compailer in every Xcode

In Xcode 3, along with both LLVM and GCC, Apple


supplied a hybrid compiler, LLVM-GCC, which provided
the advantages of LLVM compilation while parsing code
with GCC for maximum backward compatibility, without
making it the de fault compiler.
In Xcode 4, LLVM-GCC became the default compiler, but
GCC remained available.
In Xcode 4.2, LLVM 3.0 became the default compiler,
and pure GCC was withdrawn.
In Xcode 4.6, LLVM advanced to version 4.2.
In Xcode 5, LLVM-GCC has been withdrawn; the
compiler is now LLVM 5.0, and the transition from GCC
to LLVM is complete.
Variable Declaration, initialization and Data
Types

C is a strongly typed language. Every variable must


be declared, indicating its data type, before it can be
used.
declarations must precede all other statements, but
in modern versions of C, this rule is relaxed so that
you dont have to declare a variable until just before
you start using it:
int height = 2; int width = height * 2; height = height +
1; int area = height * width;
C offer few simple native data types, there are three
way: Structure, Pointer, and Arrays.
Structs

Structure, usually called a struct, is compound data


type (it combines multiple data type into a single
type, which can be passed around as a single entity.
Exemple:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
Recall that a CGFloat is basically a float, so this is a
compound data type made up of two simple native
data types; in effect, a CGPoint has two CGFloat parts,
and their names are x and y. (The rather odd-looking
last line merely asserts that one can use the term
CGPoint instead of the more verbose struct CGPoint.)
So we can write:
CGPoint myPoint;
myPoint.x = 4.3;
myPoint.y = 7.1;
Weve got a CGRect variable called myRect, a;ready
initialized. Then myRect.origin is a CGPoint, and
myRect.origin.x is CGFloat. You could change just
the width part of our CGRect directly, like this:
myRect.size.width = 8.6;
Pointer

The other big way that C extends its range of data types is
by means of pointers. A pointer is an integer (of some
size or other) designating the location in memory where
the real data is to be found.
int* intPtr; //intPtr is a pointer to an integer.
It is permitted to place the asterisk in the declaration
before the name rather than after the type:
int *intPtr;
int * intPtr;
You can put asterisk in all of ways. If you are asked what
type intPtr is, the answer is int* (a pointer to an int); the
asterisk is part of the name of the type of this variable.
Pointer

Pointer are very important in Onjective-C, because


Obejctive-C is all about objects, and every variable
referring to an object is itself a pointer.
In effect, Objective-C takes advantage of the fact that
a C pointer can designate real data whose nature and
bookkeeping are arbitrary. In this case, that real data
is an Objective-C object. Objective-C knows what this
means, but you generally wont worry about it
youll just work with the C pointer and let Objective-
C take care of the details.
Array

Array consists of multiple elements of the same data


type. An array declaration states the data type of the
elements, followed by the name of the array, along with
square brackets containing the number of elements:
int arr[3];
arr[0] = 123;
arr[1] = 456;
arr[2] = 789;

And:
int arr[] = {123, 456, 789};
Operators

Arithmetic operators are straightforward , but watch


out for the rule that integer division truncates any
fractional part.
++ Increasment.
-- Decreasment.
& and.
| Or.
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction= 1 <<1,
UIViewAnimationOptionBeginFromCurrentState= 1<< 2,
UIViewAnimationOptionRepeat= 1 << 3,
UIViewAnimationOptionAutoreverse = 1 << 4,
// ... };

<< symbol is the left shift operator. Value each of UI:


UIViewAnimationOptionLayoutSubviews
00000001
UIViewAnimationOptionAllowUserInteraction
00000010
UIViewAnimationOptionBeginFromCurrentState
00000100
UIViewAnimationOptionRepeat
00001000
UIViewAnimationOptionAutoreverse
00010000
Flow Control and Conditions

Basic flow control is fairly simple } else {


and usually involves a condition in statements;
parentheses and a block of
conditionally executed code in curly }
braces. while (condition) {
if (condition) { statements;
statements; }
} do { statements;
if (condition) { } while (condition);
statements; for (before-all; condition; after-
} else { each)
statements; {
} statements;
if (condition) {
}
statements;
} else if (condition) {
statements;
Flow Control and Conditions

NSString* key;
switch (tag) {
case 1: { // i.e., if tag is 1
key = @"lesson";
break;
}
case 2: { // i.e., if tag is 2
key = @"lessonSection"; break;
}
case 3: { // i.e., if tag is 3
key = @"lessonSectionPartFirstWord";
break;
}
}
Flow Control and Conditions

SomeType* oneItem;
for (oneItem in myCollection){
// ... statements ....
}
for (SomeType* oneItem in myCollection) { // ...
statements .... }
Function

A function is a block of code defining what should


happen; when other code calls (invokes) that
function, the functions code does happen. A
function returns a value, which is substituted for the
call to that function.
Int square ( int i) {
return i * i;
}
FUNCTION

1. We start with the type of value that the function returns; here, it returns an
int.
2. Then we have the name of the function, which is square.
3. Then we have parentheses, and here we place the data type and name of any
values that this function expects to receive. Here, square expects to receive
one value, an int, which we are calling i. The name i (along with its expected
data type) is a parameter; when the function is called, its value will be
supplied as an argument. If a function expects to receive more than one value,
multiple parameters in its definition are separated by a comma (and when the
function is called, the arguments supplied are likewise separated by a
comma).
4. Finally, we have curly braces containing the statements that are to be executed
when the function is called.
Function

Declaring, calling, and defining a function


int square(int i);
int i = square(3);
int square(int i) {
return i * i;
}
Pointer Parameters and the Address Oprator

Objective-C is chock-a-block with pointers (and


asterisks), because thats how Objective-C refers to an
object. Objective-C methods typically work with objects,
so they typically expect pointer parameters and return a
pointer value. But this doesnt make things more
complicated. Pointers are what Objective-C expects, but
pointers are also what Objective-C gives you. Pointers are
exactly what youve got, so theres no problem.
NSString* s1 = @"Hello, ";
NSString* s2 = @"World!";
NSString* s3 = [s1 stringByAppendingString: s2];
Pointer Parameters and the Address Oprator

Never mind for now what an id is, and dont worry


about the Objective-C method declaration syntax.
Just consider the types of the parameters. The first
one is an NSString*; thats no problem, as every
reference to an NSString is actually a pointer to an
NSString.
File

A large C program therefore usually consists of two


kinds of file: code files, whose filename extension is
.c, and header files, whose filename extension is .h.

File

So the strategy for constructing a large C program is


something like this:
In each .c file, put the code that only this file needs to
know about; typically, each files code consists of
related functionality.
In each .h file, put the function declarations that
multiple .c files might need to know about.
Have each .c file include those .h files containing the
declarations it needs to know about.
For IOS program, there is difference will be that
instead of .c files, youll use .m files, because .m is the
conventional filename extension for telling Xcode
that your files are written in Objective-C, not pure C.

You might also like