C basics

626 views

Published on

These slides recalls some basic concepts from C language

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
626
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C basics

  1. 1. TECNOLOGIAS EMERGENTES EM JOGOS Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014 February 14, 2014 Daniela da Cruz dcruz@ipca.pt
  2. 2. C BASICS
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 18. C basics C FUNCTIONS There are four components to a C function: its return value, name, parameters, and associated code block. 18
  19. 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. 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. 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. 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. 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. 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. 25. C basics C SUMMARY We reviewed: → Variables → Conditionals → Loops → Typedef → Struct's → Enum's → Arrays → Pointers → Functions → Static (functions and variables) 25

×