niko
niko

Reputation: 9393

Why its returning an error?

#include <stdio.h>
int call()
{
    extern int b;
    b=10;
    printf("%d",b);
}

int main ()
{
    int b=8;
    call();
    return 0;
}

Why is throwing an error like these do I get the following linker error:

 /tmp/ccAsFhWX.o:meka.c:(.text+0x7): undefined reference to `_b' collect2: ld returned 1 exit status

I wanted to change the b value in the other function using extern keyword but it gives me an error.am i right in doing so ?

Upvotes: 0

Views: 88

Answers (3)

sehe
sehe

Reputation: 393694

Declaring the extern int b declares it as.... extern. It must be defined elsewhere. If it isn't, drop the extern keyword?

I think you wanted a global variable:

#include <stdio.h>

static int b;

int call()
{
    b=10;
    printf("%d",b);
}

int main ()
{
    b=8;
    call();
    return 0;
}

If you declare the global b as extern you gain the possibility (and the duty) to define it elsewhere (perhaps in another translation unit (helpers.c) or a library (helpers.a) etc.)

Upvotes: 7

user405725
user405725

Reputation:

In the C programming language, an external variable is a variable defined outside any function block. Please read about extern variables (here, for example).

Also, variables have scopes. For example, it can be a local variable, a global variable etc. You can read more about that here.

So what you have done here is declared a function scope variable in function call () without defining it using the power of extern keyword. In other words, simply tells the compiler that variable already exists somewhere. On top of that, you declared and defined another function scope variable in function main (), which has the same name. It is important to understand that those variables are totally different. So at the end of day, when you link your program, the definition of the variable b for function call () is not found. You declared it but never defined, remember?

Here are possible solutions. Do not declare multiple b variables as that was clearly not your intent. Stick with a single declaration and definition:

#include <stdio.h>    

extern int b;

void call()
{
    b = 10;
    printf("%d\n",b);
}

int b = 8;

int main () {  
    call();
    return 0;     
} 

But global variables are usually very bad - global scope makes them extremely pipeline unfriendly, introduce threading issues etc. So you must look into something like this:

#include <stdio.h>    

void call (int *b)
{
    printf ("%d\n", *b = 10);
}

int main () {  
    int b = 8;
    call (&b);
    return 0;     
} 

I'd also recommend you read the following question and answers here. It explains a lot about extern variables in C.

And by the way, you function call () is declared with int return type but returns nothing.

Hope it helps!

Upvotes: 1

ott--
ott--

Reputation: 5722

To change the "b" in main(), you must pass a pointer to "call" like call (&b) and then do

void call (int *b) {
    *b = 10;
    printf("%d",*b);
}

Upvotes: 0

Related Questions