📚 Notes and personal understanding of the process of learning C language, used to find.
Introduction
Our daily life has become inseparable from computers. Whether you are using computers or not, you are using computers consciously or unconsciously, or using the services that computers provide for you. When we are in the use of computer, we are all in the use of computer has some software, so we will go to find the APP, if you searched all the APPs on the market, is there no have the functionality of the APP you want, then you have to write their own one, if you want to do something special, you can’t find the right software, will still have to write their own one.
Learning programming is not about writing software for yourself. It is about learning programming to understand how computers work, what they can or are good at doing, what they can’t or aren’t good at doing, and how computers solve problems.
—— Weng Kai
Get started
Framework
|
Any programs programed by C language must have this framework.
Output function
You can understand it as function in math, which is a mapping relationship. But they are different.
printf
is a function, whoes function is output a string by formating printf("......\n")
.
For example, the Hello, world!
:
|
printf
can print not only a string, but also the value of the variable, but you need to format the variable.
Variables and constants
The computer carries on the computation, then participates in the computation is the number, participates in the computation in the C language the number is called the quantity, the quantity divides into the variable and the constant.
Use decimal for expression of daily life, because is advantageous for the calculation of the human brain, a computer internal use binary, for convenience of computer calculation, and the computer expression, as a result of bytes in computer internal frequency is higher, if you can use a simple way to express its inner meaning accurately, Will bring us a lot of convenience, so often use hexadecimal expression.
But the number itself remains the same no matter which way it is counted.
Constants
As the name implies, an invariant quantity that, once initialized, cannot be changed.
Variables
As the name implies, a variable quantity that, once defined, can be assigned any value to change its size.
The way of difination:
int i; |
For example, i
is the variable itself, int
is an integer variable, whose value can only be an integer, while double
is a double-precision floating point number, which can represent a decimal.
Different variable types have different value types and value ranges.
Character variables:
Use to store character constants. A character variable can hold only one character constant. The type specifier is char
.
|
The result is:
boy |
The literal value of a character variable is independent of the character constant it holds, analogous to an integer variable.
Character variables can also store integer data, which is universal. You can change %c
to %d
during input and output.
Output and input of a variable
Output
As mentioned above, printf
can print a string, and can print the value of a variable, as shown in the following example:
|
Notice that printf prints the value of the variable with a %d
inside the double quotes, which is the way the variable is formatted.
%d
indicates that the output variable is an integer.
Variable Types | Formatting Symbols |
---|---|
int | %d |
unsigned | %u |
long long | %ld |
unsigned long long | %lu |
float | %f |
double | %lf |
You can use scientific notation when you output, and use %e
for formatting symbol.
printf("%.nf",sum); |
This line of code can preserve n decimal places.
The following are escape characters:
Symbols | Words | 中文含义 |
---|---|---|
\b | backspace | 回退一格 |
\t | tab | 下一个制表位 |
\n | new line | 换行 |
\r | return | 回车 |
\f | f | 换页 |
Input
Similarly, the function scanf
can read the input according to a certain format.
|
scanf()
stops reading a string as soon as it encounters a space, so “this is test” is three strings for scanf()
.
Floating point numbers
In mathematics, the numbers on the number line are continuous, and between any two different points, an infinite number can be found, but this is difficult to achieve in computers, so floating point numbers emerged.
Floating point numbers are used to represent fractional numbers between whole numbers, but their accuracy is not infinite, nor is their expressability infinite, so a random decimal may not be able to be expressed by a computer.
Operation
Operator
Mathematics | Add | Substract | Multiply | Divide | Remainder |
---|---|---|---|---|---|
C Language | + | - | * | / | % |
Relational operator
Relation | Equal | Not Equal | Greater | Greater or Equal | Less-than | Less-than or Equal |
---|---|---|---|---|---|---|
Operator | == | != | > | >= | < | <= |
The relational operator evaluates only zeros and ones.
Special operator
count ++
and ++ count
both mean to add one, but a = count ++;
means to assign the value of count
to a
and then add one, whereas a = ++count;
means to add one to the value of count
and then assign the result to a
. So you end up adding one to count
in both cases, but the value of a
differ by 1
.
count --
and -- count
in the same way.
,
is comma operator that generally has only one purpose: to add multiple conditions to an if
statement.
Conditional operator
count = (< |
It is equivalent to an if
statement.
Nesting is not recommended.
Logical operator
Logic | and | or | not |
---|---|---|---|
Symbol | && | || | ! |
The result of logical operation is only 0
or 1
.
Several statements
if
if (< |
To judge and to act when the conditions are true.
if (< |
We can add else
, so we can do something if the condition doesn’t work.
The else
always matches the nearest if
.
while
while (< |
The loop continues until the condition fails.
dowhile
do { |
The loop continues until the condition fails.
The difference with a while
loop is that a dowhile
does something and then evaluates the condition, whereas a while
evaluates the condition and then loops. While
might not do a loop at all, if the condition is not satisfied in the first place.
switch
switch (< |
switch
is judgment statement, the <#expression#>
is constant expression that must be a integral type or enum-type.
The essence of such a statement is the program evaluates this expression and then compares it to each case
at a time. The action after the case
is executed when equal.
There are an infinite number of cases, each followed by a value to be compared with and a colon.
The variables to be compared must be of the same type.
When all the case
is false, the program will do the action after default
. So there can be nothing after defalut
.
For example:
|
for
for (< |
for
loop applies to loops with a defined number of cycles, such as traverse.
There are three sections in parenthesis, separated with semicolons, which are respectively initialization, conditions for loop to proceed and actions to be performed in each cycle.
Miscellaneous
Key words | Implication |
---|---|
Inf |
Infinity |
-Inf |
Negative infinity |
nan |
Invalid number |
fabs(<#expression#>) |
Absolute value |
break |
Jump out of the loop |
continue |
End the cycle |
Function and customizing function
At the beginning of C language program, the implication of #include <stdio.h>
is including a function library named stdio.h
and then the program can call functions in the library. Both the printf
and the scanf
used in the previous paragraph are functions of the library.
In practice, we often encounter repeated operations, we can copy this code to complete the repeated action, but code copy is a poor quality of the program, because the maintenance may need to change too many places.
You can solve this problem by customizing functions:
< |
A function can have multiple
return
or none. However, multiplereturn
are not recommended for easy modification.Each function has its own variable space, namely
{}
(block), which is independent of each other. Local variables are limited by the block they are in. If the inside of a block has the same name as the outside of a block, the inside of a block takes precedence.When a function is called, it can only pass values to functions, not variables to functions. That is, after passing a variable to a function, the function will read the value of the variable for operation, but will not change the value of the variable.
The first line of a function with a semicolon placed before the entire program code is called a function prototype declaration. The purpose is to tell the compiler what type the function is before it encounters it.
Array
Defination
type of variables + character + [number of variables]
For example:
int a[10]; |
An array is a container that, once created, cannot be resized, is internally ordered, and can appear on both sides of an assignment symbol.
The index of an array is counted from 0
.
You can think of it as a sequence in mathematics.
Use
Integration initialization is easy to use:
int a[3] = {1,3,5,}; |
If you don’t know how many cells there are in an array, you can use sizeof(a)/sizeof(a[0])
to represent the number of cells in the array, so that you don’t need to change the number of cells in the array.
Multidimensional array
A multidimensional array is actually a multidimensional matrix, and the footer increases accordingly.
Initialization:
int a[][5] = { |
The number of columns must be given and the number of rows can be counted by the compiler itself.
Pointer
Address
Each variable has an address in the computer where it is stored. The value of a variable can change, but its address is constant. The following code can be used to view the address of a variable.
int i = 1; |
&
is the address to access the variable;
*
is the variable on the access address.
Defination
A pointer is a variable, but it cannot be used independently. It must point to a variable. In this case, the value of the pointer variable is the address of the variable to which it points.
Use
int *p = &i; |
In this case, p
is a pointer to the address of variable i
. So the value of p
is the address of i
, and the value of i
can be accessed (read and write) by *p
.
The *
at definition is not the same as the *
at access, and the first is only used to distinguish whether a pointer variable or a normal variable is being defined.
Here is an example of using a pointer to complete a call to exchange the values of two variables.
|
This is a clever use of the function, we know that the function cannot input variable parameters, so this code defines the address of the pointer to the variable, the function input pointer variable is also the address of the variable, inside the function by adding a pointer to access the variable, and then achieve the purpose of the function to modify the variable.
In addition, pointers are often used when a function needs to return multiple values.
Arrays are special Pointers
|
The result of this code is:
0x7ffcac420c3c |
We can see that the array variable a
is itself an address, so when we want to use a pointer to array a
, we should write int *p = a
, without &
. But the array unit is variable, therefore int *p = &a[0]
, at the same time, a == &a[0]
,&a[x] == &a[0] + 4x = a + 4x
(when a
is integer).
An array is a pointer to a constant and therefore cannot be assigned.
Pointer to a constant (const)
int i; |
For the above code, you can think of the following code:
int i; |
1 indicates that the variable at the address pointed to by the pointer p
cannot be modified by the pointer.
2 indicates that the address (variable’s address certainly) pointed to by p
cannot be changed.
The first whole to the right of const cannot be modified
const int a[] = {1,2,3,}; |
The above code indicates that each cell is const
, so it can be used to protect an array when a function argument is entered.
Address of a pointer
int a[]; |
*p = a = a[0],
*(p+1) = a[1],
*(p+2) = a[2],
……,
*(p+n) = a[n]
.
Allocating Memory Space
The malloc function applies space in bytes from memory, returns void *
, converts the desired type, and finally frees the memory. Format, such as:
int a[n] = (int *)malloc(n * sizeof(int)); |
If the application fails, 0
or NULL
is returned.
When you no longer use the requested memory space, you should use the free
function to free the memory space:
free(a[n]); |
String
Overview
Char word[] = {'H','D','e','!'}; |
Such an array is an array of characters, but it is not a string, because it cannot be evaluated as a string.
Char word[] = {'H','D','e','!','\0'}; |
Followed by \0
, then word is a string.
0
= '\0'
!= '0'
0
marks the end of the string, but this 0
is not part of the string.
Strings exist as Arrays and are accessed as arrays or Pointers, but more often as Pointers.
string.h
has a number of functions that handle strings.
String variables:
char *str = "hello"; |
“hello”
The compiler will turn this into an array of characters somewhere, and the length of the array is 6, because the compiler will put a 0
after it to make it a string.
Literals of strings can be used to initialize character arrays.
|
The above code runs as follows:
&i = 0x7fffa827bcf4 |
You can see that the local variable i
is far from where the pointer s
is pointing. The address of the variable i
is very far back, and the pointer is very far forward. Near the front are important parts of the computer that are not allowed to be modified, such as plus s[0] = 'B'
‘, and the result is:
1869 segmentation fault ./a.out |
The program attempted to reassign the initialized string s
. The error “segmentation fault“ was reported, meaning that the program was attempting to rewrite the value at 0x55c7d8f64004, which posed a threat to the computer and was not allowed.
In fact, for char *s = "hello world";
pointer s
is initialized to point to a string constant, which should actually be const char *s = "hello world";
, but for historical reasons, compilers accept writing without const, and try to modify the string to which s
refers, with serious consequences.
s3
is an array, and the strings inside are local variables that can be modified, plus S3[0] = 'b'
:
&i = 0x7fff28d5dd64 |
There are two ways to define a string: pointer or array.
Arrays know the address of strings, Pointers don’t.
A char *
or int *
is not necessarily a string. It is meant to be a pointer to a character or an array of characters.
A char *
or int *
is a string only if the array of characters to which the pointer points has a zero at the end.
char *t = "title"; |
For the above code, we have two Pointers, t
and s
. First, t
points to the string “title”, and then we assign a value to s
. The result is that s
also points to the same string, instead of creating a new string.
Input and output of string
For printf
and scanf
, you can use %s
to input and output strings.
Each %s
in scanf
is read until a SPACE or ENTER, which is not safe because you do not know exactly how many characters to read, therefore, the following code is used:
char s[8]; |
Array of strings
char a[][10]; |
The first line refers to a
as a two-dimensional array, and the second line refers to a
as a pointer array. Each unit in the array is a pointer to a string.
Each element of the character array holds one character, plus the 0
at the end. An array of length n
can hold n-1
characters.
Multiple loops are required to input and output all elements of multiple dimensions, whether string arrays or integer arrays.
An array is a matrix that can be used to store variables or character variables. All input and output need to be looped, but there are two types of input and output for character arrays.
Input and output single characters in format %c
|
Input and output whole array with format %s
|
Note that input characters with
%s
that encounter a SPACE, RNTER, and TAB end the string input, so C provides the input functiongets()
and the output functionputs()
that are best for strings.
gets(char *s[])
function is to enter a string from the keyboard that can contain Spaces and end with a ENTER newline character.
puts(char *s[])
or puts(string s)
function prints a string from the character array to the screen and converts the end-of-string flag to a newline character.
In addition, when %s
prints a string, it keeps one dimension, and the compiler automatically inputs or outputs all strings in that dimension. Gets is the same as puts.
Input and output of character data
Putchar(parameter)
Paremeters can be numerical values, character constants, character variables, and arithmetic or character expressions, but the final output is a character.
getchar()
Type a character from the keyboard.
String function
strlen
|
The running result is:
5 |
So strlen
is string Length, which returns the length of the string.
strcmp
The function is to compare the size of two strings, and the result of the comparison is expressed by the value returned. 0
means they are equal, 1
means the former greater, and -1
means the latter greater.
strcpy
It means string copy,the format is:
char *strcpy(char *restrict dst, const *restrict src); |
Its function is to copy the src
string to dst
.restrict
means that src
and dst
cannot overlap.
The source is in the back, and the copying destination is in the front.
Return dst
so that the function itself can be evaluated.
General usage:
char *dst = (char)malloc(strlen(src)+1); |
strcat
The function is to link one string to another string.
Another version:
char *strncpy(char *restrict dst, const *restrict src,size_t n);
char *strncat(char *restrict s1, const *restrict s2,size_t n);
int strncmp(const char *s1, const char *s2, size_t n);The first two are to limit the length of the copied string, eliminating security issues that are neither out of bounds.
The last one is to compare the first
n
characters.
strchr
To find a character in a string.
|
The running results are as follows:
llo,world! |
Enumeration
enum type {num_0,num_1,num_2,……,num_n}; |
You can use the name in curly braces, where num_0
through num_n
represents the constants 0
through n
.
For instance:
enum colors {red,yellow,green}; |
enum type {num_0,num_1,num_2,……,num_n, number of type}; |
That’s just right. The last number of type is exactly the number of type. It’s a little trick.
Data structure
|
This means that you declare a data structure type, and when you use it, you define a variable that contains all the variables in the data structure.
The structure members of a data structure do not have to be of the same variable type, and an array can only be of one type.
Data structures can perform structure operations .
Assigning values to or between structure variables is a one-to-one correspondence; the former requires curly braces.
The name of the structure variable is not the address of the structure variable, so you need to define the pointer using &
.
A data structure can be entered as a function parameter, but unlike an array, the entire structure is passed into the function as the value of the parameter, creating a new structure variable inside the function and copying the value of the caller’s structure.
You can also return a struct
.
Custom data types
A typedef
can give an alias to a data type.
typedef long int64_t; |
Union
带参数的宏一定要有括号,结尾不能加分号。
- Post title: C Language
- Create time: 2022-05-25 17:34:01
- Post link: 2022/05/25/note/Computer/Programming/Language/C/
- Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.