iOS development skill tree C language - basic knowledge

main function

A C program has one and only one main function, the main function. The main function is the only entry in the C language, and the C program executes the code in the main function.

#include <stdio.h>
int main() {
    printf("First C ");
    return 0;
}

#includeIt is a preprocessing specification, which is used to include the specified file (processed before compilation), copy the corresponding file to the specified location, and include any type of file.

#includeThere are two ways to include files, use <>and "", <>include will only check the compiler library function file, which is suitable for including library functions; ""include first searches the current directory of the program, if not found, it searches for the library function path, which is suitable for custom files.

The C program must be executed from the main function.

basic data type

type of data

  • Integer (int): a number without decimals, format character: %d
  • Character type (char): store a single character, format character: %c
  • Floating point type: numbers with decimals, single precision (float), double precision (double), format character: %f-6 decimal places, %e-scientific notation

%o, output integer in unsigned octal
%x, output integer in unsigned hexadecimal

    C语言不存在字符串变量,字符串只能存在字符数组中。
//格式化输出
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");

    int age = 23;
    float height = 180.3;
    char sex = 'M';
    printf("age:%d,height:%.2f,sex:%c \n",age,height,sex);
    return 0;
}

type modifier

short short type, modified int, double
long long type, modified int, double
signed signed type, modified int, char
unsigned unsigned type, modified int, char

operator

1. Arithmetic operators

  • Addition: +
  • Subtraction:-
  • multiplication:*
  • division:/
  • Remainder (modulus): %
  • Auto increment: ++
  • Decrement:-
#include <stdio.h>
int main()
{
    int x = 10;
    int y = 2;
    printf("x+y=%d\n",x+y);
    printf("x-y=%d\n",x-y);
    printf("x*y=%d\n",x*y);
    printf("x/y=%d\n",x/y);
    x--;
    printf("x--:%d\n",x);
    x++;
    printf("x++:%d\n",x);
    printf("x%%y=%d\n",x%y);//打印% -%%

    return 0;    
}
  • The two self-increment expressions: a++, ++a, are equivalent to a=a+1. After the expression is executed, the value of a is increased by 1. However, the operation rules of the two expressions are different. The ++a expression is evaluated first and then evaluated, while the a++ expression is evaluated first and then evaluated. It is only for the value of the expression, and has no effect on the value of a itself.
     int a = 10;
    printf("a ++ :%d \n",a++);
    printf("a:%d \n",a);

    int b = 10;
    printf("b ++ :%d \n",++b);
    printf("b :%d,\n",b);
打印结果:
a ++ :10 
a:11 
b ++ :11 
b :11

2. Assignment operator: =

  • Simple assignment operator: =
  • Conforms to assignment operators: add other operators in front of simple assignment operators such as: +=, -=, *=, /=, %=

3. Relational operators

  • Greater than: >
  • Greater than or equal to: >=
  • Less than: <
  • Less than or equal to: <=
  • is equal to: ==
  • Not equal to: !=

The results of relational expressions are simply "true" and "false", represented by 1s and 0s.

4. Logical operators

  • Logical AND: &&, when both variables involved in the operation are true, the result can be true, and if the previous variable is false, the latter variable will not be verified
  • Logical OR: ||, as long as one of the two variables involved in the operation is true, the result is true, otherwise the result is false. And, as long as the first variable is true, subsequent variables are not validated.
  • Logical NOT: ! , when the participating variable is true, the result is false, and when the participating variable is false, the result is true.

The results of logical operators are simply "true" and "false", represented by 1 and 0.

5. Ternary operator

  • Format: expression1? expression 2 : expression 3;
    to determine whether expression 1 is true, execute expression 2 if true, and execute expression 3 if false.

Generally, ternary operation is used to solve some simple judgments, instead of if-else, which is more concise.

6. Operator precedence

from the web

The highest level of operators in the C language is ()that it is often used to solve order problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325892289&siteId=291194637