


What is the difference between declaration and definition of C user identifiers?
Apr 03, 2025 pm 01:09 PMIn C language, the declaration of an identifier declares its existence in the compiler, while the definition gives it a specific value based on the declaration. Declarations can be repeated, while definitions can only occur once and must be in appropriate .c files, usually used in header files, and definitions in .c files.
In C language, the declaration and definition of identifiers are to put it bluntly to tell the compiler the difference between "I know there is such a thing" and "what is this thing". These two sound like each other, but they are actually far apart. If you can't figure this out, no matter how fancy your code is, it is easy to cause trouble.
You think, you have to tell the compiler what tools you plan to use, right? That's what the statement means. It tells the compiler: "Hey, I plan to use a variable called myVariable
, it is an integer." But it doesn't say what the specific value of myVariable
is. It's like you booked a table but haven't sat down yet.
What is the definition? Only by defining things truly “create”. It not only tells the compiler that myVariable
is an integer, but also assigns a value to it, such as myVariable = 10;
. This is equivalent to not only booking a table, but also sitting down, and this table is yours now.
Therefore, only the declaration is of type and the definition is of value. Declaration can be made multiple times, and definition can only be made once. You can declare the same variable in multiple places and tell the compiler "I want to use it here too", but you can only define it in one place and assign it the initial value.
Look at the code and you will understand:
<code class="c">// 聲明:告訴編譯器有個叫myVar的整型變量,但還沒賦值extern int myVar; // 定義:創(chuàng)建myVar這個變量,并賦值為5 int myVar = 5; void myFunction() { // 聲明:在這個函數(shù)里,我也要使用myVar extern int myVar; // 使用myVar myVar = 10; } int main() { // 使用myVar printf("myVar = %d\n", myVar); myFunction(); printf("myVar = %d\n", myVar); return 0; }</code>
In this code, extern int myVar;
is a declaration, which tells the compiler that the variable myVar
is defined elsewhere, and I want to use it here. And int myVar = 5;
is the definition, it creates myVar
and assigns values. You can declare the same global variable in multiple .c
files, but you can only define it in one .c
file. Otherwise, the compiler will go crazy because it doesn't know what myVar
is.
Let me remind you again: header files ( .h
files) are usually used to declare, not define. Definitions are usually placed in .c
files. If you define global variables in a header file and multiple files contain this header file, it will lead to repeated definition errors and the compiler will report errors without mercy. This is a common pitfall! Remember, declarations can be made multiple times, definitions can only be made once, and they should be placed in a suitable .c
file. This is the essence of C language and it is also a place where many beginners are prone to make mistakes. You must understand thoroughly in order to write robust and efficient C code.
The above is the detailed content of What is the difference between declaration and definition of C user identifiers?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Against the backdrop of violent fluctuations in the cryptocurrency market, investors' demand for asset preservation is becoming increasingly prominent. This article aims to answer how to effectively hedge risks in the turbulent currency circle. It will introduce in detail the concept of stablecoin, a core hedge tool, and provide a list of TOP3 stablecoins by analyzing the current highly recognized options in the market. The article will explain how to select and use these stablecoins according to their own needs, so as to better manage risks in an uncertain market environment.

As the market conditions pick up, more and more smart investors have begun to quietly increase their positions in the currency circle. Many people are wondering what makes them take decisively when most people wait and see? This article will analyze current trends through on-chain data to help readers understand the logic of smart funds, so as to better grasp the next round of potential wealth growth opportunities.

This article will introduce several mainstream stablecoins and explain in depth how to evaluate the security of a stablecoin from multiple dimensions such as transparency and compliance, so as to help you understand which stablecoins are generally considered relatively reliable choices in the market, and learn how to judge their "hazard-haven" attributes on your own.

Recently, Bitcoin hit a new high, Dogecoin ushered in a strong rebound and the market was hot. Next, we will analyze the market drivers and technical aspects to determine whether Ethereum still has opportunities to follow the rise.

The pattern in the public chain field shows a trend of "one super, many strong ones, and a hundred flowers blooming". Ethereum is still leading with its ecological moat, while Solana, Avalanche and others are challenging performance. Meanwhile, Polkadot, Cosmos, which focuses on interoperability, and Chainlink, which is a critical infrastructure, form a future picture of multiple chains coexisting. For users and developers, choosing which platform is no longer a single choice, but requires a trade-off between performance, cost, security and ecological maturity based on specific needs.

For newbies who are new to digital assets, they often get into choice difficulties when facing various mainstream currencies. This article will compare the three popular currencies: Bitcoin, Ethereum, and Dogecoin in detail from the aspects of technical characteristics, usage scenarios, market evaluation, etc. to help users understand the main differences between them and their respective advantages and disadvantages.

Stable coins maintain price stability by anchoring fiat currencies such as the US dollar, which are mainly divided into three categories: 1. Fiat currency collateralization types such as USDT and USDC; 2. Cryptocurrency collateralization types such as DAI; 3. Algorithm types have higher risks. Mainstream stablecoins include USDT with the highest market value and the best liquidity. USDC is known for its compliance and transparency. DAI relies on the decentralized mechanism. TUSD adopts on-chain real-time audit. BUSD is gradually withdrawing from the market due to supervision. USDP is known for its high compliance and security. Both are widely circulated on mainstream exchanges.

Stablecoins are cryptocurrencies that are pegged to assets such as the US dollar and aim to maintain stable value. They are mainly divided into three types: fiat currency collateral, cryptocurrency collateral and algorithms. 1. Fiat currency collateral types such as USDT and USCD are supported by US dollar reserves; 2. Cryptocurrency collateral types such as DAI need to over-collateralize other currencies; 3. Algorithm relies on smart contracts to adjust supply but have high risks. The reasons why it is hotly discussed on platforms such as Douyin include: as a hedging tool when the crypto market falls, a bridge for novices to enter the crypto world, a way to obtain high-yield financial management in DeFi, and the application of low-cost cross-border payments. To obtain stablecoins, you can trade through mainstream exchanges such as Binance, Ouyi, and Huobi.
