Programmer's Tutorial-Chapter 9-C programming

Source: Internet
Author: User
Tags bitwise bitwise operators

Directory structure:

9.1 C Language Foundation

9.1.1 Data types

1 Basic data types

2 arrays, character arrays, and strings

3 Enum types

4 structs, common bodies, and typedef

9.1.2 Operators and expressions

9.1.3 Input/Output

9.2 Control Statements

1 SELECT statement

2 Loop statements

3 Break statement

4 Continue statements

5 Return statement

9.3 Functions

1 function definitions

2 function declarations

3 function calls

4 Recursive functions

9.4 Hands

9.4.1 the definition of a pointer

1 NULL pointer

2 "&" and "*"

3 pointers vs. heap memory

9.4.2 Pointers and Arrays

1 accessing array elements through pointers

2 accessing string constants through pointers

3 pointer array

4 pointer arithmetic

5 constant pointer and pointer constants

9.4.3 Pointers and functions

1 Pointers as Function parameters

2 pointer as function return value

3 Function pointers

9.4.4 pointers and linked lists

9.5 Common C Program errors

1 The case of identifiers is different

2 ignoring the type of the variable causes the operation to be illegal

3 character constants are confused with string constants

4 References to uninitialized variables

5 "=" and "= ="

6 fewer semicolons or multiple semicolons

7 use of scanf and printf functions

Missing break in 8 switch statement

9 Data Overflow

10 fixed-length arrays and variable-length arrays

11 valid range of array subscripts

12 confusing array names with pointer variables

13 frequently asked questions about using pointers

Enter the text ...

C language is a general programming language, which is commonly used to write system software and to develop embedded applications.

9.1 C Language Foundation

Writing programs in C involves basic elements such as data types, operators, expressions, constants and variables, statements, function definitions, and function calls.

9.1.1 Basic data types

The basic data types for C are character (char), int (int.), float (float, double)

The void type is also a basic type

1) Variables: variables essentially refer to memory units that store data; variable declarations: variable names can be declared by using the extern keyword

2) literal: Refers to the data in the source program directly in the form of a value, can not be modified in the program operation

3) Const constant: The object to which it is decorated is a constant

4) Scope of identifiers and names: variable names, function names, labels, and user-defined data type names used in C programs are collectively referred to as identifiers

2 arrays, character arrays, and strings

1) Array: slightly

2) Character array and string: The string ends with a special character ' \ S ' and a multiline string ends with \ as the line

3 Enum types

4 structure, common body and typedef:typedef used to define the structure and the body

9.1.2 Operators and expressions

The C language provides a rich number of operators, including arithmetic operators, relational operators, logical operators, bitwise operators, conditional operators, assignment operators, comma operators, and other operators. Depending on the number of operands required by the operator, it can be divided into single-mesh operators, binocular operators, and three-mesh operators.

An expression is always composed of an operator and an operand, which specifies the process of the data object's operation.

1) Self-increment (+ +) and self-reduction (--)

2) The relational operator (= = = < <= > >=) result has a value of 1 or 0 and cannot be used to compare strings because the address of the string is compared, and there is no meaning.

3) logical operators (&& | | The result of the operation is 1 or 0.

4) Assignment operation and combinatorial assignment (=) are combined in order from right to left

5) The conditional operator and the comma operator (? :) (,)

6) Bitwise operator (~ & | ^ << >>) can be combined with bitwise operators to produce &= |= ^| <<= >>=

7) sizeof: Calculates the number of bytes of an expression or data type

8) Type conversion: Automatic type conversion, casting

9.1.3 Input/Output

The input/output operations in the C program are performed by input/output standard library functions (declared in header file stdio.h), common formatted output functions printf and formatted input functions scanf, and file manipulation functions fopen,fprintf and fscanf, etc.

1) printf

2) scanf: Commonly used are getchar, Putchar, Get (gets_s), puts

9.2 Control Statements

A statement is a basic unit in a program that uses statements to describe operations and controls during programming. The basic Process Control structure has sequence, branch (select) and loop 3 kinds, C language flow control statements have if, switch, for, while, Do-while, break, continue, return, etc.

