


What are the naming rules for local variables in C language?
Principles of local variable naming in C language: see the name and know the meaning, and clearly express the use and meaning of variables. Use meaningful English words or abbreviations to avoid blur and confusion. Follow the camel or underline nomenclature to keep the style consistent. Avoid single letter variable names unless they are loop counters or temporary variables. Keep naming consistent and do not change it at will. Never use system keywords or reserved words as variable names.
To put it bluntly, the naming of local variables in C language is how to name the little guys inside your function. This seems simple, but it has hidden mystery, which is related to the readability, maintainability, and even performance of the code (although it doesn't have much impact, we are pursuing the ultimate).
Many people think that you can just name it, but the compiler can recognize it anyway. This idea is too naive! Imagine that you are facing a function with thousands of lines of code, with variable names all a
, b
, c
, or var1
, var2
, var3
, and it feels like reading a book of heaven. Debugging is even more of a nightmare.
Therefore, good local variable naming is a compulsory course for programmers. It should clearly express the purpose and meaning of the variable.
Core principle: See the name and know the meaning
This is not just empty talk. A good variable name should let you understand what it is and what it is for at a glance. For example, in the function of calculating the area of a circle, the radius can be used as radius
, and the area can be used as area
, instead of r
and a
. Even if you think r
and a
were very concise at that time, you might forget what they mean by looking at it in a few days.
Some suggestions are not dead rules, but flexible use is the best way:
- Use meaningful English words or abbreviations:
userName
is better thanun
,itemCount
is better thanic
. Make sure that the abbreviation is easy to understand in the context of your code, and don't abbreviate it for the sake of abbreviation and make things worse. - Follow the camel nomenclature or underscore nomenclature: camelCase is like
userName
, and underscore nomenclature (snake_case) is likeuser_name
. Choose a style, and stick to it, and don't mix it in a project. I personally prefer hump, which is pleasing to the eye. - Avoid using single-letter variable names unless they are loop counters or temporary variables:
i
,j
,k
are common in loops, and everyone can understand it. But try to avoid other places. - Keep naming consistency: If you use
userName
, don't useuser_name
for a while,username
for a while. Keep consistency and make the code look tidy. - Do not use system keywords or reserved words as variable names: this can cause compilation errors, which is common sense.
Code example:
A function that calculates the average value, compares good naming and bad naming:
<code class="c">// Bad naming float avg(float a, float b, float c) { float sum = abc; float av = sum / 3; return av; } // Good naming float calculateAverage(float num1, float num2, float num3) { float sumOfNumbers = num1 num2 num3; float average = sumOfNumbers / 3.0f; // 注意这里加了.0f 保证精度return average; }</code>
Have you seen the difference? In the second version, the readability of the code is significantly improved.
Experience in trapping:
Once in a project, the variable naming was not standardized, making it very difficult to maintain later. I spent a lot of time clarifying the meaning of variables and modifying bugs. The lesson is profound! Therefore, it is definitely worth the investment to develop good naming habits from the beginning.
Summarize:
The naming of local variables in C language seems like a small matter, but it actually has a big relationship. Follow the above suggestions and develop good naming habits. Your code will be clearer and easier to maintain, and you will also avoid many detours. Remember, the code is written for people to see, and the second is executed for machines.
The above is the detailed content of What are the naming rules for local variables in C language?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

Discussion on problems when using RxJS to operate on elements in streams in learning and using RxJS...

不同数据库系统添加列的语法为:MySQL:ALTER TABLE table_name ADD column_name data_type;PostgreSQL:ALTER TABLE table_name ADD COLUMN column_name data_type;Oracle:ALTER TABLE table_name ADD (column_name data_type);SQL Server:ALTER TABLE table_name ADD column_name data_
