C语言第二节数据类型(-)

这个首发自我的微信公众号(Mark学编程),不知为什么不能复制里面的图片。如果看图片请到公众号。SORRY.

Firstly, data can be classified into two categories: namely constants and variables. C语言数据分为两类,常量和变量。当然两者都可以继续分类。

Constant: As the name suggests, a constant does not change its value in a program. for example, we use #define PI 3.1415 to define PI represent the constant value 3.1416. other kind of contant skipped in order not to confuse with the below variables. 比如我们用#define PI 3.1416来定义常量PI. 以便在程序中使用。其他的略去,以免与下面要讲的混淆。

Variable:A variable value can be changed in a program. That is why called variable. A variable is defined by giving it a data type and a name. At this point, we should know that there are three basic variable type: i.e: Int; Float; Char. Other types of variable types will be covered later. See below example:

The outcome show as follow:

Notice that before the variable name va, vb, vc, We put the type int, float,char. That is a must.without a type a variable cannot be defined. And also please notice the comma and others like “” , () etc. please copy the example. and if you dont know that it is. Just copy.

下面我们用中文讨论一下变量的究竟是什么?

一般的书中,都会将变量比喻成盒子或什么的,我的体会是这个比喻容易误导初学者。因为盒子容易让人联想起立体的东西,况且将变量名贴到上面去,就更成问题了。网上有大量的资料讨论变量的问题,不妨去看看,当然,含金量的确不一样,有的甚至是错误的。我在这里试图澄清一下,看看能不能达到澄清的目的。

还是从计算机原理说起,简单说,计算机就是计算和储存,也就是能计算,能储存。所以对应的两个重要计算机部件就是CPU和内存。我们撰写的C语言程序,经过编译后最后执行阶段应该是到了内存,以二进制的数字存储在内存中。这是简单化的理解。程序中的变量根据其属性,后面会涉及到。被分配到内存的某个区块中,用区块较好。也比较符合计算机实际。这个区块是保存变量值的,注意是值,不是变量名,至于变量类型,应该是这个区域本身所规定的,后面我们会用代码说明。所以,要把变量名和变量值分开,概念上分开,因为代码中难以分开,因为我们用变量名,其实就是用里面的值,在代码里。那么变量名在哪里存着呢,具体的,还真没学到这一步,应该是某种机制来操纵。知道这些对于C语言学习指针,首地址什么的就容易理解了。当然,学习其他语言也就容易理解里面的变量了,特别的引用变量这个在PHP,Java中的初学难点也容易解决。

这样的话,再谈谈变量的类型在内存中的大小。让我们回到英语世界中。

We already know that there are three basic types of char, int and float. Sure we have more types. You can check the book to find out.We just list some of them as below:

Short integer;

Long integer;

unsigned interger;

double-floating. etc.

我看到的基本书上分类有些不同,这个没有什么问题,应该只是角度问题。关于类型,会在代码中慢慢体会。

We will use the sizeof() operator to display the number of bytes of memory required by some of the common data types in C. What is byte? Goodness, you need to check it with the textbook to learn more of the basics of computers. We might touch the basic computer science later on if we have nothing to do at that time.

The following is a complete code file. Please read carefully and copy it to run on your computer. We will explain after the picture.

中文解释:

/* /是注释符号,中间的N行都是解释说明,整个名称叫注释。不能里面再一个/ */.

// 是单行注释。

其他的都在代码里面注释了,学学英语吧。

运行结代码下:

就到这里。

文章已于2018-12-24修改

猜你喜欢

转载自blog.csdn.net/Mark21577/article/details/85235561
今日推荐