1 SELECT statement if switch

2 Loop statement while do...while for

3 break statement jumps out of switch, jumps out of the loop

4 Continue statement: Ends the current loop and executes the next loop

5 Return statement: function return value

9.3 Functions

A function is a function module that is used to accomplish a specific task. There are two functions, a standard function that has been defined and published with the compilation system, and is called a library function, such as printf, scanf, and so on, and the user defines it according to its own needs.

1 function definition: Includes function name, argument list, return type, and function body

2 function declaration: Return type function name (parameter list)

3 Function call: Function name (argument table)

4 Recursive functions

9.4 Hands

Simply put, the pointer is the address of the memory cell, it may be the address of the variable, the address of the array, or the entry address of the function, the variable that stores the address is called a pointer variable, referred to as a pointer. Pointers are the most powerful weapon in the C language, providing programmers with great programming flexibility.

9.4.1 pointer definition: Pointer object, object pointed to by pointer

The 1 null pointer is NULL, the global pointer variable is automatically initialized to null, and the initial value of the local pointer variable is random.

2 "&" and "*" address operators, indirect operators

3 Pointer to heap memory: Malloc,calloc,realloc for memory allocation, free function release into memory, sizeof, memory leak

9.4.2 Pointers and Arrays

1 accessing array elements through pointers: array names are the first addresses of arrays in memory, ptr++ points to the next element, int a[m][n] A[I][J] address A + (I x N) + j * sizeof (int) (&a[0][0] + i x n + j)

2 accessing string constants through pointers: You can use STR as an array to access

3 Array of pointers: The elements of an array are pointer int **arr2

4 pointer arithmetic: pointer variable plus or minus an integer, moving distance is related to the size of the storage space occupied by the variable

5 constant pointer and pointer variable: a constant pointer is a constant that refers to the object that the pointer is pointing to, that is, the pointer variable can be modified, but cannot be modified by a pointer to the object it points to: const int *p = &d; pointer constant is a constant of the pointer itself and cannot point to another object: int *const p = & Amp;d; The key to differentiate between constant pointers and pointer constants is the position of "*", or constant if Const is to the left of "*" If const is the pointer constant on the right side of "*"

9.4.3 Pointers and functions

Pointers can be parameters or return values for a function

1 pointers as arguments to functions: use pointers as arguments to function to change the value of argument variables in the calling function by pointers when making function calls

2 pointer as function return value: Data type * Function name (parameter list)

3 function pointer: You can save the function address in a function pointer variable, and then use that pointer to indirectly call the function int (*compare) (const char*, const char*) compare=&strcmp can also be initialized at the time of definition

The following 3 calls are equivalent:

strcmp ("Tom", "Tim");

(*compare) ("Tom", "Tim");

(Compare) ("Tom", "Tim");

9.4.4 pointer and linked list (slightly)

9.5 Common C Program errors

1 The case of identifiers differs: C language is case-sensitive by default

2 ignoring the type of the variable causes the operation to be illegal: for example, floating-point values

3 character constants are confused with string constants: "" "Not divided

4 Referencing uninitialized variables: The values of uninitialized variables are random, and using these data can have unpredictable consequences.

5 "=" and "= =" are assigned and equal judgments are not divided

6 fewer semicolons or multiple semicolons

7 scanf and printf function use problem: scanf function less &

Missing break in 8 switch statement

9 Data Overflow

10 fixed-length arrays and variable-length arrays: variable-length arrays can use dynamic memory-request methods and hold the address of memory space with pointers: ptr = (int*) malloc (n*sizeof (int))

11 Array Subscript Valid range: Array out of bounds

12 confusing array names with pointer variables: array names are constants and cannot be modified

13 Frequently Asked Questions: memory leaks, null pointers, wild pointers: Wild pointers are those memory pointers that have been freed, so immediately set the corresponding pointer to a null value after releasing the memory, and return a pointer to the temporary variable: parameters in the function and non-static local variables allocate storage space in the stack, are temporary, Pointers to these temporary variables cannot be returned to the caller, otherwise they will have unpredictable consequences.

Programmer's Tutorial-Chapter 9-C programming

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.