Technology Sharing

C language - basic framework, variables, operators

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Basic framework:

  1. #include<stdio.h> //编译预处理指令
  2. int main() //程序的入口主函数main
  3. { //程序(函数、功能)结束标志
  4. return 0; //程序退出前返回给调用者(操作系统)的值
  5. } //程序(函数、功能)结束标志

variable:

The emphasis is on change. Its value will change during the program running.

Four characteristics: variable name, variable value, storage unit, variable type; define first and then use.

(1) Variable name/identifier:

It consists of letters, numbers and underscores, and can only start with an underscore or a letter, not a number. It is case sensitive.

Naming method: As the name implies, use English naming. Camel case naming, int secondPreYear. Function names are often connected by underscores.

(2) Variable type:

To keep it simple, in operating systems (Windows, Linux), integers are integers (4 bytes = 32 bits), floating-point types are decimals (float: 4 bytes = 32 bits), and characters are ASCII codes (char: 1 byte = 8 bits) (which can be represented by integers)

Note: 32 bits is 2 to the power of 32

In a single-chip microcomputer, an integer is 2 bytes = 16 bits, which means that the maximum number that can be represented is 2 to the 16th power, or 65536.

Operators:

The operators in C language are very rich and can be roughly divided into the following categories:

  1. Arithmetic operators: Used to perform basic mathematical operations.
    • +addition
    • -Subtraction
    • *multiplication
    • /division
    • %Find the remainder (modulus)
    • ++Auto-increment
    • --Decrement
  2. Relational operators: Used to compare two values.
    • ==equal
    • !=not equal to
    • >more than the
    • <Less than
    • >=greater or equal to
    • <=Less than or equal to
  3. Logical Operators: Used to perform logical operations.
    • &&Logical AND
    • ||Logical OR
    • !Logical NOT
  4. Bitwise Operators: Used to operate on the binary bits of integers.
    • &Bitwise AND
    • |Bitwise OR
    • ^Bitwise XOR
    • ~Bitwise inversion
    • <<Shift Left
    • >>Shift Right
  5. Assignment Operators: Used for assignment.
    • =Assignment
    • +=Accumulation Assignment
    • -=Cumulative decrement assignment
    • *=Accumulated multiplication assignment
    • /=Cumulative division assignment
    • %=Remainder Assignment
    • <<=Left shift assignment
    • >>=Right shift assignment
    • &=Bitwise AND assignment
    • |=Bitwise OR assignment
    • ^=Bitwise XOR assignment
  6. Conditional Operators(also known as the ternary operator):
    • ? :Conditional operators, such asa > b ? a : bIfa > bIf established, the result isa, otherwiseb
  7. Comma operator
    • ,The comma operator is used to execute multiple expressions in sequence, but the result of the entire expression is the result of the last expression.
  8. sizeof Operator
    • sizeofOperators are used to get the number of bytes that a data type or variable occupies in memory.
  9. Type conversion operators
    • (type)The type conversion operator is used to convert the value of an expression to a specified type.
  10. Subscript OperatorandStructure/union member access operators
    • []Subscript operator is used to index into an array and to access the offset of an element pointed to by a pointer.
    • .Member access operator for a structure or union.
    • ->Access members of a structure or union through a pointer.
  11. Pointer Operators
    • &Address-of operator.
    • *Pointer dereference operator (also the multiplication operator, but context determines its meaning).

These operators play a very important role in C language and are the basis for building various complex expressions and statements.