Table of Contents
1. A) C program: Re-declare global variables without initialization
Output
B) C program: Re-declare global variables without initialization
2. A) C program: Re-declare local variables without initialization
B) C program: Re-declare local variables without initialization
4. A) C program: Re-declare global variables and initialize them
B) C program: Re-declare global variables and initialize them
B) C program: Re-declare local variables through initialization
Home Backend Development C++ Redeclaration of global variables in C program

Redeclaration of global variables in C program

Sep 20, 2023 pm 10:29 PM
global variables restate

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

Does Go language have static global variables? Does Go language have static global variables? Jul 11, 2023 pm 03:37 PM

The go language does not have static global variables. It uses a more flexible way to handle the need for global variables. Global variables are usually declared at the package level, that is, variables declared outside the function. These global variables are throughout the package. are visible and can be used in any function in the package.

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

Implementing global variable safety in JavaScript Implementing global variable safety in JavaScript Jun 15, 2023 pm 10:33 PM

As JavaScript becomes more popular, more and more websites and applications rely on JavaScript. However, the use of global variables in JavaScript can have security issues. In this article, I will introduce how to implement global variable safety in JavaScript. The best way to avoid using global variables is to avoid using global variables. In JavaScript, all variables are global by default unless they are declared within a function. Therefore, local variables should be used whenever possible

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Redeclaration of global variables in C program Redeclaration of global variables in C program Sep 20, 2023 pm 10:29 PM

We will see how C and C++ behave differently when redeclaring a global variable without initialization, redeclaring a global variable with initialization, and redeclaring a global variable and initializing it twice. Additionally, we will repeat the above combination using local variables. 1.A) C program: Re-declare global variables without initialization #include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;} output Var=0B) C++ program:

Can golang functions directly access global variables in goroutine? Can golang functions directly access global variables in goroutine? May 01, 2024 pm 05:51 PM

Yes, Go functions in Goroutine have direct access to global variables by default. Reason: Goroutine inherits the memory space of the Goroutine that created it, including access to global variables.

What are the global variables in php What are the global variables in php Aug 01, 2023 pm 01:21 PM

PHP global variables include: 1. $_SERVER, the super global variable of the server and execution environment information on which the current script is running; 2. $_GET, an associative array of variables passed to the current script through the GET method; 3. $_POST, through the POST method An associative array of variables passed to the current script; 4. $_SESSION, which stores user-related information in the current session; 5. $_COOKIE, an associative array of variables passed to the current script through HTTP Cookies; 6. $_FILES, etc.

See all articles