Home  >  Article  >  Backend Development  >  Redeclaration of global variables in C program

Redeclaration of global variables in C program

WBOY
WBOYforward
2023-09-20 22:29:051295browse

Redeclaration of global variables in C program

We will see how C and C behave when redeclaring a global variable without initialization, redeclaring a global variable with initialization, redeclaring a global variable and initializing it twice different. Additionally, we will repeat the above combination using local variables.

1. A) C program: Re-declare global variables without initialization

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

Output

Var = 0

B) C program: Re-declare global variables without initialization

#include <iostream>
using namespace std;
int var;
int var;
int main(){
   cout<<"Var = "<<var;
   return 0;
}

Output

Compilation Error: int var;
main.cpp:3:5: note: &lsquo;int var&rsquo; previously declared here

Result: - C allows global variables to be redeclared without initialization. The value is still 0. C gives a compilation error indicating that the variable was redeclared.

2. A) C program: Re-declare local variables without initialization

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

Output

error: redeclaration of &lsquo;var&rsquo; with no linkage

B) C program: Re-declare local variables without initialization

#include <iostream>
using namespace std;
int main(){
   int var;
   int var;
   cout<<"Var = "<<var;
   return 0;
}

Output

error: redeclaration of &lsquo;int var&rsquo;

Result: - Neither C nor C++ allow redeclaration of local variables without completing initialization. Both programs fail to compile.

3. A) C program: Re-declare global variables and initialize them

#include <stdio.h>
int main(){
   int var;
   int var=10;
   printf("Var = %d",var);
   return 0;
}

Output

Var = 10

B) C program: Re-declare global variables and initialize them

#include <iostream>
using namespace std;
int var;
int var=10;
int main(){
   cout<<"Var = "<<var;
   return 0;
}

Output

main.cpp:7:9: error: redeclaration of &lsquo;int var&rsquo;
int var;

Result: -C allows redeclaration of uninitialized global variables. C program compilation failed.

4. A) C program: Re-declare global variables and initialize them

#include <stdio.h>
int var;
int var=10;
int main(){
   printf("Var = %d",var);
   return 0;
}

Output

error: redeclaration of &lsquo;var&rsquo; with no linkage

B) C program: Re-declare local variables through initialization

#include <iostream>
using namespace std;
int main(){
   int var;
   int var=10;
   cout<<"Var = "<<var;
   return 0;
}

Output

error: redeclaration of &lsquo;int var

Result: - Neither C nor C allow redeclaration of a local variable, even if it is not initialized. Both programs failed to compile

The above is the detailed content of Redeclaration of global variables in C program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete