Home > Backend Development > C++ > body text

Redeclaration of global variables in C program

WBOY
Release: 2023-09-20 22:29:05
forward
1301 people have browsed it

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;
}
Copy after login

Output

Var = 0
Copy after login

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;
}
Copy after login

Output

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

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;
}
Copy after login

Output

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

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;
}
Copy after login

Output

error: redeclaration of &lsquo;int var&rsquo;
Copy after login

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;
}
Copy after login

Output

Var = 10
Copy after login

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;
}
Copy after login

Output

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

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;
}
Copy after login

Output

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

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;
}
Copy after login

Output

error: redeclaration of &lsquo;int var
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template