Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Last active August 24, 2016 23:31
Show Gist options
  • Save alexanderankin/8b2a053b7f52ccf7a660141ac90436db to your computer and use it in GitHub Desktop.
Save alexanderankin/8b2a053b7f52ccf7a660141ac90436db to your computer and use it in GitHub Desktop.
Learning C: extern keyword
#include <stdio.h>
#include <stdlib.h>
#include "other_file.h"
int x;
int main(int, char **);
int main(int argc, char **argv) {
x = 5;
calculate();
printf("%d\n", x);
}
#include "other_file.h"
extern int x;
void calculate() {
x = 6;
}
#ifndef other_file_header
#define other_file_header
void calculate(void);
#endif
gcc main_file.c -o main_file.o -c && \
gcc other_file.c -o other_file.o -c && \
gcc -o output main_file.o other_file.o &&\
./output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment