Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

6) C structs and functions

Today:

  1. Structs

  2. Functions

1. Structs

Definition

Pseudo-code:

struct[structure tag]
{
    member definition;
    ...
    member definition;

} [one or more structure variables]

Example:

struct Books
{
    char title[50];
    char author[50];
    char subject[100];
    int year;
    double price;
} book;
book.price = 49.99;
struct Books cBook = {"Programming in C", "Steve G. Kochan", " Programming languages", 2004, 49.95}; // initialize all members (in order)

struct Books cBook = {.author = "Steve G. Kochan", .year = 2004}; // initialize only specific members with the dot operator

Example:

#include <stdio.h>
#include <string.h>

// inner (or dependend) structure
struct Employee
{
    int employee_id;
    char name[20];
    int salary;
};
// outer structure
struct Organization
{
    char orgName[20];
    char orgNumber[20];
    struct Employee emp;
};

int main(void)
{
    struct Organization org;
    // access members of the inner struct by using the dot operator twice
    org.emp.employee_id = 153;
    org.emp.salary = 60000;

    return 0;
}
struct car
{
    char make[20];
    char model[30];
    int year;
};
struct car arr_car[10];

// assign the model member of the 3rd element of the array of structs
arr_car[2].model = "Legacy";

2. Functions

Most of this section material is adopted from this resource.

Functions can be thought of as named blocks of code that (if good software design principles are followed) perform one task. When you write a program you will write many functions to perform the tasks you need. There are, however, a lot of common tasks such as displaying text to the screen that a lot of programmers will need. Instead of having everyone reinventing the wheel, GNU systems (Unix-like operating system) come with libraries of pre-defined functions for many of these tasks. Over the years, thousands of such functions have accumulated in (standard) libraries.

Every C program must have a function called main(), this is where execution of the program begins. In principle, the code of a program could be completely contained in main() but it is more usual and practical to split a program into many small functions that perform single tasks.

First you must define the function, just like we defined main() in the examples so far. Also you you must declare it. Declaring a function is like telling the compiler to expect it; we didn’t have to declare main() because it is a special function and the compiler knows to expect it. The name, or identifier, you give to a function must appear in both the definition and the declaration.

Along with it’s identifier you must give each function a type and a block of code. The type tells the compiler what sort of data it returns. The return value of a function can be ignored. For instance, printf() returns an integer saying how many character it displayed to the terminal, but if it worked correctly, i.e., it printed something to the standard output (your screen) then it is not super helpful to know that value (but it can be for debugging purposes).

Example:

functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

/* function declarations */
int first_function(void);
int goodbye(void);


int main()            // function definition
{
  printf("the program begins...\n");
  first_function();
  goodbye();

  return 0;
}


int first_function()  // function definition
{
  /* this function does nothing */
  return 0;
}


int goodbye()         // function definition
{
  printf("...and the program ends.\n");

  return 0;
}

In the above example we wrote first_function(), which does nothing and goodbye(), which displays a message.

In our case this means they must appear before our definition of main(). In practice, function declarations are generally grouped at the top of a file after any #include lines and before any function definitions or can be in their own header file included with the #include directive. Once you have placed the function declarations you wish to share into a header file you can #include your header in each C file that needs the information