c语言变量和常量_C语言中的变量,常量和关键字

2021年9月13日 4点热度 0条评论 来源: culing2941

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)

As its name suggests constants are the values which will never change during the execution of program. Sounds confusing? Let’s try to make things more clear with a simple example. 顾名思义,常量是在程序执行期间永远不会改变的值。 听起来令人困惑? 让我们尝试通过一个简单的示例使事情更清楚。

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)

  1. Primary constants

    主要常数

  2. Secondary constants (We will learn them later)

    次要常量(我们将在以后学习)

At this stage we will only discuss primary constants. Primary constants are of three types.

在此阶段,我们将仅讨论主要常量。 初级常数有三种类型。

  1. Integer constants

    整数常数

  2. Real constants

    实常数

  3. Character constants

    字符常量

Let’s discuss them one by one. 让我们一一讨论。

整数常数 (Integer Constant)

Yes it will contain only integers. Remember an integer constant will never contain any decimal point. Eg: 1, 2, -43 etc. 是的,它将仅包含整数。 请记住,整数常量将永远不包含任何小数点。 例如:1、2 -43等。

字符常量 (Character Constant)

It is single (remember) alphabet, number or any special symbol which is enclosed in an inverted commas. Eg: ‘+’, ‘1’, ‘a’, etc. 它是单个(记住)字母,数字或任何以反逗号括起来的特殊符号。 例如:“ +”,“ 1”,“ a”等。

实常数或浮点常数 (Real Constant or Floating Point Constant)

A real constant may have any digit but it must contain one decimal point. Eg: 1.22, -54.5, 3432.13 实常数可以有任何数字,但必须包含一个小数点。 例如:1.22,-54.5、3433.23

变量类型 (Types of Variables)

As I said earlier variable are name of locations in memory. In that location we can store any constant like integer, character or Real. But there is some limit that an integer variable can only store integer constant, character variable can only store character constant and real variable can only store real constant. 如前所述,变量是内存中位置的名称。 在该位置,我们可以存储任何常量,例如整数,字符或实数。 但是有一个限制:整数变量只能存储整数常量,字符变量只能存储字符常量,实数只能存储实数。

So it is quite obvious types of variables is similar types of constants. Eg: int x= 1;

因此很明显,变量的类型与常量的类型相似。 例如:int x = 1;

Here “int” is a keyword, “x” is an integer variable and it can only store integer constant, “1” is a integer constant. 这里的“ int”是一个关键字,“ x”是一个整数变量,它只能存储整数常量,“ 1”是一个整数常量。

编写变量名的规则 (Rules for writing variable names)

  1. A variable name may contain alphabets, numbers and underscores.

    变量名可以包含字母,数字和下划线。

  2. No other special character (other than underscore) can be used for writing variable name.

    不能使用其他特殊字符(下划线除外)来编写变量名。

  3. A variable name should start with either underscore or alphabets.

    变量名称应以下划线或字母开头。

  4. No space is allowed while writing variables.

    写入变量时不允许有空格。

C关键字 (Keywords in C)

Keywords are the words whose meaning is already explained to the compiler. They cannot be used as a variable name. 关键字是已经向编译器解释其含义的单词。 它们不能用作变量名。

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语言变量和常量

    原文作者:culing2941
    原文地址: https://blog.csdn.net/culing2941/article/details/108650386
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。