SlideShare a Scribd company logo
1 of 25
Download to read offline
TECNOLOGIAS EMERGENTES EM JOGOS

Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014

February 14, 2014

Daniela da Cruz
dcruz@ipca.pt
C BASICS
C basics

C  COMMENTS
There are two ways to include commentary text in a C program.

→

Inline comments

// This is an inline comment
→

Block comments

/* This is a block comment.
It can span multiple lines. */

3
C basics

C  VARIABLES
Variables are containers that can store dierent values.

type name
the = operator.

To declare a variable, you use the
to assign a value to it you use

syntax, and

double odometer = 9200.8;
int odometerAsInteger = (int)odometer;

4
C basics

C  CONSTANTS

The const variable modier tells to the compiler that a variable is
never allowed to change.

double const pi = 3.14159;

5
C basics

C  ARITHMETIC
The familiar +, -, *, / symbols are used for basic arithmetic
operations, and the modulo operator (%) can be used to return the
remainder of an integer division.

printf(6
printf(6
printf(6
printf(6
printf(6

+
*
/
%

2
2
2
2
2

=
=
=
=
=

%d,
%d,
%d,
%d,
%d,

6
6
6
6

+
*
/

2);
2);
2);
2);
6 % 2);

// 8
// 4
// 12
// 3
// 0

6
C basics

C  ARITHMETIC (2)
C also has increment (++) and decrement () operators. These are
convenient operators for adding or subtracting 1 from a variable.

int i = 0;
printf(%d, i);
i++;
printf(%d, i);
i++;
printf(%d, i);

// 0
// 1
// 2

7
C basics

C  RELATIONAL/LOGICAL OPERATORS
The most common relational/logical operators are shown below.

Operator
a == b

Description
Equal to

!=b
b
a = b
a  b
a = b

Greater than or equal to

!a

Logical negation

a

a

Not equal to
Greater than
Less than
Less than or equal to

a  b

Logical and

||

Logical or

a

b

8
C basics

C  CONDITIONALS
C provides the standard if statement found in most programming
languages.

