What use is theexternkeyword in C?
It tells the compiler that a variable or function is defined elsewhere and should be resolved by the linker. Functions are implicitly extern'd unless you specify static. Variables aren't, but if you declare a variable with extern, it won't allocate any memory for it, just point to a variable with that name declared somewhere else, and the linker figures out where.
>>54772678
When you declare external global variables, you have to use extern. For external functions, it's implicit if you don't provide the body of the function.// in top level scope
int foo; // declares 'foo', and creates space for it
extern int foo; // declares 'foo', but the location of its memory is defined in another file
extern int bar (); // declares 'bar', but its implementation is in another file
int bar (); // same as above
int bar () { return foo; } // provides implementation for 'bar' function.
Usage:// game.h
#pragma once
// all external definitions
extern int points;
void reset_game ();
void collect_star ();
// game.c
#include "game.h"
int points; // we are actually creating the variable here
void reset_game ()
{
points = 0;
}
void collect_star ()
{
points += 10;
}
// main.c
#include <stdio.h>
#include "game.h"
int main (int argc, char** argv)
{
int i;
reset_game();
for (i = 0; i < 5; i++)
collect_star();
printf("points: %d\n", points);
return 0;
}
>>54772678
extern = I'm declaring this variable here because I'll use it, but it's actually defined somewhere else.
>>54772759
how are the statements in game.h definitions??
>>54772896
They're not. The definitions are in the implementation file, game.c.
>>54772678
>>54772858
Go home lain, maki is our new mascot. You're obsolete and replaced.
>>54772753
Good post. Here's a (You).
>>54775286
cuck
>>54772678
Lain, why do you care about C?
>>54775286
>some dumb slut from an idol show is more /g/-relevant than the god of the wired
Retard
>>54776615
is that lisp?
>>54776669
Oh Lain, did you erase your memory again?
>>54776669
yes
>>54775286
Maki BTFO