Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

static anus

Name: Anonymous 2011-12-11 22:42

are variables declared in C headers static even if you don't put static with them?

Name: Anonymous 2011-12-11 22:49

Here's a hint, run gcc with just the -E flag and the name of your .c source file.

Name: Anonymous 2011-12-11 22:53

declare my anus

Name: Anonymous 2011-12-12 0:18

A global variable defined in a source file will be accessible to other source files, as long as the other source files see a declaration for the variable, which you can do like this:

globals.c

char* advertisement = "Try ALTOIDS Today!";


globals.h

extern char* advertisement;


main.c

#include<stdio.h>

#include "globals.h"

int main(int argc, char** argv) {
  printf("%s!!\n", advertisement);
  return 0;
}

Name: Anonymous 2011-12-12 10:46

>>1

No. The compiler does not differentiate between header and source files.

Variable declarations within file scope without a storage class specifier have external linkage. C, however, accepts tentative definitions, so you will not have linking errors if you simply declare: int variable; (in contrast with something like int variable = 0;, which is a definition with initialization.)

This may look like if variable was static, but it is not.

Name: Anonymous 2011-12-12 11:16

>>1
There is no such thing as a header file to the compiler.

Name: Anonymous 2011-12-12 12:00

>>6
so wait, mean if i have a
global.h with "int anus = 0;" in it
and i include global to multiple .c files

each .c file will have its own int anus = 0;?

or how does this work

MY MIND I HURTING NOW

Name: Anonymous 2011-12-12 12:26

>>7
No, the linker will allocate the storage only once and make all variables refer to it. Unless you declared it as `static`, in which case each compilation unit gets its own copy.

Though you are encouraged to follow the following pattern: declare the variable in the header as `extern`, then declare it normally exactly once somewhere where you also include the header. This prevents most of bad shit which could happen if the variable is declared with different types in different places.

Name: Anonymous 2011-12-12 14:30

Why are we even talking about globals, you shouldn't be using them in the first place.

Name: Anonymous 2011-12-12 14:34

>>9
AbstractSingletonFactoryFactory

Name: Anonymous 2011-12-12 14:38

>>10

No factories, just copy constructors. Go suck a dick.

Name: Anonymous 2011-12-12 15:59

>>11
C
constructors

Name: Anonymous 2011-12-12 17:38

>>12
You don't use constructors in C?
Your C code must be really bad.

Name: Anonymous 2011-12-13 3:24

>>8
No, the linker will allocate the storage only once and make all variables refer to it.

That's not how C works.

Name: Anonymous 2011-12-13 5:36

>>7

yeap.

Doing

globals.h

int i = 0;


f1.c

#include "globals.h"

int f1() {
  ...
}


f2.c

#include "globals.h"

int f2() {
  ...
}


Is equivalent to:

f1.c

int i = 0;

int f1() {
  ...
}


f2.c

int i = 0;

int f2() {
  ...
}



So there will be an i defined in f1.c and an i defined in f2.c. If you were to compile both f1.c and f2.c, you'd get a linker error, saying that there are two symbols with the same name between f1.o and f2.o.

Name: Anonymous 2011-12-13 6:15

>>15
extern int i;

Name: Anonymous 2011-12-13 9:09

>>14-16
There's one thing that always amazes me in idiots and it's their impenetrable belief that they are right. I fully understand that in fact it is the reason they are idiots, and everything else, like being ignorant and stupid, are the consequences.

But still I see someone like that guy, who not only corrected me, but also WROTE THE WHOLE FUCKING TEST CODE, but did not spend five more seconds to see if it indeed produces a linker error, and I just sit here in profound amazement. How the fuck is that possible?


#!/bin/sh

cat >test1.c <<*EOF*
#include <stdio.h>
int x;
int f(void);
int main(int argc, const char * argv[])
{
    printf("%d %d\n", x, f());
}
*EOF*

cat >test2.c <<*EOF*
int x = 42;
*EOF*

cat >test3.c <<*EOF*
int x;
int f(void) { return x + 1; }
*EOF*

