C and pointers (1)


preprocessing directives

The preprocessor replaces the corresponding #includeinstruction statements with the contents of the library function header file.
Using stdio.hheader files allows us to access functions in the standard I/O library.
Another type of preprocessing directive is #define. When this name appears anywhere in the source file later, it will be replaced by the defined value.

 int read_column_numbers ( int columns [ ] , int max );
 void rearrange ( char *output, char const *input , int n_columns, int const columns[] );

These declarations are called function prototypes . They tell the compiler the characteristics of functions that will be defined in the source file later, so that when these functions are called, the compiler can check them for accuracy. Each prototype begins with a type name, indicating the type of the function's return value. Following the return type is the name of the function, followed by the parameters the function is expected to accept. Therefore, the function read_column_numbersreturns an integer and accepts two types of parameters: an integer array and an integer scalar.
rearrangeThe function accepts 4 parameters, the first and second parameters are pointers . A pointer specifies the address of a value stored in computer memory. The 2nd and 4th parameters are declared as const, which means that the function will not modify the two parameters passed by the function caller. The keyword voidindicates that the function does not return parameters. In other languages, this non-returning function is called a procedure .

main function

int main( void )
main is the starting point of program execution.
In C language, array parameters are passed by reference, that is, call by address, while scalars and constants are passed by value.
All parameters passed to functions are passed by value.
The biggest expense of software today is not writing it, but maintaining it.
Commonly used printf format codes:

Format meaning
%d Print an integer value in decimal form
%o Print an integer value in octal form
%s Print an integer value in hexadecimal form
%g print a floating point value
%c print a character
%s print a string

scanfAll scalar parameters in the function must be preceded by an "&" symbol. Array parameters do not need to be preceded by an "&" symbol. However, if a subscript reference is added to the array parameter, it means that the actual parameter is a certain parameter in the array. A specific element must also be preceded by the "&" symbol.

  scanf ( "%d", &columns[num] )  

Commonly used scanf format codes:

Format meaning
%d read an integer value int
%ld Read a long integer value long
%f Read a real value (floating point number) float
%lf Read a double precision real value double
%c read a character char
%s Read a string char array from the input

&&It is a "logical AND" operator. When the expressions on both sides of the && operator are true, the entire expression is true. If the expression on the left is false, the expression on the right will no longer be evaluated.

=Assignment operations are not comparison operations.

The return statement is a function that returns a value to the expression that calls it.

C语言的for语句:for ( col = 0; col , n_columns; col += 2) {
    
    
                        初始部分,    测试部分,  调整部分

Execution: The program must be loaded into memory. When program code is executed, a runtime stack is used, which is used to store local variables and return addresses of functions. Programs can also use static memory, and variables stored in static memory will retain their values ​​throughout the execution of the program.
Basic data types: The integer family includes characters, short integers, integers and long integers, divided into signed and unsigned.
The rules for the size of integer values ​​relative to each other are: long is at least as long as integer, and integer is at least as long as short.

Format meaning
char 0 to 127
signed char -127 to 127
unsigned char 0 to 256
short int -32767 to 32767
unsigned short int 0 to 65535
int -32767 to 32767
unsigned int 0 to 65535
long int -2147483647 to 2147483647
unsigned long int 0 to 4294967295
    short int 至少16位,long int 至少32位。 

Constants and variables

integer literal

Literal value is the abbreviation of literal constant, which specifies its own value and does not allow changes. The difference between a named constant (a variable declared as const) and an ordinary variable is that its value cannot be changed after it is initialized. Adding the character or
after the integer literal value causes the integer to be interpreted as . The character or is used to specify the numerical value as . If added after a literal value , it is interpreted as . Hexadecimal starts with 0x and octal starts with 0. Character constant int type. Is a single character (or character escape sequence or three-letter word) enclosed in single quotes, such as: 'M' '\n' '\377' Floating-point numerical literal values ​​are of type double by default, unless It is followed by an L or l to indicate a long double value, or by an F or f to indicate a float value.Lllong整数值Uuunsigned整型值ULunsigned long整型值


pointer:

Variables are stored in the computer's memory, and each variable occupies a specific location. Each memory location is uniquely identified and referenced by an address.
A string constant is a string of characters surrounded by a pair of double quotes, "Hello" "\aWarning!\a" "" Even an empty string has a NUL symbol as a terminator.

Basic statement:

The basic form of variable declaration is: Specifier (one or more) Declaration expression list The
specifier contains keywords used to describe the basic type of the declared identifier. Specifiers can also be used to change the default storage type and scope of an identifier.

  Int I; char j, l, l;

Initialization is the variable name followed by an equal sign (assignment sign), followed by the value you want to assign to the variable. Int j = 15;
Declare a simple array int values[ 20 ] ; Declare an integer array, the array contains 20 integer elements. The subscript of the array starts from 0, and the subscript of the last element is the number of elements minus 1. Declare the pointer
int a; This statement indicates that the result type produced by the expression a is int. Knowing that the * operator performs indirection, we can deduce that a is a pointer to an int.

[Indirect access operations are legal only for pointer variables. The pointer points to the result value. Indirect access operations on pointers can obtain this resulting value.

int* b, c, d; The error asterisk is actually part of the expression *b and is only useful for this identifier. b is a pointer, but the remaining two variables are just plain integers. To declare three pointers, the correct statement is as follows: int *b, *c, *d;

When declaring a pointer variable, you can also specify an initial value for it. Char *message = "Hello world!"; This statement declares message as a pointer to a character, and is used to initialize the pointer with the address of the first character in the string constant. The previous statement is equivalent to:
char *message; message = "Hello world!";

Implicit declaration:

There are several declarations in C language whose type names can be omitted. If a function does not explicitly declare the type of the return value, it returns an integer by default.
typedef: Allows new names to be defined for various data types.
char *ptr ; Declare the variable ptr as a pointer to a character. => typedef char *ptr ; Use the identifier ptr as the new name for the type of pointer to character. You can use this new name ptr a in the following declaration; declare a to be a pointer to a character.
should be used typedefinstead of #defineto create new type names. Because the latter cannot handle pointer types correctly.

            #define d_ptr_to_char char * d_ptr_to_char a ,b ;

a is correctly declared, but b is declared as a character. When defining complex type names, such as function pointers or pointers to arrays, it is more appropriate to use typedef.

constant:

        常量和变量完全一样,只是它们的值不能被修改。 如: int const  a;  const int  a; 都可以赋值  int const a = 15;  

Guess you like

Origin blog.csdn.net/qq_44710568/article/details/132298335