C language basics-Lesson 2-data types and operators and Data Type Operators

Source: Internet
Author: User

C language basics-Lesson 2-data types and operators and Data Type Operators
1. Data Type 1.1 constant in C Language

A constant is an unchangeable amount in a program. when defining a constant, an initial value must be given.

1.1.1 # define

Define a macro constant

1.1.2 const

Define a const constant

1.2 string constants

"Hello world"

For# DefineType constant,CThe habit of the language is that the constant name is capitalized, but for commonConstConstants and variables, usually in lower case and in upper case

1.3 binary, bit, byte, and word

We are used to decimal numbers: 10, 12, etc.

A single bit can only represent 0, or one of the two States, bit for short, a single bit is a bit

One BYTE is eight binary bytes, which are referred to as BYTE. The eight bits are one BYTE.

A word is two bytes.

Dual words, DWORD for short

1.4

Octal is a base-8 number system. In C, 0 indicates octal, 0666;

1.5 hexadecimal

A hexadecimal system with a base value of 16. It uses 0x in C to represent the hexadecimal system.

Decimal

Hexadecimal

Binary

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

 

 

 

 

Convert decimal to octal, use decimal number as the divisor, and 8 as the divisor. Take the quotient number and remainder until the quotient number is 0.

Convert decimal to hexadecimal notation. Use decimal number as the divisor, and use 16 as the divisor. Take the commodity number and remainder until the commodity number is 0.

1.6 original code

Use the highest bit as the sign bit (0 indicates positive, 1 indicates negative), and the rest represent the absolute value of the value.

+ The original Code of 7 is 00000111

-The original Code of 7 is 10000111

The original code of + 0 is 00000000.

-The original code of 0 is 10000000

 

 

1.7 reverse code

If the value of a number is positive, the anticode is the same as the original code.

If a number is negative, the symbol bit is 1. Others are opposite to the original code.

+ 7 reverse code 00000111

-7 anti-code 11111000

-0 anticode 11111111

 

1.8 Complement

Both the original code and the reverse Code are not conducive to computer operations. For example, the addition of 7 and-7 in the original code also requires the judgment of the symbol bit.

Positive: the original code and reverse code are all the same

Negative number: the highest bit is 1, and the rest of the original codes are reversed. Finally, the entire number is + 1.

-7 completion code: =

10000111 (original code)

111111000 (reverse code)

11111001 (Supplemental code)

+ 0 completion code is 00000000

The-0 complement code is also 00000000

The complement sign does not move. If the other bits are reversed, the entire number is + 1 to obtain the original code.

Perform computation using the complement code. subtraction can be implemented through addition.

7-6 = 1

The sum of the 2's complement and The 2's complement: 00000111 + 11111010 = 100000001

After the carry is discarded, the remaining 00000001 is the complement of 1.

-7 + 6 =-1

-Add the complement Code of 7 and the complement Code of 6: 11111001 + 00000110 = 11111111

11111111 is the complement of-1

 

1.9 sizeof keywords

SizeofYesCLanguage keyword, which is used to determine the size of the specified data type in the memory. Unit: bytes.

Sizeof and size_t types 

1.10 int type 1.10.1 int constant, variable

Int Is a 32-bit binary integer that occupies 4 bytes of space in the memory.

1.10.2 printf output int Value

% D: returns a signed 10-digit integer, % u, representing an unsigned decimal integer.

1.10.3 printf outputs octal and hexadecimal

% X, indicating the hexadecimal number, % X, and The hexadecimal number in uppercase.

% O indicates the number of 8 bytes output.

1.10.4 short, long, long, unsigned int

Short refers to a short integer, which is a 32-bit system consisting of 2 bytes and 16 bits.

Long indicates a long integer. In a 32-bit system, long is 4 bytes. In a 64-bit system, windows is 4 bytes, and unix is 8 bytes.

Int Is 4 bytes in both 32-bit and 64-bit systems, windows and unix.

Long long is a 64-bit integer (8-byte integer). For 32-bit operating systems, the CPU register is 32-bit, so the efficiency of longlong Data calculation is very low.

9l, 9L, 9ll, 9LL, 9u, 9ull, 9ULL

1.10.5 Integer Overflow

When an integer exceeds the maximum unit that the integer can accommodate, the integer overflows and the overflow result is high-level discard.

When a small integer is assigned to a large integer, the symbol bit will not be lost and will inherit

1.10.6 large-end and small-end alignment

For the complex instruction CPU of the intel x86 architecture such as arm, integers are stored in the memory reversely. Low addresses are low, high addresses are high, and small ends are aligned.

However, for the CPU Of a unix server, the large-end alignment is used to store integers.

1.11 char type 1.11.1 char constant, variable

Char c; defines a char variable

'A', char constant

Char is essentially an integer, an integer of only one byte size.

1.11.2 printf output char

% C means to output a character, not an integer

1.11.3 the char Escape Character cannot be printed

