C language knowledge Summary (1), C language knowledge Summary

Source: Internet
Author: User

C language knowledge Summary (1), C language knowledge Summary

Data Types in C Language

Variable Storage

 

Variable instance

int b = 10;int a = 20;

Scanf Functions

int age;scanf("%d",&age);

Note: % d reads a decimal integer % o reads an octal integer % x or % X reads a hexadecimal integer

When multiple data is input using the scanf function, what is the delimiter in the middle of each value? What is input? If each value is separated by a space, each time we enter an integer, we must enter a delimiter, which can be a space, a tab, or a carriage return.

Do not include \ n in the first parameter of scanf, for example, scanf ("% d \ n", & a); this will cause the scanf function to fail.

Arithmetic Operation

The remainder after the division of two integers % both sides can only be positive and negative integers, depending on the value on the left of %

Forced type conversion

double a = (double)(1 / 2);

Sizeof

Function: used to calculate the memory bytes occupied by a variable, a constant, or data type.

Usage: sizeof (variable \ constant) sizeof variable \ constant sizeof (data type)

Condition judgment

In C, "any non-0 value is true, and only 0 is false"

Notes for Link comparison:

  • =,! In Relational operators ,! = Has the same priority. <, <=,>,> = has the same priority, and the former has a lower priority than the latter: 2 = 3> 1
  • The Union direction of Relational operators is "from left to right": 4> 3> 2

  • The priority of Relational operators is less than that of arithmetic operators: 3 + 4> 8-2

Logical operation

1. & logic and

1> use format Condition A & Condition B

2> the calculation result is 1, that is, "true", and 0, that is, "false", only when Condition A and Condition B are both true"

3> the operation process always determines whether Condition A is true.

  • If Condition A is true, then judge whether Condition B is true. If Condition B is true, the result of Condition A & Condition B is 1, that is, true ", if Condition B is not true, the result is 0, that is, "false"

  • If Condition A is not true, Condition B will not be judged whether it is true. Because Condition A is no longer true, regardless of Condition B, the result of "Condition A & Condition B" must be 0, that is, "false"

4> note

  • If you want to determine whether the value of a is within the range of (3, 5), do not write it as 3 <a <5, because the Union direction of Relational operators is "from left to right ". For example, if a is 2, it calculates 3 <a, that is, 3 <2. The condition is not true and the result is 0. Compare with 5, that is, 0 <5. The condition is true and the result is 1. Therefore, the result of 3 <a <5 is 1 and the condition is true. That is to say, when the value of a is 2, the value of a is within the range of (3, 5. This is obviously incorrect. The correct judgment method is: (a> 3) & (a <5)

  • C language: any non-0 value is true, and only 0 is false ". Therefore, logic and values are also applicable to numerical values. For example, the result of 5 & 4 is 1, which is "true"; the result of-6 & 0 is 0, which is "false"

2. | logical or

1> use format Condition A | Condition B

2> when Condition A or condition B is set to 1 (both Condition A and Condition B are true), the result is true "; if neither Condition A nor Condition B is true, the result is 0, which is false ".

3> the operation process always determines whether Condition A is true.

  • If Condition A is true, Condition B will not be judged whether it is true. Because Condition A is true, regardless of Condition B, the result of "Condition A | Condition B" must be 1, that is, "true"
  • If Condition A is not true, then judge whether Condition B is true. If Condition B is true, the result of Condition A | Condition B is 1, that is, true ", if Condition B is not true, the result is 0, that is, "false"

3 ,! Non-logical

1> use format! Condition

2> the calculation result is false or false.

4. Priority

  • The priority of logical operators is: parentheses ()> minus sign->!> Arithmetic Operators> Relational operators >&>||
  • Expression! (3> 5) | (2 <4) & (6 <1): Calculate first! (3> 5), (2 <4), (6 <1), the result is 1, and the formula becomes 1 | 1 & 0, and then 1 & 0, formula 1 | 0. The final result is 1.

  • Expression 3 + 2 <5 | 6> 3 is equivalent to (3 + 2) <5) | (6> 3). The result is 1.

  • Expression 4> 3 &&! -5> 2 is equivalent to (4> 3 )&&((! (-5)> 2), the result is 0.

Three-object Operator

For conditional expression B? X: y. Calculate Condition B and then judge. If the value of B is true, the value of x is calculated and the calculation result is the value of x. Otherwise, the value of y is calculated and the calculation result is the value of y.

 

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.