Basic data type

The data type Take up the size Value range
char 1 byte -128 to 127 or 0 to 255
Int the integer 2 or 4 bytes (system bit dependent) -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
Short short integer 2 – 32768 to 32767
Long long integer 4 bytes 2147483648 to 2147483647
Unsigned int Unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
Float Single-precision floating point type 4 bytes 1.2 e-38 to 3.4 e
Double floating-point type 8 bytes E+308 e-308 2.3 to 1.7

Address & Pointer

  • & means take the address of the variable
#include <stdio.h> // Import the C header file
 
int main (a)
{
    int num = 10;
    int *p;              // Define a pointer variable
    p = &num;
 
   printf("Address of num variable: %p\n", p);
   return 0; } output: address of num variable:0x7ffeeaae08d8
Copy the code
  • Variables decorated with an asterisk (*) represent pointer variables that point to the address of the variable
#include <stdio.h>
 
int main (a)
{
   int  var = 20;   /* Actual variable declaration */
   int  *ip;        /* Pointer variable declaration */
   ip = &var;  /* Store the address of var in pointer variables */
   printf("Var variable address: %p\n", &var  );
   /* Address stored in a pointer variable */
   printf("IP variable store address: %p\n", ip );
   /* Use Pointers to access values */
   printf("* IP variable value: %d\n", *ip );
 
   return 0; } output: var variable address:0x7ffeeef168d8The IP variable stores the address:0x7ffeeef168d8* The value of the IP variable:20
Copy the code
  • Multilevel pointer

A multilevel pointer is a pointer to a pointer that takes an address from an address

#include <stdio.h>

int main(void)
{
    int i = 10;
    int *p = &i;
    // Secondary pointer
    int **q = &p;
    // A three-level pointer
    int ***r = &q;
    printf("P address %p \n" ,p );
    printf("Q address %p \n" ,q );
    printf("R address %p \n" ,r );
    printf("i = %d\n", ***r);
    return 0; } The output is: p address0x7fff5feef944Q address0x7fff5feef938R address0x7fff5feef930 
 i = 10
Copy the code
variable value address
i 10 0x7fff5feef944
p 0x7fff5feef944 0x7fff5feef930
r 0x7fff5feef930 &r

Function pointer & callback function

Function pointer definition: return value (* function name)(argument…) , such as int (* p) (int, int)

#include <stdio.h>

int max(int x , int y ){
    returnx>y? x:y; }int main(void) { 
	printf("Function pointer");
	int a,b,c,d;
	// Define a pointer to a function named p
	int(*p)(int.int) =&max;
	printf("Please enter three numbers :");
	scanf("%d %d %d ",&a,&b,&c);
	d = p(p(a, b), c); 
	printf("The largest number is: %d\n", d);
	return 0; } input:1 2 3Output:3
Copy the code

Callback function: Using a function as an argument, a callback function is a function called through a function pointer. In simple terms, a callback is a function that you implement when someone else’s function executes it.

#include <stdio.h>

int max(int x , int y, int(*action)(int.int)){
    return action( x,y);
}

int callback_method(int x , int y){
    printf("Call callback function, return maximum \n");
   returnx>y? x:y; }int main(void) { 
	int a,b,c;
	// Function pointer
	printf("Please enter two numbers :\n");
	scanf("%d %d ",&a,&b);
	c = max(a,b,callback_method);
	printf("Max: %d\n", c);
	return 0; } Please enter two numbers:4 2Call the callback function, return the maximum value4
Copy the code

Structure struct

  • The structure of the body

An array can store variables of the same type of data items. A structure can store data items of different types.

// Note: Tag member-list variable-list must be at least two
struct tag { 
    member-list
    member-list 
    member-list. } variable-list ;
Copy the code

example

#include <stdio.h>
#include <string.h>
// Define a structure with the tag Books
struct Books {
    char author[50];
    char title[50];
    int book_id ;

};


int main(void) { 

  struct Books book1;
  struct Books book2;
  struct Books *p_book1;
  p_book1 =&book1;
  strcpy(book1.title,"An Advanced Guide to Android");
  strcpy(book2.title,book1.title);
  strcpy(p_book1->title,"C/C++ Advanced Guide");
  printf("Book1 address = %p \n" , &book1);
  printf("Pointer structure p_book1 address = %p \n" , p_book1);
  printf("Book2 address = %p \n" , &book2);
  printf("Pointer structure p_book1 title = %s \n" , p_book1->title);
  printf("book1 title = %s \n" , book1.title);
  printf("book2 title = %s \n" , book2.title);
  return 0; } Output: book1 address =0x7fffcfdf38d0Pointer structure p_book1 address =0x7fffcfdf38d0Book2 address =0x7fffcfdf3860P_book1 title = C/C++ advanced guide book1 title = C/C++ advanced guide book2 title = Android Advanced guideCopy the code

File operations

Open file operation

//filename indicates the filename, and mode indicates the mode
FILE *fopen( const char * filename, const char * mode );
Copy the code

Open file mode:

model describe
r Open an existing text file, allowing the file to be read.
w Open a text file to allow writing to the file. If the file does not exist, a new file is created. Here, your program writes from the beginning of the file. If the file exists, it is truncated to zero and rewritten.
a Open a text file and write to the file in append mode. If the file does not exist, a new file is created.
r+ Open a text file, allowing reading and writing files.
w+ Open a text file, allowing reading and writing files. If the file already exists, it is truncated to zero length, and if it does not exist, a new file is created.
a+ Open a text file, allowing reading and writing files. If the file does not exist, a new file is created. Reads start at the beginning of the file, and writes can only be in append mode.

Close the file

 int fclose( FILE *fp );
Copy the code

Written to the file

// The character value of the argument c is written to the output stream pointed to by fp. It returns the character written if the write was successful, and EOF if an error occurred.
int fputc( int c, FILE *fp );
// Writes the string s to the output stream pointed to by fp. It returns a non-negative value if the write was successful, or EOF if an error occurred.
int fputs( const char *s, FILE *fp );
Copy the code

Read the file

// Read a single character
int fgetc( FILE * fp );
// Read n-1 characters. It copies the read string into the buffer buf and terminates the string by appending a null character at the end.
char *fgets( char *buf, int n, FILE *fp );
Copy the code

example

#include <stdio.h>
 
int main(a)
{
   FILE *fp = NULL;
 
   fp = fopen("/tmp/test.txt"."w+");
   fprintf(fp, "This is testing for fprintf... \n");
   fputs("This is testing for fputs... \n", fp);
   fclose(fp);
}
Copy the code
#include <stdio.h>
 
int main(a)
{
   FILE *fp = NULL;
   char buff[255];
 
   fp = fopen("/tmp/test.txt"."r");
   fscanf(fp, "%s", buff);
   printf("1: %s\n", buff );
 
   fgets(buff, 255, (FILE*)fp);
   printf("2: %s\n", buff );
   
   fgets(buff, 255, (FILE*)fp);
   printf("3: %s\n", buff ); fclose(fp); } output: This2: is testing for fprintf.3: This is testing for fputs.// First, the fscanf() method only reads This because it encounters a space after it.
// Next, call fgets() to read the rest of the line until the end of the line. Finally, you call fgets() to read the second line in its entirety.
Copy the code