- Variables and Types
- short , int , long
- float , double
- Char
- pointer
- A pointer holds a memory address.It is declared using the asterisk character.
- For example,a variable declared as int * can hold a memory address where an int is stored.
- It does not hold the actual number's value,but if you know the address of the int,the you can get to its value.
- Struct
- A type made up of other types
- If/else
- If(…){…}
- If(…){…}else{…}
- If(…){…}else if(…){…}else{…}
- …?a:b
- Comparison operators
< > , <= , >= , == , !=
- Logical operators
&& , || , !
- Boolean variables
- C programmers have always used an int to hold a boolean value
- Objective-C programmers typically use the type BOOL (an alias for an integer type)
- Functions
- Void congratulateStudent(char *student ,char *course , int numDays)
congratulateStudent("Kate","Cocoa",5)
- Local variables are variables declared inside a function. They exist only during the execution of that function and can only be accessed from within that function.
- A function can have many local variables , and all of them are stored in the frame for that function.
- Programmers use the word stack to describe where the frames are stored in memory. When a function is called , its frame is pushed onto the top of stack.
- When a function finishes executing , we say that it returns. That is , it pops its frame off the stack and lets the function that called it resume execution.
- Scope - Any pair of braces, whether they are a part of a function definition, an if statement, or a loop, defines its own scope that restricts the availability of any variables declared within them.
- Recursion - function call itself
- Debugger - setting a breakpoint
- Return - return 0 for success / return 1 for error || return EXIT_SUCCESS / EXIT_ERROR (need import stdlib.h)
- Global variables - can be accessed from any function at any time
- Static variables - only accessible from the code in the file where it was declared
- Format Strings
- Using tokens - The token you choose tells print() how the variable's value should be formatted.(%s - string / %d - integer(decimal))
- Escape sequences - \n & \"
- Numbers
- Integers
- An unsigned 8-bit number can hold any integer from 0 to 255.(2^8 = 256)
- A signed 64-bit number can hold any integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- When you declare an integer , you can be very specific:
- UInt32 x; //An unsigned 32-bit integer
- SInt16 y; //A signed 16-bit integer
- Common use
- Char a; //8 bits
- Short b; //Usually 16 bits(depending on the platform)
- Int c; //Usually 32 bits(depending on the platform)
- Long d; //32 or 64 bits(depending on the platform)
- Long long e; 64 bits
- Tokens for displaying integers
- %d - an integer as a decimal number
- %o - base-8(octal)
- %x - base-16(hexadecimal)
- %u - unsigned decimal number
- Integer operations
- +,-,* work as you would expect
- When you divide one integer by another , you get 11 / 3 =3 (get a third integer)
- Integers
11 divided by 3 is 3 with a reminder of 2
- % - return the remainder
- Convert the int to a float using the cast operator(cast your denominator as a float before you do the division)
- NSInteger and NSUInteger
- They are 32-bit integers on 32-bit systems & 64-bit integers on 64-bit systems.
- NSInteger is signed , NSUInteger is unsigned.
- It is recommended that you cast them to the appropriate long before trying to display them
- Operator shorthand
- X++ / x--
- X += y / x -= y / x *= y / x /= y / x %= y
- Absolute value: abs() - for int return / labs() for long return (both functions are declared in stdlib.h)
- Floating-point numbers
- How they stored: a mantissa multiplied by 10 to an integer exponent(eg.345.32 is throught of as 3.4532 * 10^2)
- A 32-bit floating number has 8 bits dedicated to holding the exponent(a signed integer) and 23 bits dedicated to holding the mantissa , with the remaining 1 bit used to hold the sign.
- Unlike integers , floating-point numbers are always sigend:
- Float g; //32 bits
- Double h; //64 bits
- Long double; //128bits
- Tokens for displaying floating-point numbers
- %f uses normal decimal notation
- %e uses scientific notation
- Math library
- Terminal - man math
- Use - #include <math.h>
- Loop
- The while loop
- The for loop
- Break / continue
- The do-while loop
- User input : readline() / atoi()
- Address and Pointers
- The memory is numbered , and we typically talk about the address of a particular byte of data.
- When people talk about a 32-bit CPU or a 64-bit CPU , they are typically talk about how big the address is.
- Everything is stored in memory and thus everything has an address.(Variable , function …)
- Float *str; (ptr is a variable that is a pointer to a float / It does not store the value of a float ; it can hold an address where a float may be stored)
- If you have an address , you can get the data stored there using the * operator.
- Two different ways of using asterisk
- When you declared addressOfI to be an int * .That is , you told the complier "It will hold an address where an int can be stored."
- When you read the int value that is stored at the address stored in addressOfI.
- You can also use the * operator on the left-hand side of an assignment to store data at a particular address.
- Printf("A point is %zu bytes\n",sizeof(int *);
- %zu - a placeholder token for a vaule of type size_t
- Pointer is 4 bytes long - program is running in 32-bit mode
- Pointer is 8 bytes long - program is running in 640bit mode
- NULL
- We use NULL for a pointer to nothing
- NULL is zero. //if(NULL) == false
- When using pointers to objects,you will use nil instead of NULL. They are equivalent , but Objective-C programmers use nill to mean the address where no object lives.
- Stylish pointer declarations
- Float *powerPtr / float* powerPtr are both ok for complier
- Float *powerPtr is recommended ( Because you can declare mutiple variables in a single line)
- Float x,y,z; //Each one is a float
- Float* b,c; //b is a pointer to a float , but c is just a float
- Pass-by-Reference
- When you call modf() , you will supply an address where it stash one of the numbers. In particular , it will return the fractional part and copy the integer to the address you supply.
- Pass-by-Reference is you supply an address(as knows as a reference) and the function puts the data here.
- Avoid derefencing NULL - if(pointer) {…}
- Structs
- A variable to hold several related chunks of data
- Usage1:
- Struct Person{
Float heightInMeters;
Int weightInKilos;
}
- Struct Person evan;
Evan.heightInMeters = 1.8;
Evan.weightInKilos = 85;
Struct Person tom;
Tom.heightInMeters = 1.7;
Tom.weightInKils = 60;
- Usage2:(typedef)
- typedef struct {
float heightInMeters;
int weightInKilos;
} Person;
- Person evan;
Evan.heightInMeters = 1.8;
Evan.weightInKilos = 85;
Person tom;
Tom.heightInMeters = 1.7;
Tom.weightInKilos = 60;
- The heap
- Programmers often use the word buffer to mean a long line of bytes of memory.
The buffer comes form a region of memory known as the heap , which is separate from the stack.
- On the heap , the buffer is independent of any function's frame. Thus, if can be used across many functions.
- You request a buffer of memory using the function malloc().
When you are done using the buffer, you call the function free() to release your claim on that memory and return it to the heap.
- Usage1: need a chunk of memory big enough to hold 1,000 floats
Float *stackOfBuffer;
startOfBuffer = malloc(1000 * sizeof(float));
//use the buffer here …
Free(startOfBuffer);
startOfBuffer = NULL;
- Usage2: allocate a buffer on the heap for a struct
Person *mikey = (Person *)malloc(sizeof(person));
Mikey -> weightInKilos = 96;
Mikey -> heightInMeters = 1.7;
Free(mikey);
Mikey = NULL;
- The operator ->
- The code p->weightInKilos says, "Dereference the pointer p to the struct and get me the member called weightInKilos."
- Variables and Types