The basic small problem in C language introduce _c language in detail

Source: Internet
Author: User
Tags arithmetic fread function definition function prototype lowercase uppercase letter
1, printf format output function
If the format control description number is more than the output table column number, then the error data is output;
If the number of output table columns is more than the format control description, the number is not output.
%md,m refers to the width of the output field. If the number of output fields is less than M, the left is padded with spaces, and if greater than M, the actual number is output.
%-MD, the basic ibid, except that the space in the right side of the
printf parameters can be constants, variables or expressions, VC + + 6.0 is evaluated in Right-to-left order, output from left to right as
Copy Code code as follows:

int x = 5;
printf ("%4d%4d%4d", X, ++x, ++x);

The output is 7,7,6. And not 5,6,7.
Note that different compilers may output different results and compile the results directly with GCC as 7,7,7
2, 0-9 digits to character
Number is M, then m+ ' 0 ' is the character form ' m ' of M
3, lowercase letters into capital letters
char c; c is lowercase, then C ' a ' + ' a ' is the corresponding uppercase letter
4. Switch
If a matching case entry is found, the following statement is executed, and after the execution of the statement, it does not exit like an if statement, and if no break statement is encountered, all subsequent case statements are executed, and the conditional judgment is no longer performed.
The statement at the back of the case entry can be either a sentence or a multiple sentence, and no curly braces are required.
5, character array storage string
When Char str[5]=new {"S"}, the program will go wrong and the output will be garbled in the back of the post because there is a ' yes ' after the ' string ', so you should give the STR array a bit more. That is, char str[6]=new {"A"};
and ' I ' only represent the end of the string and will not output.
scanf ("%s", str); You cannot save a space because the space is considered to represent the end of the string. Gets (str); You can add spaces in the middle of a string.
Puts (str); Automatically add line wrapping after the output string
6. String manipulation function
String copy function: Strcy (STR1,STR2); Copies the string str2 to the str1.
string concatenation function: strcat (STR1,STR2); The str2 is connected to the last character (not ' str1 ') with the word "," and the result is placed in str1.
string comparison function: strcmp (STR1,STR2); Compares the size of the STR1 and str2, returns 0 if STR1==STR2, or returns a positive integer if STR1&GT;STR2, or a negative integer if str1<str2.
String length function: strlen (str); Returns the actual length of the string str, excluding the end of ' the '.
7, the function of the parameters and one-way value transfer
The parameters of a function are divided into arguments and formal arguments. The formal parameter appears in the function definition and is used throughout the function body, leaving the function body unused. Arguments appear in the keynote function, and arguments cannot be used when they enter the called function.
The formal parameter is only called when the system allocates the memory unit, at the end of the call, the compiling system releases the allocated memory unit, so the formal parameter is valid only within the function, then the function call is returned to the main function and can no longer be used;
One-way transfer value: Only the value of the argument can be passed to the formal parameters, can not be the value of the parameters of the inverse pass to the argument, called one-way value transfer.
Therefore, the value of the formal parameter is changed during the function call, and the value of the argument does not change
8. Array as parameter
The array name can be used as a function argument, when the parameter can be an array or a pointer. And the formal parameter is a one-dimensional array, you can specify no length. When a formal parameter is a two-dimensional array, the first dimension can be omitted and the size of the second dimension is specified.
9, the variable storage mode
(1) Local variables
Local variables in each function call, the system will be in the memory of the dynamic storage area for them to reallocate the memory unit, as the function of the frequent invocation, the storage location of a variable will change as the program runs, so the value of the unassigned local variables is indeterminate. A local variable in a function cannot be a return value because the local variable is reclaimed after the function has finished.
(2) Static type
Static variables are allocated memory at compile time, the initial value is assigned, and the initial value is only assigned once. Static variables that do not have an initial value, the system automatically assigns an initial value of 0 (or ' 0 '). Static variables occupy a fixed memory unit in the memory static storage area. Even if the function call at which it is located does not release the storage cell, its value will continue to be retained and the value will continue to be used the next time it is called. Static variables are divided into static local variables and static global variables, static global variables are defined in the function of static variables outside the body, static local variables are defined in the body of the static variables.
As follows:
Copy Code code as follows:

#include <stdio.h>
void F ()
{
static int a=0; The initial value is assigned at compile time, and the whole process is assigned an initial value only once
++a;
printf ("%-2d", a);
}
Main ()
{
f ();
f ();
f ();
}

