"0 Basic Learning iOS development" "02-c language" 04-constants, variables

Source: Internet
Author: User

directory of this document
    • First, the storage of data
    • Ii. Types of data
    • Three, constant
    • Third, variable

In our use of computer process, will be exposed to a variety of data, there are document data, image data, video data, as well as chat QQ generated text data, with thunder download file data. Let's talk about the processing of the data in C language.

Back to top one, storage of data 1. Data type

Let's start by looking at how the computer stores the data. In general, the data stored in the computer can be divided into two types: static data and Dynamic Data.

1> static data

Concept: Static data refers to some persistent data that is typically stored on a hard disk. Hard disk storage space is generally relatively large, now ordinary computer hard disk has about 500G, so the hard disk can be stored in some relatively large files.

Storage Duration: After the computer shuts down, the data is still in, as long as you do not actively delete or the hard drive is not bad, the data will always be.

What is static data: Static data is usually stored on the hard disk in the form of files, such as documents, photos, videos, etc.

2> Dynamic Data

Concept: Dynamic Data refers to temporary data that is generated dynamically during the course of a program's operation, typically stored in memory. Memory storage space is generally relatively small, now the average computer memory only about 4 G, so be careful to use memory, do not occupy too much memory space.

Storage Duration: After the computer shuts down, the temporary data is erased.

What is Dynamic Data: When you run a program (software), the entire program is loaded into memory, and as the program runs, it produces a variety of temporary data that is stored in memory. When the program stops running or the computer is forced to shut down, all temporary data generated by the program will be erased.

You might ask, why not load all the applications into the hard drive, since the storage space is so large? One of the main reasons is that the memory accesses faster than the hard disk n times.

3> Dynamic Data and static data conversion

Hard disks and memory are the two hardware most frequently used by computers, and data between them is often converted.

For example, there is a video file called "C language. mp4" On the hard disk, now you want to use storm audio to play

First turn on the Storm AV software, the computer will load the storm video into memory, and then the computer will read the contents of the hard disk in memory files. The storm audio will parse the contents of the read file and present it to the user in the form of a video. This completes a transition from static data to Dynamic Data.

For example, you use Thunder to download a video file called "C language. mp4" from the Internet.

First open the Thunderbolt software, the computer will be the Thunder loaded into memory, followed by Thunder will be downloaded from the Internet video files. As we all know, this download process must be time-consuming, mainly affected by file size and download speed. The data that is downloaded in each time period is first put into memory and then written to the hard disk. Once all the data has been downloaded, a full video file will be available on the hard drive. This completes the conversion of dynamic data to static data.

2. Storage form 1> binary storage

As stated in the previous article, computers can only recognize 0 and 1. As a result, the static and dynamic data mentioned above are stored in the form of 0 and 1, which is called "Binary storage". Some people may find it very surprising that only 0 and 1 can represent this much data? Yes, if it's just a single number, it can only represent 2 kinds of data: either 0 or 1. But if there are multiple digits, the situation will be different. If there are 2 digits, then can represent 4 kinds of data: 00, 01, 10, 11, if there are 3 digits, it can represent 8 kinds of data, and so on, if there are n digits, it can represent 2 of the n-th square data. It can be found that as long as the number of bits is sufficient, 0 and 1 can represent a very large amount of data.

2> bits and bytes

* Usually we see on the computer MP4, MP3, photos and other documents, are composed of 0 and 1 groups, but the computer analysis of these 0 and 1, in the form of graphical interface present before us. The larger the file, the more 0 and 1 included, in order to facilitate the calculation of file size, the measurement unit made a provision: a bits of 1bit, that is, 1 0 or 1 for the 1bit,bit of the Chinese translation is "bit"; 8 bits is 1byte, 8 0 or 1 is 1byte, The Chinese translation of 1byte=8bit,byte is "byte". Usually we say that a file size of 64B, is 64 bytes meaning, the interior contains 64x8 0 and 1.

* There are also some units of measurement that need to be understood:

1 KB = b,1 MB = 1024x768 kb,1 GB = 1024x768 mb,1 TB = 1024x768 GB

* View the size of a MP4 file on your Mac:

26.2 MB = 26.2 x 1024x768 = 26.2 x 1024x768 x 1024x768 B (bytes)

Back to top second, data type

