Data types, constants, variables in C

Source: Internet
Author: User

One, data 1. What is Data

Life is always with the data, such as weight data, blood pressure data, stock price data and so on. 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 .

2. Classification of data

The data stored in the computer can be divided into two types: static data and Dynamic Data.

1> static data

L Concept: Static data refers to some permanent data that is generally stored on the 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.

L 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 is always in.

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 (temporary data)

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

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

What is Dynamic Data: When a program (software) is run, the entire program is loaded into memory, and during the run of the program, a variety of temporary data is generated 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 .

What data does the programmer care about the most?

3> conversion of static and dynamic Data

Static -- dynamic

Dynamic - static

3. Size of the data

1) both static and dynamic data are composed of 0 and 1 . How do 0 and 1 make so much data?

2) data are size, static data will occupy the space of the hard disk, dynamic Data occupies memory space

3) The larger the data, the more the 0 and 1 are included , bits and bytes

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

4. Appa variety of data in

5. Cdata types in the language

Because of the wide variety of data in theapp,C - language data is classified to facilitate the operation of data.

Two, constant 1. What is a constant

Constant, which represents some fixed data

2. Classification of constants

1> integral type constant (int)

Includes all integers, such as 6,109,-10 , , 0 , -289 wait

2> Floating-point constant (float\double)

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

u double: double-precision floating-point type, in fact, is a decimal. such as 5.43 , -2.3 0.0 0.0 also a decimal number )

u 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 , thefloat 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)

U will have a number (0~9), English alphabet (a~z,a~z) or other symbols (+,-,!,?and so on) are enclosed in single quotation marks, which make up a character constant. Like' 6 ',' A ',' F ',' + ',' $ 'and so on.

Note: The single quotation mark can only enclose 1 characters, and cannot be Chinese characters, the following writing is wrong:' abc ',' 123456 ', ' man '

4> String Constants

u  one or more characters in double quotation marks ( "6" " man " " Wow, " , " MY_CAR4 " actually printf ("Hello world"); in the statement

What is the difference between 6,' 6 'and' 6 ' in usage? This is not to be discussed and will be introduced later.

3. Exercises

What types of constants are the following?

10.6 19.0f 0.0 0 ' A ' "Male" "MJ" 294 ' + '

Third, the variable 1. What is a variable

When the value of a data needs to change or be uncertain, it should be represented by a variable. such as game points.

2. Defining variables

1> Purpose

You must define any variable before it is used.

The purpose of defining variables is to allocate a chunk of memory to the variable to store the data later.

If you define more than one variable, you allocate different storage space for each variable.

2> format

Variable type variable name ;

such as int num;

L variable name belongs to identifier

L Variable Type

U different types of variables occupy different sizes of storage space. Memory is extremely limited, allocating the appropriate storage space

The type of data stored by the U constraint variable (convenient operation)

3> instances

int main ()

{

int i;

char c;

int A, B;

return 0;

}

3. Use of variables

1> Assignment Value

L put something in the variable, it's assignment. With a semicolon after the assignment statement ;

i = 10;

Note: The equals sign here isnot "equal" in mathematics, but rather the assignment operator in C , which assigns the right constant to the left variable I

L first assignment, which can be called "Initialize"

L Two types of initialization

U define first, then initialize:int A; A = ten;

The U-definition is initialized at the same time:int a = ten;

2> modification

L can modify the value of a variable and assign it multiple times. Each assignment will overwrite the original value

i = 10;

i = 20;

the last value of the variable i is

L Use printf to output the value of one \ Multiple variables

int a = ten, c = 11;

printf ("a=%d, c=%d", A, c);

L Double\float\char output, some tips for formatting characters

Double height = 1.55;

char blood = ' A ';

printf ("height=%.2f, blood type is%c", height, blood);

L Simple Add-subtract operation

int a = 10 + 20;

Do not use when not initialized ( the following wording is not recommended)

int score;

printf ("score=%d", score);

Transfer of values between 3> variables

L can assign the value of a variable to another variable

int a = 10;

int b = A;

L Continuous Assignment

A = b = 10;

4. Common errors

1> variable name is the same as int a = 10; int a = 12;

The scope of the 2> variable is not correct

Creation and release process of U variables

U code block scope {int a = 10;}

5. Exercises

1> swaps The value of an integer variable a,b . such as a=10,b=11; the value of a after Exchange is one,b is a value of ten. Implemented in two ways:

U use third-party variables

int temp;

temp = A;

A = b;

b = temp;

U do not use third-party variables

A = b-a;

b = b-a;

A = B + A;

2> Watch the game interface, think about how many variables need to be defined?

(Note: Refer to Yu Shunji)

Data types, constants, variables in C

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.