[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
What use is the extern keyword in C?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 6
File: rain.jpg (10 KB, 454x324) Image search: [Google]
rain.jpg
10 KB, 454x324
What use is the
 extern
keyword 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.
>>
File: rain.jpg (13 KB, 399x369) Image search: [Google]
rain.jpg
13 KB, 399x369
>>54772825
Oh
>>
>>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).
>>
File: 1460257610678.jpg (400 KB, 1211x1500) Image search: [Google]
1460257610678.jpg
400 KB, 1211x1500
>>54775286
cuck
>>
File: mitinanime_lain3.jpg (39 KB, 640x480) Image search: [Google]
mitinanime_lain3.jpg
39 KB, 640x480
>>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
>>
File: 85.jpg (804 KB, 1660x2010) Image search: [Google]
85.jpg
804 KB, 1660x2010
>>54776615
is that lisp?
>>
File: lisp_apple_Lg.png (239 KB, 512x384) Image search: [Google]
lisp_apple_Lg.png
239 KB, 512x384
>>54776669
Oh Lain, did you erase your memory again?
>>
>>54776669
yes
>>
>>54775286
Maki BTFO
Thread replies: 16
Thread images: 6

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.