Basic data types and operators in C Language

Source: Internet
Author: User
Tags binary to decimal integer division

  1. Basic Data Types in C Language

    Char, int, float, double

    1>CharCharacter)

    The character enclosed in single quotes is char; ''contains characters, and" "contains strings. For example, char c = 'a ';

    Characters are displayed in graphs, but are stored in an encoded integer. Therefore, the characters have two unique features: character and data, the earliest character encoding is ASCIIAmerican Standard Code for Information Interchange) encoding. 'A' = 65 'A' = 97 '0' = 48.

    8-bit binary characters), 1 byte ).

    # Include <stdio. h> int main () {char c1 = 'a'; char c2 = 97; c1 = c1 + 1; printf ("c1 = % c \ n", c1 ); // B printf ("c1 = % d \ n", c1); // 66 printf ("c2 = % c \ n", c2 ); // a printf ("c2 = % d \ n", c2); // 97 printf ("sizeof (c1) = % d \ n", sizeof (c1 )); char c3 = '9'; int I = c3-'0'; printf ("I = % d \ n", I); char c4 =-191; // take the last 8 digits of-191 printf ("c4 = % c, c4 = % d \ n", c4, c4); return 0;}/* exercise: convert the numeric character '9' to a numeric 9 */

    Appendix: sizeof () Usage:

    Sizeof (type), sizeof (variable), sizeof (expression)

    The memory size of all computing types and variables is measured in bytes)

    When sizeof (expression) is used, the expression is not used for calculation and only the result is estimated. It is not recommended.

Exercise: enter a lowercase letter, convert it to a corresponding uppercase letter, and then output:

# Include <stdio. h> int main () {printf ("enter a lowercase letter \ n"); char c1, c2; scanf ("% c", & c1 ); c2 = c1-'A' + 'a'; printf ("c1 = % c, c2 = % c \ n", c1, c2); return 0 ;}

In C, there are some special characters calledEscape charactersFor example:

\ N newline character '\ n' is valid

\ R carriage return characterPress enter to return to the beginning of the line.

\ T horizontal tab (n blank)

\\\'\? \ "4 special characters

2>Integer intInteger)

Int integer represents two or four bytes. The current mainstream machine represents four bytes (32-bit binary ). Integer types also include:

Short int (short) short integer, 2 bytes

Long int (short for long) long integer, 4 bytes

Long int (C99) 8 bytes

Unsigned int unsigned integer (non-negative)

Signed int signed integer (positive number), default

An integer can be expressed in binary, octal, decimal, or hexadecimal notation.

The original code representation of an integer:

The first digit of the signed int is regarded as the symbol bit. 0 indicates a non-negative number, and 1 indicates a negative number. The expression range is from the 31 power of negative 2 to the 31 Power of Positive 2 to the 31 Power-1 of positive 2;

The unsigned int is a non-negative number. The expression range is 0 to the power of 32 to 1 of positive 2;

The differences between signed and unsigned memory are only the first priority. The number of symbols regards the first digit as a symbol bit, and does not participate in the operation. The number of unsigned symbols regards the first digit as a number and participates in the operation.

#include <stdio.h>int main(){    int a = -1;    printf("unsigned a=%u,a=%d\n",a,a);    unsigned int b = -1;    printf("unsigned b=%u,b=%d\n",b,b);    return 0;}

Convert binary to decimal)

Hexadecimal: 0-9, 10-15 is represented by a-fA-F.

Octal, decimal, and hexadecimal representation in C language:

Octal starts with 0, for example, 011 = 8 + 1 = 9 int a = 011;

The hexadecimal format starts with 0X/0x, for example, 0xF1 = 15*16 + 1 = 241 int a = 0xF1;

Binary and decimal conversion:

Positive number 2-> 10

0111 1011 = 64 + 32 + 16 + 8 + 2 + 1 = 112 + 11 = 123

0101 1101 = 64 + 16 + 8 + 4 + 1 = 93

Positive Value 10-> 2

108-> 108-64 = 44-32 = 12-8 = 4-4 = 0-> 0110 1100

0 1 1 0 1 0 0

87-> 87-64 = 23-16 = 7-4-2-1-> 0101 0111


Exercise: 0110 1011-> 107 125-> 0111 1101


Negative 10-> 2