As programmers, the most concern is definitely in-memory dynamic data, because we write the program is running in memory. In the process of running, the program will produce a variety of dynamic temporary data, in order to facilitate the calculation and operation of data, C language to classify these data, provide a rich data type. Roughly as shown:

* Among the many data types in the graph, the most common is the 4 basic data types: char, int, float, double, and most importantly the pointer type, the pointer is used properly, not only can save the code amount, also can optimize memory management, improve performance. Therefore, pointers are a very important concept that must be valued. If you say that the C language except the pointer, others have learned very well, then you simply say you have not learned C language.

* These rich data can be represented by constants or variables in the C language. The next step is to explain the use of constants and variables.

Back to top three, constant 1. What is a constant

"Amount" represents data. Constant, it means some fixed data, that is, data that cannot be changed.

2. Types of constants

In the C language, constants can be broadly divided into the following types:

1> integral type constant (int)

is actually the int type of data, including all the integers, such as 6, 27, 109, 256, 10, 0, 289, etc.

2> floating-point constant (float\double)

Floating-point constants are divided into double and float two data types

    • Double: dual-precision floating point, in fact, is a decimal. For example, 5.43, 2.3, 0.0, and so on, note that 0.0 is also considered a decimal.
    • FLOAT: single-precision floating-point type, also a decimal, is less accurate than a double, meaning that it can represent a smaller number of decimal places. To differentiate from double, the float type data is terminated with F, such as 5.43f, -2.3f, 0.0f. It should be noted that there is absolutely no 10f in this format, the compiler will directly error, only decimals allowed to add F.

3> character constant (char)
    • Enclose a number (0~9), English letter (A~z, a~z), or other symbol (+ 、-、!、?, etc.) in single quotation marks, which is a character constant. such as ' 6 ', ' A ', ' F ', ' + ', ' $ ' and so on.
    • Note: The single quotation mark can only enclose 1 characters, and cannot be Chinese characters, the following wording is wrong: ' abc ', ' 123456 ', ' Male '

4> string Constants
    • Enclose one or more characters in double quotation marks (""), which makes up a string constant. such as "6", "Male", "wow haha", "ABCD", "MY_CAR4", in fact printf ("Hello World"); the statement "Hello World" is a string constant.
    • What is the difference between 6, ' 6 ' and ' 6 ' in usage? This is not to be discussed and will be introduced later.

Back to top three, variable 1. What is a variable

Constants represent data that cannot be changed, and data represented by variables can be modified frequently. For example, the protagonist's health in the game can be represented by a variable, the protagonist is hurt, the health will be reduced, the protagonist after treatment, the health value will increase, in the game process, the protagonist's health has been changing, so the protagonist's health should be represented by a variable. The bottom line: When the value of a data needs to change frequently or is uncertain, it should be represented by a variable.

2. Definition of variables

Before any variables can be used, they must be defined first. The purpose of defining variables is to allocate a piece of storage space in memory to variables, which makes it easier to store data later. If you define more than one variable, you allocate different storage space for each variable.

1> Variable Type

* Computer memory is limited, now normal computer memory is 4G, then define a variable when allocating how much storage space to this variable? is 4G all to it? This is obviously impossible, if the 4G storage space is given to this variable, it means that the space can not be allocated to other variables, and the system will be paralyzed, because the memory is not enough to run other programs. Therefore, when we define a variable, we need to indicate the variable type, and the system allocates the corresponding storage space according to the variable type. The storage space used by different data types is not the same, if it is a character (char) variable, allocate 1 bytes of storage space, and if it is an integer (int) variable, allocate 4 bytes of storage space.

* Another function of variable types is to constrain the type of data that a variable holds. Once the variable is given a type, the variable can only store this type of data, such as Integer (int) variables can only store integer data, cannot store floating-point data.

2> variable Name

In the process of running a program, you will definitely define a large number of variables, each of which has its own storage space. So how do you differentiate between these variables? How do I find the storage space for a variable? To differentiate these variables, you should specify a variable name for each variable, and the variable name is one of the identifiers. When we want to modify the data of the variable, the system will find the corresponding storage space according to the variable name and change the data inside the storage space.

3> definition

As a summary, you need 2 conditions to define a variable: variable type, variable name. The format of the definition variable is: variable type variable name;

1 int Main () 2 {3     int i;4     5     char c;6     7     return 0;    8}

