directory of this document
- First, the basic statement
- II. Basic Operations
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore
Back to the top one, the basic statement
The basic language of C is almost the same as in Java, so here's just a simple way to mention
- Loop statements (Do while, while, for)
- Conditional statements (if, if-else, switch)
- Goto statement
Back to top second, basic operations
The basic ability of a computer is computing, so the computational power of a language is very important. The reason why C is omnipotent is that it has strong computational power. There are 34 types of operators in C, most of which are identical to those used in Java. Here is a brief introduction.
1. Arithmetic operators
- -subtraction operator, or negative operator
- * Multiplication operator
- % modulo operator, or call-to-rest operator, requires that both sides are integral
2. Relational operators
- <= less than equals operator
- >= greater than equals operator
- ! = does not equal operator
* The result of the relational operation is "true" (for example, 5>=4), which is "false" (such as 5<4).
* In Java, the result of the relational operation is "true" returns True, "false" returns False, with a Boolean type of variable to receive
Boolean B1 = 5 > 4; Trueboolean B2 = 5 < 4; False
There is no Boolean type in the C language
* In the C language, the result of the relational operation is "true" returns 1, "false" returns 0
int a1 = 5 > 4; 1int A2 = 5 < 4; 0
* Also note that in C, any value other than 0 is true, and only 0 is false
So the following is the right approach:
int a = 10;if (a) { printf ("conditional");} else { printf ("condition not valid");}
Because A is not 0, it is "true" and the output is:
In Java, the compiler directly complains because the If bracket () can only put a Boolean value.
And so on, the following wording is also true:
int a = 10;if (A = 0) { printf ("conditional");} else { printf ("condition not valid");}
The above code is completely reasonable, the compiler will not error, just a warning. Because A is 0, the result is "false" and the output is:
In this sense, C language seems to be much more convenient than Java, in fact there are a lot of traps here:
Suppose you want to judge whether a is 0, then Ben should write if (a = = 0), if you mistakenly write if (a = 0), it would be a very scary thing, because the compiler does not error, such a bug is difficult to find. Therefore, the expression like a==0, preferably written 0==a, if you mistakenly write 0=a, the compiler will directly error.
Not recommended if (A = = 0) {}//recommended if (0 = = a) {}
* In C, you can not save the results of relational operations
Therefore, the following wording is legal:
1 int a = 10;2 a > 10;3 a = = 0;
If it is in Java, the 2nd, 3 line compiler will directly error, but the C compiler appears to be legal, just a warning.
So, here is another trap, assuming that your intention is to assign a value of 0, then Ben should write a = 0; , if you mistakenly write a = = 0; , it will be a very hard to find bug, because the compiler won't make any errors at all. In 1993, the bug almost blew up a $20 million hardware business because if the bug didn't work, the product wouldn't work.
3. Logical operators
The result of the logical operation is also only two: the establishment is "true", return 1; "false", return 0
4. Assignment operators
1. Simple assignment operator =: int a = 5;
2. Compound assignment operator
- + = Plus assignment operator. such as a + = 3+1, equivalent to a = A + (3+1)
- -= minus assignment operator. such as a-= 3+1, equivalent to A = A-(3+1)
- *= multiply the assignment operator. such as a *= 3+1, equivalent to a = A * (3+1)
- /= In addition to the assignment operator. such as a/= 3+1, equivalent to A = A/(3+1)
- %= the value of the remainder assignment operator. such as a%= 3+1, equivalent to a = a% (3+1)
5. Self-increment and decrement operators
- + + auto-increment operator. such as A++,++a, are equivalent to a = a+1
- --the self-decrement operator. such as a--,--a, are equivalent to a = A-1
6. Comma operator and comma expression * The comma operator is used primarily for connection expressions, for example:
1 int a = 9;2 int b = 10;3 4 A = a+1, B = 3*4;
* An expression concatenated with the comma operator is called a comma expression, and its general form is:
Expression 1, expression 2, ..., expression n
The operation of the comma expression is: From left to right, evaluates expression 1, evaluates expression 2, ..., and evaluates expression n
* The value of the entire comma-expression is the value of the last expression
1 int a = 2;2 int b = 0;3 int c;4 5 c = (++a, a *= 2, B = A * 5); 6 7 printf ("C =%d", c);
The result of ++a is 3,a *= 2 results for 6,b = A * 5 result is 30. As a result, the output is:
It is important to note that the right expression is wrapped in parentheses (), if not enclosed in parentheses, that is:
c = ++a, a *= 2, b = A * 5;printf ("c =%d", c);
The output will be: because C = ++a is also part of the comma expression and is independent of the subsequent a *= 2 and B = A * 5
7. Conditional operators and conditional expressions
is actually a three-mesh operator, the general form is: expression 1? Expression 2: Expression 3
int a = (b > 5)? 10:9;
8.sizeof
* sizeof can be used to calculate the number of bytes of memory for a variable or a constant, a data type.
int size = sizeof;p rintf ("10 Bytes:%d", size);
Output: 10 is data of type int, in 64bit compiler environment, int type needs to occupy 4 bytes
* sizeof has 3 different forms
- sizeof (variable \ constant)
sizeof (+); char c = ' a '; sizeof (c);
- sizeof variable \ constant
sizeof 10;char C = ' a '; sizeof C;
sizeof (float);
Note that it is not possible to write sizeof float;
"C Language" 07-Basic statements and operations