int modelYear = 1990;
if (modelYear  1967) {
printf(That car is an antique!!!);
} else if (modelYear = 1991) {
printf(That car is a classic!);
} else if (modelYear == 2014) {
printf(That's a brand new car!);
} else {
printf(There's nothing special about that car.);
}
9
C basics

C  CONDITIONALS (2)
C also includes a
integral types 

switch

statement, however it

only works with

not oating-point numbers, pointers, or

Objective-C objects.

switch (modelYear) {
case 1987:
printf(Your car is from 1987.); break;
case 1989:
case 1990:
printf(Your car is from 1989 or 1990.); break;
default:
printf(I have no idea when your car was made.);
break;
}
10
C basics

C  LOOPS
The

while

the related

for loops can be used for iterating over values,
break and continue keywords let you exit a loop
and

and

prematurely or skip an iteration, respectively.

int modelYear = 1990;
int i = 0;
while (i5) {
if (i == 3) {
printf(Aborting the while-loop);
break;
}
printf(Current year: %d, modelYear + i);
i++;
}
11
C basics

C  LOOPS (2)
int modelYear = 1990, i;
for (i=0; i5; i++) {
if (i == 3) {
printf(Skipping a for-loop iteration);
continue;
}
printf(Current year: %d, modelYear + i);
}

12
C basics

C  TYPEDEF
The

typedef

keyword lets you create new data types or redene

existing ones.

typedef unsigned char ColorComponent;
int main() {
ColorComponent red = 255;
return 0;
}

13
C basics

C  STRUCTS
A struct is like a simple, primitive C object. It lets you aggregate
several variables into a more complex data structure, but doesn't
provide any OOP features (e.g., methods).

typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
} Color;
int main() {
Color carColor = {255, 160, 0};
printf(Your color is (R: %u, G: %u, B: %u),
carColor.red, carColor.green, carColor.blue);
return 0;
}
14
C basics

C  ENUMS
The

enum

keyword is used to create an enumerated type, which is a

collection of related constants.

typedef enum {
FORD,
HONDA,
NISSAN,
PORSCHE
} CarModel;
int main() {
CarModel myCar = NISSAN;
return 0;
}
15
C basics

C  ARRAYS
The higher-level
the

NSArray

and

NSMutableArray

classes provided by

Foundation Framework are much more convenient than C

arrays.

int years[4] = {1968, 1970, 1989, 1999};

16
C basics

C  POINTERS
A pointer is a direct reference to a memory address.

→

The reference operator () returns the memory address of a
normal variable. This is how we create pointers.

→

The dereference operator (*) returns the contents of a
pointer's memory address.

int year = 1967;
int *pointer;
pointer = year;
printf(%d, *pointer);
*pointer = 1990;
printf(%d, year);

17
C basics

C  FUNCTIONS
There are four components to a C function: its return value, name,
parameters, and associated code block.

18
C basics

C  FUNCTIONS
Functions need to be dened before they are used.
C lets you separate the

implementation.
→

declaration of a function from its

A function declaration tells the compiler what the function's
inputs and outputs look like.

→

The corresponding implementation attaches a code block to
the declared function.

Together, these form a complete function denition.

19
C basics

C  STATIC FUNCTIONS
The

static

keyword let us alter the availability of a function or

variable.
By default, all functions have a global scope.
This means that as soon as we dene a function in one le, it's
immediately available everywhere else.
The

static

specier let us limit the function's scope to the current

le, which is useful for creating private functions and avoiding
naming conicts.

20
C basics

C  STATIC FUNCTIONS (2)
File

functions.m:

// Static function declaration
static int getRandomInteger(int, int);
// Static function implementation
static int getRandomInteger(int minimum, int maximum) {
return ((maximum - minimum) + 1) + minimum;
}
You would not be able to access

main.m.

getRandomInteger()

from

Note that the static keyword should be used on both the function
declaration and implementation.
21
C basics

C  STATIC VARIABLES

Variables declared inside of a function are reset each time the
function is called.
However, when you use the

static

modier on a local variable, the

function remembers its value across invocations.

22
C basics

C  STATIC VARIABLES (2)
int countByTwo() {
static int currentCount = 0;
currentCount += 2;
return currentCount;
}
int main(int argc, const char * argv[]) {
printf(%d, countByTwo());
// 2
printf(%d, countByTwo());
// 4
printf(%d, countByTwo());
// 6
}

return 0;

23
C basics

C  STATIC VARIABLES (2)

This use of the static keyword

does not aect the scope of local

variables.
Local variables are still only accessible inside of the function itself.

24
C basics

C  SUMMARY
We reviewed:

→

Variables

→

Conditionals

→

Loops

→

Typedef

→

Struct's

→

Enum's

→

Arrays

→

Pointers

→

Functions

→

Static (functions and variables)

25

More Related Content

What's hot

C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
sangeethajames07
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Vivek Singh
 
Lec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory ConceptLec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory Concept
Rushdi Shams
 

What's hot (20)

C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment6
Assignment6Assignment6
Assignment6
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
Savitch Ch 09
Savitch Ch 09Savitch Ch 09
Savitch Ch 09
 
Savitch ch 09
Savitch ch 09Savitch ch 09
Savitch ch 09
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
C++ book
C++ bookC++ book
C++ book
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
C introduction by piyushkumar
C introduction by piyushkumarC introduction by piyushkumar
C introduction by piyushkumar
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
C Programming
C ProgrammingC Programming
C Programming
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Assignment8
Assignment8Assignment8
Assignment8
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Lec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory ConceptLec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory Concept
 

Similar to C basics

UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
cuptopointer-180804092048-190306091149 (2).pdf
cuptopointer-180804092048-190306091149 (2).pdfcuptopointer-180804092048-190306091149 (2).pdf
cuptopointer-180804092048-190306091149 (2).pdf
YashwanthCse
 

Similar to C basics (20)

UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
C programming
C programmingC programming
C programming
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Hello world! Intro to C++
Hello world! Intro to C++Hello world! Intro to C++
Hello world! Intro to C++
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
cuptopointer-180804092048-190306091149 (2).pdf
cuptopointer-180804092048-190306091149 (2).pdfcuptopointer-180804092048-190306091149 (2).pdf
cuptopointer-180804092048-190306091149 (2).pdf
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 

More from Daniela Da Cruz

More from Daniela Da Cruz (9)

Introduction to iOS and Objective-C
Introduction to iOS and Objective-CIntroduction to iOS and Objective-C
Introduction to iOS and Objective-C
 
Games Concepts
Games ConceptsGames Concepts
Games Concepts
 
Game Development with AndEngine
Game Development with AndEngineGame Development with AndEngine
Game Development with AndEngine
 
Interactive Verification of Safety-Critical Systems
Interactive Verification of Safety-Critical SystemsInteractive Verification of Safety-Critical Systems
Interactive Verification of Safety-Critical Systems
 
Android Introduction
Android IntroductionAndroid Introduction
Android Introduction
 
Android Lesson 3 - Intent
Android Lesson 3 - IntentAndroid Lesson 3 - Intent
Android Lesson 3 - Intent
 
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
Comment Analysis approach for Program Comprehension (Software Engineering Wor...Comment Analysis approach for Program Comprehension (Software Engineering Wor...
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
 
Android Lesson 2
Android Lesson 2Android Lesson 2
Android Lesson 2
 
Android Introduction - Lesson 1
Android Introduction - Lesson 1Android Introduction - Lesson 1
Android Introduction - Lesson 1
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

C basics

  • 1. TECNOLOGIAS EMERGENTES EM JOGOS Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014 February 14, 2014 Daniela da Cruz dcruz@ipca.pt
  • 3. C basics C COMMENTS There are two ways to include commentary text in a C program. → Inline comments // This is an inline comment → Block comments /* This is a block comment. It can span multiple lines. */ 3
  • 4. C basics C VARIABLES Variables are containers that can store dierent values. type name the = operator. To declare a variable, you use the to assign a value to it you use syntax, and double odometer = 9200.8; int odometerAsInteger = (int)odometer; 4
  • 5. C basics C CONSTANTS The const variable modier tells to the compiler that a variable is never allowed to change. double const pi = 3.14159; 5
  • 6. C basics C ARITHMETIC The familiar +, -, *, / symbols are used for basic arithmetic operations, and the modulo operator (%) can be used to return the remainder of an integer division. printf(6 printf(6 printf(6 printf(6 printf(6 + * / % 2 2 2 2 2 = = = = = %d, %d, %d, %d, %d, 6 6 6 6 + * / 2); 2); 2); 2); 6 % 2); // 8 // 4 // 12 // 3 // 0 6
  • 7. C basics C ARITHMETIC (2) C also has increment (++) and decrement () operators. These are convenient operators for adding or subtracting 1 from a variable. int i = 0; printf(%d, i); i++; printf(%d, i); i++; printf(%d, i); // 0 // 1 // 2 7
  • 8. C basics C RELATIONAL/LOGICAL OPERATORS The most common relational/logical operators are shown below. Operator a == b Description Equal to !=b b a = b a b a = b Greater than or equal to !a Logical negation a a Not equal to Greater than Less than Less than or equal to a b Logical and || Logical or a b 8
  • 9. C basics C CONDITIONALS C provides the standard if statement found in most programming languages. int modelYear = 1990; if (modelYear 1967) { printf(That car is an antique!!!); } else if (modelYear = 1991) { printf(That car is a classic!); } else if (modelYear == 2014) { printf(That's a brand new car!); } else { printf(There's nothing special about that car.); } 9
  • 10. C basics C CONDITIONALS (2) C also includes a integral types switch statement, however it only works with not oating-point numbers, pointers, or Objective-C objects. switch (modelYear) { case 1987: printf(Your car is from 1987.); break; case 1989: case 1990: printf(Your car is from 1989 or 1990.); break; default: printf(I have no idea when your car was made.); break; } 10
  • 11. C basics C LOOPS The while the related for loops can be used for iterating over values, break and continue keywords let you exit a loop and and prematurely or skip an iteration, respectively. int modelYear = 1990; int i = 0; while (i5) { if (i == 3) { printf(Aborting the while-loop); break; } printf(Current year: %d, modelYear + i); i++; } 11
  • 12. C basics C LOOPS (2) int modelYear = 1990, i; for (i=0; i5; i++) { if (i == 3) { printf(Skipping a for-loop iteration); continue; } printf(Current year: %d, modelYear + i); } 12
  • 13. C basics C TYPEDEF The typedef keyword lets you create new data types or redene existing ones. typedef unsigned char ColorComponent; int main() { ColorComponent red = 255; return 0; } 13
  • 14. C basics C STRUCTS A struct is like a simple, primitive C object. It lets you aggregate several variables into a more complex data structure, but doesn't provide any OOP features (e.g., methods). typedef struct { unsigned char red; unsigned char green; unsigned char blue; } Color; int main() { Color carColor = {255, 160, 0}; printf(Your color is (R: %u, G: %u, B: %u), carColor.red, carColor.green, carColor.blue); return 0; } 14
  • 15. C basics C ENUMS The enum keyword is used to create an enumerated type, which is a collection of related constants. typedef enum { FORD, HONDA, NISSAN, PORSCHE } CarModel; int main() { CarModel myCar = NISSAN; return 0; } 15
  • 16. C basics C ARRAYS The higher-level the NSArray and NSMutableArray classes provided by Foundation Framework are much more convenient than C arrays. int years[4] = {1968, 1970, 1989, 1999}; 16
  • 17. C basics C POINTERS A pointer is a direct reference to a memory address. → The reference operator () returns the memory address of a normal variable. This is how we create pointers. → The dereference operator (*) returns the contents of a pointer's memory address. int year = 1967; int *pointer; pointer = year; printf(%d, *pointer); *pointer = 1990; printf(%d, year); 17
  • 18. C basics C FUNCTIONS There are four components to a C function: its return value, name, parameters, and associated code block. 18
  • 19. C basics C FUNCTIONS Functions need to be dened before they are used. C lets you separate the implementation. → declaration of a function from its A function declaration tells the compiler what the function's inputs and outputs look like. → The corresponding implementation attaches a code block to the declared function. Together, these form a complete function denition. 19
  • 20. C basics C STATIC FUNCTIONS The static keyword let us alter the availability of a function or variable. By default, all functions have a global scope. This means that as soon as we dene a function in one le, it's immediately available everywhere else. The static specier let us limit the function's scope to the current le, which is useful for creating private functions and avoiding naming conicts. 20
  • 21. C basics C STATIC FUNCTIONS (2) File functions.m: // Static function declaration static int getRandomInteger(int, int); // Static function implementation static int getRandomInteger(int minimum, int maximum) { return ((maximum - minimum) + 1) + minimum; } You would not be able to access main.m. getRandomInteger() from Note that the static keyword should be used on both the function declaration and implementation. 21
  • 22. C basics C STATIC VARIABLES Variables declared inside of a function are reset each time the function is called. However, when you use the static modier on a local variable, the function remembers its value across invocations. 22
  • 23. C basics C STATIC VARIABLES (2) int countByTwo() { static int currentCount = 0; currentCount += 2; return currentCount; } int main(int argc, const char * argv[]) { printf(%d, countByTwo()); // 2 printf(%d, countByTwo()); // 4 printf(%d, countByTwo()); // 6 } return 0; 23
  • 24. C basics C STATIC VARIABLES (2) This use of the static keyword does not aect the scope of local variables. Local variables are still only accessible inside of the function itself. 24
  • 25. C basics C SUMMARY We reviewed: → Variables → Conditionals → Loops → Typedef → Struct's → Enum's → Arrays → Pointers → Functions → Static (functions and variables) 25