C Introduction
Functions, Header Files

CS 2130: Computer Systems and Organization 1

April 6, 2026

Announcements

C Reference Guide

Calling Functions

The C code

long a = f(23, "yes", 34uL);

compiles to

movl $23, %edi
leaq label_of_yes_string, %rsi
movq $34, %rdx
callq f
# %rax is "long a" here

without respect to how f was defined. It is the calling convention, not the type declaration of f, that controls this.

Calling Functions

But, if the C code has access to the type declaration of f, then it might perform some implicit casting first; for example, if we declared

long f(double a, const char *b, double c);

long a = f(23, "yes", 34uL);

then the call would be interpreted by C as having implicit casts in it:

long a = f((double)23, "yes", (double)34uL);

Calling Functions

and the arguments would be passed in floating-point registers, like so:

movl $23, %eax
cvtsi2sd %eax, %xmm0           # first floating-point argument

leaq label_of_yes_string, %rdi # first integer/pointer argument

movl $34, %eax
cvtsi2sd %eax, %xmm1           # second floating-point argument

callq f
# %rax is "long a" here

Function Declaration

int f(int x);

We want this in every file that invokes f()

Function Definition

int f(int x) {
    return 2130 * x;
}

We only want this in one .c file

Header Files

C header files: .h files

Big Picture

Header files

C files

Including Headers

#include "myfile.h"

#include <string.h>

Macros

#define NAME something else

#define NAME(a,b) something b and a

Lexical replacement, not semantic

Interesting Example

#define TIMES2(x)  x * 2        /* bad practice */
#define TIMES2b(x) ((x) * 2)    /* good practice */

int x = ! TIMES2(2 + 3);




int y = ! TIMES2b(2 + 3);

Examples and More

Memory

The Heap

The heap: unorganized memory for our data

The Heap: Requesting Memory

void *malloc(size_t size);

The Heap: Freeing Memory

Freeing memory: free

void free(void *ptr);