Home >Common Problem >What are the rules for variable names in C language?

What are the rules for variable names in C language?

青灯夜游
青灯夜游Original
2019-02-25 15:25:48139104browse

C language variable name rules are: 1. The variable name starts with an English letter; 2. The letters in the variable name are case-sensitive; 3. The variable name cannot be a keyword; 4. The variable name cannot Contains spaces, punctuation, and type specifiers.

What are the rules for variable names in C language?

Recommended: "c Tutorial"

When defining a variable, the variable name can be letters, A combination of numbers and underscores. But it is not a random combination. Please pay attention to the following naming rules:

1. The variable name starts with an English letter;

The variable name cannot start with a number. Can start with a letter or an underscore. However, in fact, the most commonly used names in programming begin with a letter, and variable names beginning with an underscore are system-specific.

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

So in order to avoid conflicts with system-defined names, when programming, never use an underscore as the beginning of a variable name unless it is required to be defined in this way.

Correct variable name:

int In_1=2;
int abc=5;

Incorrect variable name:

int 1In_1=2;
int 2abc=5;

2. It cannot contain spaces, punctuation marks and type specifiers (%, & ,!, #, @, $);

//错误的变量名:不能包含空格、标点符号和类型说明符(%、&、!、#、@、$),只能是字母、数字、下划线组成。
int %age = 13; 
int a%ge = 13; 
int name age = 12;

3. Letters are case-sensitive;

//变量n和变量N是两个变量。
int n = 11;
int N = 18;

4. The effective length is 255 characters;

5. It cannot be a keyword;

//错误的变量名:case是C语言的一个关键字,不能作为变量名
int case = 12;

The keywords defined in C language are:

There are 32 C language keywords defined by the ANSI standard: auto, double, int, struct, break, else, long, switch, case, enum, register, typedef, char, extern, return, union, const , float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile, do, if, while, static.

These 32 keywords have been used by the C language itself and cannot be used for other purposes. For example, they cannot be defined as variable names or function names.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What are the rules for variable names in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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