Lesson 20 Notes

Readings

Assignment

Lesson Outline

Admin

Structs

Remember objects from Java and other High Level Languages? C has a rough equivalent called structs. They are collections of variables, but don't have their own functions / methods.

General case:

struct <name>
{
    <type> <var1>;
    <type> <var2>;
    ...
};

struct <name> <variable_name>;
<variable_name>.<var1> = <value>;
<variable_name>.<var2> = <value>;

Example:

// Header
// #includes
// #defines

struct point {
    char x, y;
};

struct circle {
    struct point center;
    char radius;
};

void main(void)
{
    // You can create and initialize them like this:

    struct point center = {20, 7};
    struct circle myCircle = {center, 5};

    // You can use dot notation to access variables:

    center.x = 10;
    circle.radius = 25;
}

Functions

Functions are the C equivalent to assembly subroutines or Java methods. They allow you to make your code modular and reusable.

Function Call

General Case:

<variable> func_name(<variable 1>, ...);

Example:

// Header
// #includes
// #defines

void main(void)
{
    unsigned int mySummation;
    unsigned char maxN = 42;

    mySummation = summation(23);
    mySummation = summation(maxN);
}

Function Prototype

General case:

<output_type> func_name(<input type 1> <variable name 1>, ...);

Note: function parameters / output can be void.

Example:

// Header
// #includes
// #defines

unsigned int summation (unsigned char n);

void main(void)
{
    unsigned int mySummation;
    unsigned char maxN = 42;

    mySummation = summation(23);
    mySummation = summation(maxN);
}

Function Definition

Just by convention, I prefer to define function prototypes above main and implementations below it.

General Case:

<output_type> func_name(<input type 1> <variable name 1>, ...)
{
    // Some interesting stuff
    return <output variable>;
}

Example:

// Header
// #includes
// #defines

unsigned int summation (unsigned char n);

void main(void)
{
    unsigned int mySummation;
    unsigned char maxN = 42;

    mySummation = summation(23);
    mySummation = summation(maxN);
}

unsigned int summation(unsigned char n)
{
    // recursion!
    if (n <= 0)
        return 0;
    else
        return n + summation(n-1);
}

Header and Implementation Files

Preprocessor Commands

Last time, we talked a little about preprocessor directives (#define and #include). The preprocessor is executed before your code compiles. It handles any lines that start with #<some_comand> <params>. The following are the preprocessor commands you will use:

C Headers

#ifndef _ATD_HELPER_H_
#define _ATD_HELPER_H_

// Your header file code (typedefs, function prototypes, #defines, etc.)
// Use good comment headers to define each function (see example)
// ...

#endif // _ATD_HELPER_H

C Implementation Files

#include "atd_helper.h"

// Function definitions
// ...

Example

Program Requirements

math_helper.h

// Your high-quality header with author / description / revision history
#ifndef _MATH_HELPER_H_
#define _MATH_HELPER_H_

// Useful constants
                            //  (estimate)      (actual)
#define PI  (339 / 108)     //  3.139       vs  3.142
#define E  (155 / 57)       //  2.719       vs  2.718

// Note: you would add some really good headers before each of these
//       functions to describe their purpose.  In the interest of
//       brevity, I'm omitting them here.
unsigned int summation(unsigned char n);
unsigned int factorial(unsigned char n);
char max(char a, char b);
char min(char a, char b);

#endif // _MATH_HELPER_H_

math_helper.c

#include "math_helper.h"

unsigned int summation(unsigned char n)
{
    if (n <= 0)
        return 0;
    else
        return n + summation(n-1);
}

unsigned int factorial(unsigned char n)
{
    if (n <= 0)
        return 1;
    else
        return n * factorial(n-1);
}

char max(char a, char b)
{
    return (a > b) ? a : b;
}

char min(char a, char b)
{
    return (a < b) ? a : b;
}

main.c

#include "math_helper.h"

void main(void)
{
    char a = 10;
    char b = 15;
    char maxVar, minVar;
    unsigned int sum, fact;

    sum = summation(a);
    fact = factorial(6);
    maxVar = max(a, b);
    minVar = min(a, b);

    while(1){};                 // trap CPU
}