Since the C program's entry is the main function, the code that defines the variable is now written in the main function. In line 3rd, an integer variable named i is defined, stating that I can only store integer data, and in line 5th, a character variable named C is defined, stating that C can only store character data. Lines 3rd, 5, 7 are called "statements," and each statement is followed by a semicolon.

Therefore, the system will be in memory for the variable I, C allocated a certain amount of storage space, as shown, I and C each occupy a piece of storage space. As for how much storage space to occupy, temporarily do not have to study, will be introduced later.

If it is a variable of the same type, it can be defined consecutively, and the variable names are separated by commas. The format is: variable type variable name 1, variable name 2, variable name 3, ...;

1 int Main () 2 {3     int A, c;4     5     return 0;6}

The 3rd line of code means that 2 variables of type int are defined, and the variable names are a, C, respectively.

3. Use of variables 1> first defined, then initialized

There are two variables defined earlier, but these two variables do not store any value, we need to assign the variable first, also called "Initialize".

The format of variable assignment is: variable name = value;

This equals "=" is an assignment operator that assigns the value on the right to the left variable, that is, the value on the right is stored in the storage space of the left variable.

1 int Main () 2 {3     int i; 4     i = ten; 5      6     char c; 7     c = ' A '; 8      9     return 0;10}

In line 4th, the variable i is assigned an integer constant of 10, and the variable C is assigned a character constant ' a ' on line 7th. Assignment operations, such as Lines 4 and 7, are called "assignment operations."

The memory is roughly as shown, the integer 10 is stored in the storage space of I, and the letter A is stored in the storage space of C.

(In fact, my picture is not very accurate, because all the data in memory is stored in the form of 0 and 1, such as 10, it will be stored as 1010; letter A, it will be stored as 1000001.) In order to achieve the intuitive effect, it is not written in binary form)

Simultaneous initialization of 2> definitions

The above code can also be written as follows, which is initialized at the same time as the variable is defined: Variable type variable name = value;

1 int Main () 2 {3     int i = 10;4     5     float f = 10.9f;6     7     Double d = 9.8;8     return 0;9}

3> can be constantly modified

Since I is a variable, it shows that its value can be changed constantly, see the following code

1 int Main () 2 {3     int i = ten; 4      5     i =; 6      7     char c = ' A '; 8      9     return 0;10}

The variable i is defined in line 3rd and has an initial value of 10. The value of I is then changed to 89 in line 5th, and this 89 overwrites the previously stored 10, which is roughly as shown in memory

4> const keyword

As mentioned earlier, by default, the value of a variable can be changed continuously. However, there are times when we want the value of a variable to be initialized only once at the time of definition and cannot be changed later, so we can use the Const keyword to modify the variable.

1 int Main () 2 {3     const int i = 10;4     5     i = 11;6     7     return 0;8}

Note Line 3rd, which adds a const keyword to the front of the Int. The value representing the variable i is initialized only once, that is, the value of I is always 10 of the beginning, and can no longer be changed. Therefore, the compiler will report the 5th line of code error, not allowed to modify the value of I again.

4. Use of variables note

1> cannot define the same variable repeatedly

The following code is incorrect

1 int Main () 2 {3     int i = 10;4     5     int i = 89;6     return 0;7}

The compiler will report the 5th line of error, the reason is very simple, the 3rd and 5th lines are defined as the variable I, so in memory will be like this

There will be two storage space in memory, and the name is called I, then if I want to take out the value of the variable i, then you say the computer 10 good or take 89 good? Therefore, such an approach is certainly not possible.

2> can assign the value of a variable to another variable

1 int Main () 2 {3     int i = 10;4     5     int a = i;6     7     return 0;8}

In line 3rd, the variable i is defined and the initial value is 10, then the variable A is defined on line 5th, and the value of variable i is assigned to a. In memory roughly as shown:

Variables I and variable a store values of 10

The scope of the 3> variable (scope) starts with the line of code that defines the variable.

The following code is incorrect

1 int Main () 2 {3     int a = i; 4      5     int i = ten; 6      7     int b = i; 8      9     return 0;10}

The compiler will report an error on line 3rd, because the error is: identifier I cannot find it. We defined the variable i in line 5th, so the variable i is valid starting at line 5th, which is not valid in the previous 3rd line.

"0 Basic Learning iOS development" "02-c language" 04-constants, variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.