Unofficially, 80% of Android app developers have little or no chance to write C/C++. If you’re an aspiring programmer, pick up C.

The source code

All source code involved in this article: github.com/gavinliu/St…

The data type

C language is a typed language, using variables must be defined to determine the type.

  • Integer: char, short, int, long, long long, bool
  • Floating point numbers: float, double, long double
  • Pointer to the
  • Custom type

Different data types

On 64-bit machines:

The data type The length of the memory formatting
char 1 Byte %d
short 2 Byte %d
int 4 Byte %d
long 8 Byte %ld
float 4 Byte %f
double 8 Byte %lf

Both integers and floating-point numbers exist in memory as binary numbers, but integers are real binary numbers that can be computed directly, whereas floating-point numbers are coded and cannot be computed directly. Typically, the CPU has a dedicated piece of hardware to handle floating-point arithmetic.

sizeof

This method returns the number of bytes that a type or variable occupies in memory

sizeof(int);
int a = 0;
sizeof(a);
Copy the code

The integer

The special meaning of int

The length of int and long depends on the compiler and CPU, and is usually meant to represent the length of “a word”.

What is a word?

CPU and memory read data model:

CPU [register] < — bus — > RAM

The registers in the CPU read and write data through the bus and memory. The size of the registers in the CPU is usually 32bit or 64bit, which is the length of a word, usually representing the length of an int.

Internal expression

Any data in a computer is binary data, which depends on how we look at it.

The range of numbers that can be expressed in a byte:

00000000 ~ 11111111 (0 ~ 255)

Since char just happens to be a byte, let’s experiment with char:

Since a single byte can contain only 256 digits, a char can be used to represent numbers ranging from 0 to 255 or can be used to represent negative numbers.

char c = -1;
printf(c="%d\n", c);Copy the code
c=-1Copy the code

The test code shows that char can represent negative numbers.

So what about negative numbers? What’s the binary equivalent?

1 – > 00000001 0 – > 00000000-1 – >?

Let’s think of it this way: -1 + 1 = 0, then:

?????????? + 00000001 = 00000000Copy the code

This makes it obvious:

  11111111
+ 00000001
=100000000
= 00000000
Copy the code

Because 100000000 is out of the 1-byte range, the overflow becomes 00000000.

What is the relationship between 11111111 and 00000001?

Source code: 00000001 Reverse code: 11111110 Complement code: 11111111

So although the data in memory is 11111111, it is 255 when viewed as source code, and -1 when viewed as complement.

signed & unsigned

The signed, unsigned data type modifiers can only apply to data of the type integer, indicating whether or not the numbers are signed.

Base 8 base 16

int i = 012; // octal int j = 0x12; Printf (" I =0%o,j=%x\n", I,j);Copy the code

Floating point Numbers

The precision of error

Float f = 1.123 f; Float ff = 2.123 f; float sum = f + ff; If (sum == 3.246f) {printf(" equal \n"); } else {printf(" not equal sum=%.10f, sum=%f\n", sum, sum); }Copy the code
Unequal sum=3.2459998131, sum=3.246000Copy the code

Floating point numbers are not accurate, the significant number is limited, so in dealing with the problem of money, do not use the decimal point to represent the Angle minutes, but converted to the smallest unit to calculate, such as: 1.23 yuan = 123 minutes, so converted to the integer calculation will not error.

Pointer to the

What are Pointers? A pointer is a memory address that represents a block of memory.

Int I = 5; Printf (" I's address %p\n",&i); Int * p; int* p; P = &i; p = &i; p = &i; printf("i=%d\n",i); Printf ("*p = %d\n",*p); // *p = %d\n;Copy the code

How many bytes is a pointer

int i =3; Double d = 3.141692; Float f = 3.1423; char c ='B'; int* ip = &i; double* dp = &d; float* fp = &f; char* cp = &c; Printf ("int pointer variable of length %d\n",sizeof(IP)); // --> 4 printf("double: %d\n",sizeof(dp)); // --> 4 printf("float pointer variable with length %d\n",sizeof(fp)); // --> 4 printf("char pointer variable with length %d\n",sizeof(cp)); / / -- > 4Copy the code

An array of

Array variables are special Pointers

  • You don’t need to fetch the address with ampersand

    int a[10]; int *p = a;Copy the code
  • But array units represent variables, so ampersand is used to fetch the address

    *a == &a[0]Copy the code
  • [] Can be done with arrays or Pointers.

    p[0]; a[0];Copy the code
  • * You can also do it with arrays

    *a = 0;Copy the code
  • An array variable is a pointer to const, so it cannot be reassigned, only initialized.

    int b[] = a;
    // int b[] === int * const b
    Copy the code

