Last Updated 10/30/18
Program Structure Includes Function Declarations void main(void){ foo = fun1(a, b); fun2(2, c); if(fun1(c, d)) { } } Function 1 Definition Function 2 Definition 2 tj
Function Input and Output Input through actual parameters Output through return value Only one value can be returned Input/Output through side effects printf scanf 3 tj
Function Input and Output int main(void){ float checking; float savings; float int_rate; checking = update_acct(checking, int_rate); savings = update_acct(savings, int_rate); return 0; } float update_acct(float balance, float int_rate){ balance += balance * int_rate; return balance; } 4 tj
Pointer Review variables in memory address for myvar1 0x2000 0020 address for myvar2 0x2000 0024 address for myvar3 0x2000 0028 4 Byte word x x x x x x x x x 0x2000 0020 0x2000 0024 0x2000 0028 5 tj
Pointer A special Type A variable that holds the memory location of another variable Holds an address in our case 32 bits Each pointer must be tied to a specific data type int, float, char, 6 tj
Pointer To find the memory location of a variable use the address of operator: & &myvar1 0x2000 0020 &myvar2 0x2000 0024 &myvar3 0x2000 0028 Precedence Operator Description Associativity ++ -- Prefix increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) 7 tj
Pointer To declare a pointer variable follow the type declaration with a * int* myvar1_ptr; // declare a pointer variable with name myvar1_ptr // that holds the memory location of an integer variable float* myvar2_ptr; // declare a pointer variable with name myvar2_ptr // that holds the memory location of a float variable 8 tj
Pointer Precedence Operator Description Associativity ++ -- Prefix increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) To determine the value of a variable pointed to by a pointer variable precede the pointer variable with * (dereference operator) *myvar1_ptr; // provides the value held in the memory location // pointed to by myvar1_ptr (as an int) *myvar2_ptr; // provides the value held in the memory location // pointed to by myvar2_ptr (as a float) 9 tj
Pointer int foo1; float foo2; int foo3; int foo4; var1 0x2000 0010 0x2000 0014 int var1; float var2; // define a variable of type int // define a variable of type float var2 0x2000 0018 0x2000 001C 0x2000 0020 10 tj
Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 0x2000 001C 0x2000 0020 11 tj
Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 0x2000 001C int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float ptr1 0x2000 0020 ptr2 12 tj
Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float ptr1 = &var1; // set ptr1 to 0x2000 0010 ptr2 = &var2; // set ptr2 to 0x2000 0014 ptr1 ptr2 0x 2000 0010 0x 2000 0014 0x2000 001C 0x2000 0020 13 tj
Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 ptr1 = &var1; // set ptr1 to 0x2000 0010 ptr2 = &var2; // set ptr2 to 0x2000 0014 foo1 = *ptr1; // set foo1 to 5 foo2 = *ptr2; // set foo2 to 12.0 foo3 = &ptr1; // set foo3 to 0x2000 001C foo4 = &ptr2; // set foo4 to 0x2000 0020 ptr1 ptr2 0x 2000 0010 0x 2000 0014 0x2000 001C 0x2000 0020 14 tj
Pointers and functions Pointers allow us to use called functions to change values in the calling function Instead of passing variables in the parameter list (remember copies are made and then destroyed) we can pass pointers Pointers allow us to modify the passed variables by memory reference 15 tj
Pointers and functions Declaration Indicate that a pointer is being passed in the Formal Parameter List void update_acct(float* balance_ptr, float int_rate); 16 tj
Pointers and functions Definition Indicate that a pointer is being passed in the Formal Parameter List Operate on the variables pointed to by the pointers via the dereference operator void update_acct(float* balance_ptr, float int_rate){ *balance_ptr += *balance_ptr * int_rate; return; } 17 tj
Pointers and functions Usage Pass a pointer variable in the Actual Parameter List Pass the address to the variable in the Actual Parameter List int main(void){ float checking; float savings; float int_rate; float* check_ptr = &checking; update_acct(check_ptr, int_rate); update_acct(&savings, int_rate); return 0; } // ptr variable to a float variable 18 tj
Pointers and functions int main(void){ float checking; float savings; float int_rate; float* check_ptr = &checking; clear_acct(check_ptr); clear_acct(&savings); return 0; } // ptr variable to a float variable void clear_acct(float* balance_ptr){ *balance_ptr =0.0; return; } 19 tj
Pointers and functions int main(void){ int a; int b; swap(&a, &b); return 0; } void swap(int* x, int* y){ int tmp; tmp = *x; *x = *y; *y = tmp; return; } 20 tj
Pointers and functions int main(void){ int num; int den; int quo; int rem; divide(num, den, &quo, &rem); return 0; } void divide(int num, int den, int* quo, int* rem){ *quo = num / den; *rem = num % den; return; } 21 tj
Pointers and functions * circle.c Calculate the area and circumference of a circle Created by tj Rev 0, 11/15/16 */ #include <stdio.h> #define PI 3.14159 int main(void){ setbuf(stdout, NULL); // disable buffering // Local variables float radius; float circumference; float area; // Get input for radius printf("please enter a value for radius: "); scanf("%f", &radius); // Calculate circumference and area circumference = 2 * PI * radius; area = PI * radius * radius; // Output results printf("circumference = %f\n", circumference); printf("area = %f\n", area); return 0; } 22 tj
Standard Functions C standard functions C function reference https://en.wikipedia.org/wiki/c_standard_library 23 tj
Standard Functions I/O Functions C functions #include <stdio.h> printf() scanf() 24 tj
Standard Functions Math Functions #include <math.h> 25 tj
Standard Functions Math Functions Ceiling ceil(x) ceilf, ceill Smallest integer value the operand ceil(4.5) 5 ceil(-12.7) -12 Floor floor(x) floorf, floorl Largest integer value the operand floor(4.5) 4 floor(-12.7) -13 26 tj
Standard Functions Math Functions Truncate trunc(x) truncf, truncl Rounds toward 0 trunc(4.5) 4 trunc(-12.7) -12 Round round(x) roundf, roundl Rounds to nearest integer round(4.5) 5 round(-12.7) -13 27 tj
Standard Functions Math Functions Power pow(x,y) powf, powl x to the power y pow(3.0, 4.0) 81.0 pow(3.4, 2.3) 16.687893 Square Root sqrt(x) sqrtf, sqrtlf Square root of x sqrt(25) 5.0 28 tj
Standard Functions Math Functions Absolute value abs(x), fabs(x) labs, llabs, fabsf, fabsl if(fabs(a-b) < 0.000001){ 29 tj