CS157: Intro to C, Part II

Spring 2018

Includes

See this page as a slide show

User Includes

CS157 Includes

User includes

#include

#include

#include

// main.c
#include <stdio.h>
#include "swap.h"

void printNicely(int x, int y);

int main() {
    int x = 9, y = 2;
    printNicely(x, y);
    swap(&x, &y);
    printNicely(x, y);
    return 0;
}

void printNicely(int a, int b) {
    printf("(%d,%d)\n", a, b);
}
// swap.h
void swap(int *a, int *b);
// swap.c
#include "swap.h"
void swap(int *m, int *q) {
    int tmp = *m;
    *m = *q;
    *q = tmp;
}
% c11 -c swap.c
% c11 -c main.c
% c11 main.o swap.o

Global Variables

extern

The extern modifier tells the compiler that a variable is defined outside of the current file.

// other.c
int flag = 10;
// main.c
extern int flag;
int main() {
    return flag;
}

You probably shouldn’t need to use this (globals are bad, mmmmkay?).

Modularity: Reuse

Modularity: Multiple Files

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2017-12-19T11:20

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building