\ A, alarm

\ B Return

\ N line feed

\ R press ENTER

\ T Tab

\ Slash

\ 'Single quotes

\ "Double quotation marks

\? Question mark

1.11.4 char and unsigned char

Char value range:-128 to 127

Unsigned char is 0-255

1.12 floating point float, double, long double Type 1.12.1 floating point constant, variable

Float is 4 bytes in a 32-bit system, and double is 8 bytes in a 32-bit system.

Decimal point efficiency is very low. Avoid using it unless you explicitly want to calculate a decimal point.

1.12.2 printf output floating point number

% F, % Lf

% F is a double output.

% Lf output a long double

1.13 type limitation 1.13.1 const

Const is a constant that cannot change the value.

1.13.2 volatile

It indicates that a variable may be changed outside of the CPU instruction, and the compiler will not optimize the target code for this variable.

1.13.3 register

Variables are in the CPU registers rather than in the memory. But regist is a recommended command, not a command.

2. String formatting: storage of Input and Output 2.1 strings in the computer

A string is a continuous char space in the memory and ends with '\ 0'.

 

"" Is a way to express strings in C Language

2.2 printf function, putchar Function

Printf format characters

Character

Corresponding data type

Description

D

Int

Accept the integer and express it as a signedDecimalInteger

Hd

Short int

Short integer

Hu

Unsigned short int

Unsigned short integer

O

Unsigned int

Unsigned octal integer

U

Unsigned int

Unsigned base 10Integer

X/X

Unsigned int

Unsigned16HexadecimalInteger. x corresponds to abcdef, and X corresponds to ABCDEF.

F

Float or double

Single-precision floating point numberOrDouble-precision floating point number

E/E

Double

Scientific notationThe number. Here, the case of "e" indicates the case of "e" used in the output.

C

Char

CharacterType. You can set the input numberASCIICodeConvert to corresponding characters

S/S

Char */wchar_t *

String. Output the characters in the string until the null characters in the string (the string ends with '\ 0', and this' \ 0' is an empty character)

P

Void *

Output in hexadecimal formatPointer

%

%

Output A percent sign

Additional printf format

Character

Description

L

Appended to d, u, x, o, indicates a long integer

-

Left aligned

M (representing an integer)

Minimum data width

0

Add 0 to the front of the output until the specified column width is full. This parameter cannot be used together-

N (representing an integer)

At least n characters in width are not filled with spaces

 

Putchar is a function that explicitly represents a character.

 

2.3 scanf and getchar Functions

Scanf reads user input through the keyboard and puts it into the variable. Remember that the parameter must be the address of the variable (&)

Int a = 0;

Int B = 0;

 

Scanf ("% d", & a); // you must use the & get variable address.

Scanf ("% d", & B); // you must use the & get variable address.

 

Getchar: Get the character entered by the user's keyboard

3. operator expressions and statements 3.1 basic operators 3.1.1 =

Data Object: refers to the storage area of data in the memory.

Left: indicates the data object that can be changed.

Right value: the amount that can be assigned to the left value.

3.1.2 +

Add

3.1.3-

Subtraction

3.1.4 *

Multiplication

3.1.5/

Division

3.1.6%

Returns the remainder.

3.1.7 + =

Add equals

3.1.8-=

Equal to or less

3.1.9 * =

Multiplication equals

3.1.10/=

Division equals

3.1.11% =

Remainder equals

3.1.12 ++

Auto-increment 1

, I ++ calculates the expression value first, and then ++

, ++ I is the first ++, and then the value of the expression is calculated.

3.1.13 --

Auto-minus one

3.1.14 comma Operator

Int a = 2;

Int B = 3;

Int c = 4;

Int d = 5;

Int I = (a = B, c + d );

 

The comma expression first calculates the value on the left of the comma and then the value on the right. The value of the entire statement is the value on the right of the comma.

3.1.15 operator priority

Priority

Operator

Associativity

1

++ (Suffix), -- (suffix), () (call function), {}( statement block),...,->

Left to right

2

++ (Prefix), -- (prefix), + (prefix),-(prefix ),! (Prefix ),~ (Prefix), sizeof, * (take pointer value), & (take address), (type) (type conversion)

From right to left

3

*,/, %

Left to right

4

+ ,-

Left to right

5

<>

Left to right

6

<> <=> =

Left to right

7

=! =

Left to right

8

&

Left to right

9

^

Left to right

10

|

Left to right

11

&&

Left to right

12

|

Left to right

13

?

From right to left

14

=, * =, % =, + =,-=, <=, >>=, & =, | =, ^ =

From right to left

15

, (Comma operator)

Left to right

 

3.2 compound statements

{} Code block

3.3 empty statement

There is only one; the number statement is an empty statement, which is in the C language and is required in some scenarios.

3.4 type conversion

Double f= (double) 3/2;

() Is the forced type conversion operator.

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.