c语言变量和常量
In this tutorial you will learn about variables, constants and keywords in C.
在本教程中,您将学习C语言中的变量,常量和关键字。
C中的变量 (Variables in C)
In a typical C program we have to do a lot of computation. Of course there will be storing of some data in different locations of memory in computer. These memory locations are identified by their address like 56234. Suppose if programmer wants to access the particular locations like 10 times in a program to store another value at that location.
在典型的C程序中,我们必须进行大量计算。 当然,某些数据将存储在计算机内存的不同位置。 这些存储位置由它们的地址标识,例如56234。假定程序员是否要在程序中访问特定位置(例如10次)以在该位置存储另一个值。
So It will become a tedious job for a programmer if he have to memorise the numerical address name. So to make this job easy we use variables.
因此,如果程序员必须记住数字地址名称,这将成为繁琐的工作。 因此,为了简化这项工作,我们使用变量。
So variables are nothing but the name of the locations or addresses of memory which is given by programmer.
因此变量不过是程序员给定的存储器的位置或地址的名称。
C中的常数 (Constants in C)
In the above picture (1st) we have stored the constant value 3 at x location. The name of that location is x. It’s a variable. We can also store another value at x location.
在上面的图片(第1张)中,我们在x位置存储了常数3。 该位置的名称是x。 这是一个变量。 我们还可以在x位置存储另一个值。
Here X = variable (location or memory address name)
X =变量(位置或内存地址名称)
3 = constant
3 =恒定
Try to understand the second example yourself if you have any doubt, do comment below.
如果您有任何疑问,请尝试自己理解第二个示例,请在下面进行评论。
有两种类型的常量 (There are two type of Constants)
- Primary constants 主要常数
- Secondary constants (We will learn them later) 次要常量(我们将在以后学习)
At this stage we will only discuss primary constants. Primary constants are of three types.
在此阶段,我们将仅讨论主要常量。 初级常数有三种类型。
- Integer constants 整数常数
- Real constants 实常数
- Character constants 字符常量
整数常数 (Integer Constant)
字符常量 (Character Constant)
实常数或浮点常数 (Real Constant or Floating Point Constant)
变量类型 (Types of Variables)
So it is quite obvious types of variables is similar types of constants. Eg: int x= 1;
因此很明显,变量的类型与常量的类型相似。 例如:int x = 1;
编写变量名的规则 (Rules for writing variable names)
- A variable name may contain alphabets, numbers and underscores. 变量名可以包含字母,数字和下划线。
- No other special character (other than underscore) can be used for writing variable name. 不能使用其他特殊字符(下划线除外)来编写变量名。
- A variable name should start with either underscore or alphabets. 变量名称应以下划线或字母开头。
- No space is allowed while writing variables. 写入变量时不允许有空格。
C关键字 (Keywords in C)
A question which may arise in your mind that, how computer will know that its integer variable or character variable or anything else?
您可能会想到一个问题,计算机将如何知道其整数变量或字符变量或其他内容?
The simple answer is with the help of keywords. In one of the above example I have used “int” keyword. Eg: int x=1
简单的答案是借助关键字的帮助。 在以上示例之一中,我使用了“ int”关键字。 例如:int x = 1
In this example “int” is a keyword and it will tell the computer that “x” will be an integer variable and it will only store integer constant.
在此示例中,“ int”是关键字,它将告诉计算机“ x”是整数变量,并且仅存储整数常量。
There are 32 keywords used in C language which are given below. We will discuss them in later tutorials.
下面列出了用C语言使用的32个关键字。 我们将在以后的教程中讨论它们。
|
Keywords in C |
C关键字 |
影片教学 (Video Tutorial)
Watch below video tutorial to learn more about variables and keywords in C.
观看下面的视频教程,以了解有关C语言中的变量和关键字的更多信息。
翻译自: https://www.thecrazyprogrammer.com/2014/12/variables-constants-and-keywords-in-c.html
c语言变量和常量