Pointer arithmetic

Char * pChar = &arr[2]; char* pChar = &arr[2]; Printf ("pChar memory address: %#X, for the value: %c\n", pChar, *(pChar)); Printf ("pChar + 1 memory address: %#X, for the value: %c\n", pChar + 1, *(pChar + 1)); // pChar + 1:0X28FF23; // pChar + 1:0X28FF23; // pChar + 1:0X28FF23;Copy the code
printf("dist=%c\n", &arr[0] - &arr[2]); Dist =2 // pointer subtraction returns distance, not integer subtractionCopy the code

Pointer type conversion

int* p = &i;
void* q = (void*) p;
Copy the code

A function pointer

void print(char const * str) {
    printf("%s\n", str);
}
void sayHi(void (* p)(char const * str)) {
    p("hi2");
}
sayHi(print);Copy the code

Use of Pointers

  1. Used as an argument when larger data needs to be passed: pass an array
  2. Operations on arrays
  3. When a function returns more than one result
  4. You need functions to modify more than one variable: swap
  5. Dynamic Memory Application
  6. Function Pointers are equivalent to Java callbacks

Dynamic memory allocation

Int * p = (int*) malloc(n * sizeof(int)); / / recycling free (p);Copy the code

string

Three ways to declare a string

char c[] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("%s\n", c); char c2[6] = {'H', 'e', 'l', 'l', 'o'}; printf("%s\n", c2); char* str = "Hello World!" ; printf("%s\n", str);Copy the code

String array

char s[][] = {"Hello", "Wrold"};
char *ss[] = {"Hello", "Wrold"};
Copy the code

String manipulation

#include <string.h>Copy the code

There are libraries for handling strings in the standard string.h library.

The specific method to find the: www.runoob.com/cprogrammin…

Structure type

The enumeration

enum Type {
    Open,
    Close
};
enum Type t = Open;
Copy the code

The structure of the body

struct Position {
    int x;
    int y;
};
Copy the code

The memory size of a structure is the sum of the sizes of all its members

Structure initialization

struct Position p = {1, 2};
struct Position q;
q.x = 2;
q.y = 2;Copy the code

Structure pointer

struct Position* p = &q;
(*p).x = 1;
p->y = 2;Copy the code

The C -> operator is shorthand for the value of a pointer.

Structure operation

p = (struct Position) {1, 2}; // equivalent to p.x = 1; p.y = 2; P1 = p2 // equivalent to p1.x = p2.x; p1.y = p2.yCopy the code

A consortium

union Value {
    int x;
    float y;
    char z;
    double q;
};
union Value v;
v.x = 1;
printf("%d, %f, %c, %f\n", v.x, v.y, v.z, v.q);
v.z = 'A';
printf("%d, %f, %c, %f\n", v.x, v.y, v.z, v.q);
v.y = 0.1;
printf("%d, %f, %c, %f\n", v.x, v.y, v.z, v.q);
v.q = 0.2;
printf("%d, %f, %c, %f\n", v.x, v.y, v.z, v.q);
printf("%zu\n", sizeof(union Value));
Copy the code

The size of the union, there will be memory alignment, its memory size is equal to the length of the largest attribute in the member, and because the members of the union share memory, only one value will be safely stored.

1, 0.000000,, 0.000000 65, 0.000000, A, 0.000000-1717986918, � 0.100,000-1717986918, � 0.100,000-1717986918, � 0.100,000-1717986918, � 0.200000 8Copy the code

The alias

ttypedef int Age;
typedef int* AgeP;
struct Man {
    char* name;
};
typedef struct Man Student, *StudentP;
Copy the code
Age a = 1;
printf("%d\n", a);
AgeP p = &a;
printf("%d\n", *p);
Student student;
student.name = "Jack";
printf("%s\n", student.name);
StudentP sp = &student;
printf("%s\n", sp->name);Copy the code
1
1
Jack
Jack
Copy the code

Aliasing a structure makes its use more Java like.

Macro definition

#define valueCopy the code

The following is the standard C header macro definition, in order to solve the header import loop

#ifndef __X__HEADER__
#define __X__HEADER__
void hello();
#endifCopy the code

Prior to C99, C didn’t have the const keyword, so defining constants was often done with macros

# define PI (3.14159)Copy the code

The compiler handles macro definitions as primitive text replacements.

double x = 2 * PI;Copy the code

= = >

Double x = 2 * 3.14159;Copy the code

Macros with arguments

#define cube(x) ((x)*(x)*(x)) printf("%d\n", cube(2)); / / 8Copy the code