c variable naming rules

What are the rules for variable names in the c language

Recommended: “C Tutorial”

When defining a variable, the name of the variable can be a combination of letters, numbers and underscores. But it’s not just any combination, so pay attention to the following naming rules:

1. Variable names begin with a letter of the alphabet;

Variable names can’t begin with a number, they can begin with a letter or an underscore. But, in fact, the most commonly used in programming is to start with a letter, and the variable name starting with an underscore is system-specific.

Open any header file and you’ll see that all the variable names, macro names, and function names in it start with an underscore.

So to avoid conflicts with system-defined names, never start a variable name with an underscore unless it is required.

Correct variable name:

intIn_1=2;

intabc=5; Wrong variable name:

int1In_1=2;

int2abc=5; 2. It must not contain spaces, punctuation marks, or type specifiers (%, &,! , #, @, $);

// Wrong variable name: cannot contain spaces, punctuation and type specifiers (%, &,! , #, @, $),can only be composed of letters, numbers, underscores.

int%age=13;

inta%ge=13;

intnameage=12;3. Letters are case-sensitive;

// Variable n and variable N are two variables.

intn=11;

intN=18;4, the effective length is 255 characters;

5, it can not be a keyword;

// Wrong Variable Name: case is a keyword in C, it can not be used as a variable name

intcase=12;The keywords defined in C are:< /p>

There are 32 C keywords defined by the ANSI standard: auto, double, int, struct, break, else, long, switch, case, enum, register, typedef, char, extern, return, union, const

These 32 keywords are those that are already used by the C language itself and cannot be used for any other purpose. They cannot be used for any other purpose, such as being defined as variable names or function names.

The above is the whole content of this article.

What to note about naming variables in c language

Naming variables in c language:

Numbers, letters, underscores, can all be named for variables.

The beginning of a variable name cannot be a number, such as 3a_ is wrong, while a_3 or _a3 is correct

Variable name cannot be a keyword, such as if’else’int and so on.

c language variable naming rules

Variable names are composed of a-z,A-Z,0-9,_ (upper and lower case letters, numbers, underscores) and cannot begin with 0-9 (numbers)

The following are popular in terms of variable naming:

I.Hungarian Nomenclature

This nomenclature starts from combining the variable names in the order of: attribute+type + object description of the order of the combination, in order to make the programmer for the variable when the variable type and other attributes of the variable have an intuitive understanding, the following is the HN variable naming specification.

Attribute part:

g_ Global variable

c_ Constant

m_ c++ class member variable

s_ Static variable

Type part:

Array a

Pointer p

Function fn

Null v

Handle h<

Long integer l

Boolean b

Floating-point (sometimes referred to as file) f

Double word dw

String sz

Short integer n

Double-precision floating-point d

Count c (usually with cnt)

Character ch (usually with c)

Integer i (usually with n)

Byte by

Word w

Real r

Unsigned u

Description section:

Max Max

Min Min

Initialization Init

Temporary variable T (or Temp)

Source object Src

Destination object Dest

Examples:

hwnd: h is a type description that represents a handle, and wnd is a variable object description that represents a window, so hwnd represents a window handle;

pfnEatApple: pfn is a type description that represents a pointer to a function, and EatApple is a variable object description, so it represents a function pointer variable to the EatApple function.

g_cch: g_ is an attribute description that denotes a global variable, c and ch are count type and character type respectively, together they denote the type of the variable, the object description is ignored here, so it denotes a global variable that counts characters.

The above is the general rule of HN nomenclature.

Second, Hump Nomenclature

The center point of Hump Nomenclature is the capitalization of the beginning of each word, and Hump Nomenclature can be divided into Big Hump and Little Hump, Big Hump means that all the words are capitalized at the beginning, and Little Hump means that the first word starts in lowercase, and the following words are capitalized at the beginning.

Big Hump: EatSimpleApple

small hump: eatSimpleApple

Generally, big hump is used for function naming, small hump is used for variable naming

When there is an abbreviation (e.g., IP), if the abbreviation is at the beginning, then if it is a big hump, then it is all capitalized, and if it is a small hump, then it is all lowercase, and if it is not at the beginning, then it is all capitalized

Large hump: ipAddIP

Large hump: IpAddIp

Small hump: ipAddIp

Small hump: ipAddIp

Three, Pascal nomenclature

Pascal nomenclature means that each word is separated by an underscore, and each word is written in lowercase (the same is true for abbreviations)

Pascal nomenclature refers to the fact that each word is separated by an underscore, and each word is written in lowercase (the same is true for abbreviations)

This is an example of the use of the abbreviations. /p>

Example: eat_simple_apple

Example: ip_add_ip