Corresponding positive number binary, and then bitwise inverse plus 1 (complement)

-107-> 0110 1011-> 1001 0100 + 1 = 1001 0101

-58-> 0011 1010-> 1100 0110

Negative number 2-> 10

First subtract 1 and then return the corresponding positive number by bit. Then add the minus sign.

Return the bitwise result and Add 1 to get the corresponding positive number. Add the minus sign.

1101 1011-> 1101 1010-> 0010 0101->-37

1101 1011-> 0010 0100 + 1-> 0010 0101->-37

1111 1111->-1

Exercise:-77-> 0100 1101-> 1011 0011

1110 0111-> 0001 1001->-25

3> floating point type

Float Single-precision floating point

Double-precision floating point

Long double Extended dual precision (rarely used)

Floating-point numbers are decimal places, but floating-point numbers are approximate values related to computer storage ).

Note:Determine the type of an integer constant (literal value):

3 is considered as int

3.5 is considered double

3.5f is considered float

3.5L is considered as long double

35L is considered as long int

35LL is considered as long int

35u is considered as unsigned int

35UL is unsigned long

But the type can be converted to the type, and the type conversion is actually a change in memory.

2. Operators in C Language

1> operator priority:

The highest priority value operator =) of parentheses is extremely low.

2> Arithmetic Operator: +-*/% returns the remainder)

Int division, take the integer part, no decimal part is discarded );

% Can only be used as an integer and cannot be used as a decimal number;

The int and double operations are combined to convert the int to the double operation;

0 cannot be used as a divisor or a remainder; otherwise, a floating point exception running error occurs.) and the program is interrupted;

0.0 division can be performed, and the result is inf (infinite );

# Include <stdio. h> int main () {int a = 5, B = 2, c = 0; double d1 = 2.0, d2 = 0.0; printf ("a/B = % d \ n", a/B); // 2.5, printf ("a % B = % d \ n ", a % B); // 1 printf ("a/d1 = % lf \ n", a/d1); // 2.0, 2, 5, 2 // printf ("a % d1 = % d \ n", a % d1); // The floating point cannot be used for remainder, compilation error // printf ("a/c = % d \ n", a/c); // floating point number exception, printf ("a/d2 = % lf \ n", a/d2); // a/d2 = inf infinitely large) printf ("% d, % d, % d \ n ",-9/7,-9% 7, 9%-7); // when-1,-is used to calculate the remainder, the remainder symbol is the same as the divisor symbol C99) printf ("main over \ n"); return 0 ;}

Note: In C89, the numbers/% are uncertain. In C99, division is rounded to 0, and the remainder I % j is determined by I.

Exercise: Print two digits in reverse order, for example, 92-> 29 integer division and remainder)

# Include <stdio. h> int main () {int num, res = 0; printf ("enter a two-digit integer \ n"); scanf ("% d", & num ); res = num % 10*10 + num/10; printf ("res = % d \ n", res); return 0 ;}

3> value assignment operator =)

The value assignment operator can change the value of a variable. In C, except ++/--, the value of a variable can only be changed using the value assignment operator. The value assignment operators include = + =-= * =/=...

A + = B;-> a = a + B; the assignment operator is calculated from the right to the left, for example:

A = B = c = 0; // c = 0-> B = c-> a = B; however, this method is not recommended. The value assignment operator assigns the value on the right to the variable on the left. Therefore, the expression on the Left cannot be used. It is best to use a variable. The left is also called the left value.

I + j = 10; error: expression 10 = I on the left; error: value on the left, constant on the 10, and cannot be assigned a value.

4> auto increment and auto increment ++ /--)

Auto-increment and auto-increment are variable-specific. The error code is 5 ++. ++/-- Can be used for char, int, float, and double types, but few floating-point variables are used.

Auto-increment and auto-increment can be used in the following ways: First ++ (++ I) and last ++ (I ++), both increase by 1. However, before ++, the operation starts with auto-increment, and after ++ is used in the operation. After ++, perform the first operation and then auto-increment, and use the number before ++ to perform the operation. The front ++ can be used together, and the back ++ can be used together)

Experience: I ++/++ I is used as a separate statement. Do not mix it with other statements.