gcc -c test1.c
gcc -c test2.c
gcc -c test3.c
gcc -o test test1.o test2.o test3.o
./test


HOLY SHIT NO FUCKING ERRORS AND IT WORKS CORRECTLY WOW!

Name: >>17 2011-12-13 9:32

To see that it's not a bug in GCC, you have to download the C99 standard: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf, then head to the section 6.2.2 "Linkages of identifiers" and read it. Paragraph 5 says it as clear as possible:

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
The `extern` specifier is fucking implied, if there's no other fucking storage specifier. Except for one little detail: if all declarations use actual `extern`, you do get a linker error. Also, you get a linker error if you include initializer more than once (section 6.9.2). Hence, the standard way of declaring global variables: use `extern` in the header, include the header in the corresponding code file, define and initialize the variable there. This way you are safe from most silly errors where you, say, changed the definition but failed to recompile some object files.

Name: Anonymous 2011-12-13 9:34

Relying on the object format considered harmful.
You linker could make it so can't even link GNU/Freedom produced code.

Name: Anonymous 2011-12-13 9:37

>>19
Learn English first, please.

Name: Anonymous 2011-12-13 9:37

>>18
Did you burst a blood vessel in your eye spazzing out so hard?

Name: >>18 2011-12-13 9:39

And holy shit, I'm still amazed by >>15, I mean, what the actual fuck? I'd have understood if he just wrote some unsubstantiated bullshit. But he spent some time writing up actual files, several minutes probably! And then BAM, he ain't got no five seconds more to compile them. What. The. Fuck.

Name: Anonymous 2011-12-13 9:54

>>17,18,22

   _,,....,,_  _人人人人人人人人人人人人人人人_
-''":::::::::::::`''>   Take it easy !!!         <
ヽ::::::::::::::::::::: ̄^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄
 |::::::;ノ´ ̄\:::::::::::\_,. -‐ァ     __   _____   ______
 |::::ノ   ヽ、ヽr-r'"´  (.__    ,´ _,, '-´ ̄ ̄`-ゝ 、_ イ、
_,.!イ_  _,.ヘーァ'二ハ二ヽ、へ,_7   'r ´          ヽ、ン、
::::::rー''7コ-‐'"´    ;  ', `ヽ/`7 ,'==─-      -─==', i
r-'ァ'"´/  /! ハ  ハ  !  iヾ_ノ i イ iゝ、イ人レ/_ルヽイ i |
!イ´ ,' | /__,.!/ V 、!__ハ  ,' ,ゝ レリイi (ヒ_]     ヒ_ン ).| .|、i .||
`!  !/レi' (ヒ_]     ヒ_ン レ'i ノ   !Y!""  ,___,   "" 「 !ノ i |
,'  ノ   !'"    ,___,  "' i .レ'    L.',.   ヽ _ン    L」 ノ| .|
 (  ,ハ    ヽ _ン   人!      | ||ヽ、       ,イ| ||イ| /
,.ヘ,)、  )>,、 _____, ,.イ  ハ    レ ル` ー--─ ´ルレ レ´

Name: Anonymous 2011-12-13 9:56

>>22

I can type in this little window here faster than I can compile stuff, and I had similar errors in the past and was quite convinced it was the same cause. I think in the past I had two definitions for a function with the same name. It is good to know about the implied extern, although I would never rely on it.

Name: Anonymous 2011-12-13 10:39

>>23
Get you're fucking yukkari out of here you fucking faggot it's not funny that they're bad and that's iorinic or whatever they're fucking retarded like all touhou shit because you're nothing but a stupid faggot pedophile who masturbates to little girls then says he isn't a pedophile I won't take shit easy if I don't have to how the fuck do you think discussions work you dense cunt? I'd like to see you walk into a war zone and be all "HURR DURR TAKE IT EASY YUKKURI SHITE ^___^" then see what happens you touhoufags really need to get the fuck out of your shitty game has fuck all to do with programming anyway do you see /v/ coming in here and blogging about mega man or whatever? No because even they aren't as fucking retarded and narcissistic as you pedo cunts.

Don't change these.
Name: Email:
Entire Thread Thread List