The output of the above program is 1 2 3
Because assigning an initial value to a static variable is done at compile time, and only once, after the call function does not perform an initial operation, so output 1 2 3; If you remove the static keyword, the result becomes 1 1 1; From this, the function is called repeatedly, and the local variable is assigned an initial value each time, The static variable is only assigned the initial value at the first time it is invoked.
Special note here: There are no static local variables in Java, there will only be static global variables for the class.
(3) Register type
Definition form: Register data type variable name; Register type local variable has the same scope, lifetime, and local variable.
The number of registers is limited, the number of storage data of registers is limited, so the variable of register type cannot be too many, and there are integer variables and character variables to be defined as local variables of register type. Now the optimization system can automatically judge the relevant variables in the register.
(4) External variables
10. Compile preprocessing
The difference between #include < file name > and #include "filename" is that when you use angle brackets, the compilation preprocessor is only looking in the system-specified folder, and with double quotes, the compiler first looks in the folder where the current file is located, and if you can't find it, is found in the system-specified folder.
11, & and *
Priority is the second level, and right-to-left operations
12, * and + + 、--operator
Belong to the second level, computed from right to left
*p++ equivalent to * (p++)
*++p equivalent to * (++P)
13, [] and *
[] priority is higher than *
13, two-dimensional array of row and column address
int a[2][2]={1,2,3,4};
The first header address, *a and a[0] are the addresses of the first element of the first line.
A+1 is the second row address
* (A+1) is the first element address of the second line, and A[1] is also the first element address of the second line
14, pointer to the array of variables (array pointers)
int * (P) [4]; Represents a pointer to an array containing 4 int elements.
(1) P points to the first row address of a one-dimensional array
Copy Code code as follows:

Main ()
{
int a[2]={1,2};
int (*p) [2];
P=&a;//p is a pointer to an array, a cursor, so you assign a value to it using &a, &a the address
printf ("%d\n", **p);//Take the first element, where p is the row address, *p is the element address, **p is the element value
printf ("%d\n", * (*p+1));//Take the second element
You can also take the following values
printf ("%d\n", (*p) [0]);//(*P) can replace a
printf ("%d\n", (*p) [1]);
}

(2) P points to the first row address of a two-dimensional array
Copy Code code as follows:

Main ()
{
int a[2][2]={1,2,3,4};
int (*p) [2];
P=a;//p point to the first row address of a two-dimensional array
printf ("%d\n", **p);//*p is the address of the element, **p is the element content
printf ("%d\n", * (* (p+1) +1));
}

15. Array of pointers
int *p[4]; indicates that an array contains 4 INT-type pointers.
Char *p[4]; indicates that an array contains 4 char arrays, or 4 strings
16, each address in a two-dimensional array
int a[2][3];
Then A is the first line address, *a, **a the first element address, the value
A+1 is the second row address, * (A+1) is the address of the first element of the second line, * * (A+1) is the value of the first element in the second line
* (a+1) and a[1] equivalence: All represent the address of the first element in the second line
Example:
Copy Code code as follows:

Main ()
{
int a[2][2]={1,2,3,4};
printf ("a=%d\n", a);
printf ("*a=%d\n", *a);
printf ("**a=%d\n", **a);
printf ("*a+1=%d\n", *a+1);
printf ("a+1=%d\n", a+1);
printf ("a[1]=%d\n", a[1]);
printf ("a[1]+1=%d\n", a[1]+1);
printf ("*a[1]=%d\n", *a[1]);
}

17, character pointer variable to accept the input string, you must first open up storage space
Char *CP;
scanf ("%s", CP);
The above is wrong.
Can be changed to:
Char cp[20];
scanf ("%s", CP);
Or
Char *cp,str[20];
CP=STR;
scanf ("%s", CP);
In short, we must first open up space, and then accept the string
The return value in 18, C, and C + + cannot be an array, and the Java return value can be an array
19, pointer functions and function pointers
pointer function: int * function ();
function pointers:
Copy Code code as follows:

Int (*p) ();
int max (int a,int b);
P=max;
int a= (*P) (2,3);

20, about the life cycle of variables
A local variable defined in a function cannot be returned as a value because the local variable is reclaimed after the function is finished.
21. Structural Body
A structure can be nested within a struct, but it cannot be the body itself. And the definition of member structures must precede the main structure body.
22, malloc
The malloc function is located in Stdlib.h, and the function prototype is void * malloc (unsigned size);
Eg:struct student *p= (struct student *) malloc (sizeof (struct student));
Because malloc returns a pointer of type void, it is forced to cast.
23, free
The function prototype is
void free (void * ptr)
Frees the dynamically allocated memory space that has pointer ptr pointing. In order to ensure the efficient use of the dynamic storage area, when a storage space is no longer used, it should be released in time.

