top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Locally extern variable is resulting in error?

+4 votes
391 views

case:1

int main(void)
{
  extern int var = 0;
  var = 10;
  return 0;
}

case:2

#include<stdio.h>
extern int a;
int main()
{
 extern int a;
 int a=34;
 printf("%d",a);
 return 0;
}

In both cases it's error why so? Curious to know exactly how declaration of variable with extern differ locally and globally ?

More informative answer is most welcome.

posted Jan 24, 2014 by Atiqur Rahman

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
When you declare a symbol extern, you are telling the compiler to link all occurrences of this value into the same symbol.

Any occurrences of extern int a; in your program would link to the externally defined a.

Similar Questions
+4 votes
#include<stdio.h>
int var;
int var;
int var;

int main(void) { 
  printf("%d \n",var); 
  return 0;
}
+2 votes
typedef union
{
    int integer;
    char* string;
} Value;

main()
{
  Value v = { 1 };
  v = { 2 }; // getting the error on this 
}
+2 votes

I assume that constant must be initialized to a constant value at compile time, but following code the return value of ge_const() is assigned to x which will be collected at run time. So this must cause an error but I am getting the output as 50. Can someone clarify the detail?

main()
{
    const int x = get_const();
    printf("%d", x);
}
int get_const()
{
    return 50;
}
+1 vote

In large product based companies LLD is written by senior team members and developers write the code.
Does the senior team member decide what data type they should use in advance ? If yes then what things derives type of data for example. How they decide a variable should be declared as volatile ? and what would be advantage of having a variable volatile and what could be the problem if it is taken without the volatile ?

...