#include <stdio.h>int main(){    int i=1,res;    double d = 1.0;    d++;    printf("d=%lf\n",d);//2.0    printf("i++=%d\n",i++);//1    printf("i=%d\n",i);//2    printf("++i=%d\n",++i);//3    printf("i=%d\n",i);//3    //res = i++ + ++i;//8 4+4    //res = i++ + i++;//6 3+3    res = ++i + ++i;//10  5+5    printf("res=%d,i=%d\n",res,i);    return 0;}

5> logical expressions

Relational operators: >>=<=

= Equal (= value assignment)

! = Not equal

The value of a logical expression is 1 (logical truth, true) or 0 (logical false, not true ). HoweverIn C, non-0 is true, and 0 is false (for variables and non-logical expressions ).

Logical operators can be used for true and false (logical operations:

! The logic is not the opposite)

& Both logical and (and) are true, and the result is true. Otherwise, the result is false.

| Both logic and (or) are false, and the result is false. Otherwise, the result is true.

& | Optimized the algorithm.Short circuit effect. ForLogic and, if the previous step is false, no operation is performed laterForLogic or, true in front, not followed by Computation.

# Include <stdio. h>/* test the short-circuit effect between logic and logic */int main () {int I = 0, j = 0; if (I ++ & j ++) {} // if (0 & j ++) j ++ does not run if (I -- | j ++) {} // if (1 | j ++) j ++ does not run printf ("I = % d, j = % d \ n", I, j ); return 0 ;}

6> bitwise operators (only standard int and char can be used)

Bitwise AND&Bitwise and operation (two integers) are both 1 and result 1; otherwise, 0

Bit or|Bitwise OR (two integers): 0 and 0; otherwise, 1

Bitwise Inversion~Bitwise inversion (one integer) bitwise inversion ,~ 3 + 1 =-3-> ~ 3 =-4

Bitwise OR^Two bitwise XOR operations (two integers) are performed based on binary bits. The result is 1. Otherwise, the result is 0.

Note:

You can set the corresponding binary bit to 0 or get the corresponding value. Bits and 111 0 111 are set to 0.

Bit or you can set the corresponding binary bit to 1 or get the corresponding value. Bit or 000 1 000 set 1.

Bitwise is exclusive or binary digits can be reversed. Bitwise OR 000 1 000 can be reversed.

# Include <stdio. h> int main () {int a = 5, B = 7; // 0101 0111 printf ("a & B = % d \ n", a & B ); /// 0101 = 5 printf ("a | B = % d \ n", a | B ); // 0111 = 7 printf ("a ^ B = % d \ n", a ^ B); // 0010 = 2 printf ("~ A = % d \ n ",~ A); //-6 printf ("B <2 = % d, B> 2 = % d \ n", B <2, B> 2 ); // 11100 = 28,0001 = 1 B = B & 0 xFFFFFFFB; // 111... 1011 = 11 = B printf ("B = % d \ n", B); printf ("% d \ n", (B & 0x00000004 )! = 0); // judge whether the last and third bits of B are 1 return 0 ;}

7> shifts left and right

Left shift <shifts left by binary. During the Left shift operation,Add 0 to the following blank space

Right Shift> right shift by binary bit. During the right shift operation, when there is a space in front, the signed bit is supplemented by the number of symbols, and the unsigned number is supplemented by 0.

Note: Shifts one to the left is equivalent to * 2, and shifts one to the right is equivalent to/2, but the efficiency is higher.

8> &/* operator

& Get address * Get Variable Based on address

% P in printf is used to output the address.

The address is numbered by byte, and the starting address of a variable is obtained from the address.

9> three-object Operator

(Condition, logical value )? Expression 1: expression 2 ---> If the condition is true, expression 1 is used; otherwise, expression 2 is used.

Int a = 10; (a> 10 )? 11: 10; // If a> 10, take 11; otherwise, take 10 (a> 10 )? 9.0: 8; // The value is 8.0, and the type is automatically increased. A> B? A: B // obtain the maximum value of a and B

3. type conversion

1> floating-point and integer operations, automatically converted to floating-point operations;

2> integer operations smaller than intchar and short, which are automatically converted to int;

3> forced type conversion format: (target type) () Type of the result;

The forced type conversion operator (target type) can be omitted. Char c = (char) int;


This article from the "Snow Wolf" blog, please be sure to keep this source http://wolfzhxp.blog.51cto.com/2409990/1285495

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.