24. Structure and common body
(1) Structural body
The struct body can be used as a function parameter and return value.
A struct can only be assigned a value directly in the form of a brace {} when it is initialized, and the first declaration, when assigned, can only be assigned to a single element, no longer in curly braces. This is similar to the array assignment. Examples are as follows
Copy Code code as follows:

struct student
{
int BH;
Char *name;
};
struct student stu={1, ' Typ '};//is right.

But here's the wrong one.
Copy Code code as follows:

struct student Stu;
Stu={1, "Typ"};//is wrong.

At this point, this can Stu.bh=1;stu.name= "Typ."
(2) Common body
The shared body cannot be used as a function parameter and return value
A shared body cannot be stored at the same time, and only one member can be stored at a time, with the last member being a valid member. The size of the common body is the size occupied by the maximum element;
A common body can appear in a struct type, whereas a struct can also appear in the type of a common body.
25. Enumeration type
Copy Code code as follows:

Enum color {red,green,blue};
Enum color c=red;
int i=red;//value is 0

26. redefinition of type identifiers
The C language uses a keyword typedef to declare a new type name
typedef int INTEGER;
INTEGER X,y;
Equivalent to
int x, y;
Another example is the definition of a structural body:
Copy Code code as follows:

typedef struct
{
int num;
Char name[10];
Float score;
}student;
Student stu1, STU2, *s;

In addition, the typedef is just a type redefinition, just naming an alias for the type and not creating a new data type
27, bit operation
Include (with, or, XOR, or reverse).
Where the bitwise operator is operating, the number is in the complement form, and the symbol bit participates in the operation.
XOR: Same as 0, 1 different
A^a=0;a^0=a;a^~a=1;
Here you can use a different or to achieve the exchange of two numbers
A=a^b;
B=b^a;
A=a^b;
This avoids the introduction of temporary variables
28. Shift Operation
(1) A<<b, which means that the binary value of a is shifted to the left B bit
(2) A>>b, which means that the binary value of a is shifted to the right B bit
The shift operation is implemented in 3 different forms:
(1) Cyclic shift: a bit equal to the moved Bits
(2) Logical shift: The removed bit is lost, and the bit taken is 0.
(3) Arithmetic shift: The removed bit is lost, the left-moved bit is 0, the right-to-move bit sign bit, the symbol bit remains unchanged
C-language shift operations are related to specific C-compiler systems, such as vc++6.0 using arithmetic shifts
Note: The shift operation does not change the value of the original operand. For example, after the a>>2 operation, the value of a remains the same unless the value of a is changed by the assignment a=a>>2.

29, documents
(1) The file in C language is a byte stream file.
(2) The user-defined file type in C is a file,file file type, which is a structure defined by the keyword typedef.
Copy Code code as follows:

struct _IOBUF
{
char * _PTR;
int _cnt;
Char *base;
int _flag;
int _file;
.........
};
typedef struct _IOBUF FILE;

(3) File open and close
File pointer = fopen ("File path \ file name", "File operation mode");
Operation mode is divided into r,w,a,r+,w+,a+
Returns null if the fopen open fails

If the buffer is not full 512B, it will not be written to disk and if the program terminates abnormally, the data in the buffer is lost, causing the file to be incomplete. You can only force data that is less than 512B in the buffer to be written to a disk file in order to ensure the integrity of the file, only when a shutdown is done on the open file. fclose function to close a file
fclose (file pointer);
The return value is an integer value, which, if 0, indicates that the file will not shut down properly if it is closed properly.
(4) file input and output
Read and write one character: char fgetc (file pointer); EOF FPUTC (character, file pointer)
Read and write a string: fgets (string s, read the number of characters N, file pointers)---> in the middle of the encounter \ or EOF stop, read n-1 characters, at the end of the ""; fputs (string, file pointer)---> string end tag is not written to the file
Formatted read and write: fscanf (FP, "%d%s", &i, s)---> read data from files to variables; fprintf (FP,%d%c, J, c)---> Write data to a file in a specified format
Block reading and writing: Fread (BUFFER,SIZE,COUNT,FP) and fwrite (BUFFER,SIZE,COUNT,FP)
Buffer is a pointer to the first address of the variable that holds "input data" in the Fread (), and the first address of the variable that holds the "output data" in fwrite ().
Size represents the number of bytes in a block of data
Count represents the number of blocks of data
FP File pointers
The return value is both the Count value
(4) Functions for other file operations
Feof (FP) determines the end of a file, returns 1 at the end, or 0
Rewind (FP) is used to locate the file where the pointer returns to the beginning of the file.
Fseek (FP, offset, base) is used to control the position pointer movement within the file. Base is the datum point for position movement. Offset is offsets
Ftell (FP) is used to get the position of the position pointer, relative to the beginning of the file.

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.