Course Content#
Overview of C Language#
- Why learn C language?
Assembly Language | C Language (Compiled Language, High-level Language) |
---|---|
Strong dependence on hardware devices - registers | Portability |
Direct communication with registers, complex code, higher efficiency | Simple, readable, high development efficiency |
Example: Want to communicate with foreigners | |
Learn English | Find a translator |
-
Importance of C
- Linux came after C
- Unix operating system was not free, so Linux was developed by Linus
- Linux is implemented in C language at the low level
- Java, C++, and Python are all based on C
PS: Mac OS is based on Unix
- Linux came after C
-
Learning thinking logic, programming paradigm
- Not just syntax
- C language only supports one: procedural
- C++ supports four: procedural, object-oriented, generic programming, functional programming
Input and Output Functions#
Analogous to human listening and speaking
- Output function: printf
Header File | stdio.h | |
---|---|---|
⭐Prototype | int printf(const char *format, ...); | Outputs characters |
Return value, int, number of characters successfully outputted | '\n' is one character | |
Parameter 1, format, format control string | ||
Variable argument list, ... | Legal statement, can be compiled |
- Input function: scanf
Header File | stdio.h | |
---|---|---|
⭐Prototype | int scanf(const char *format, ...); | |
Return value, int, number of parameters successfully read | <0, illegal; ≥0, legal (including 0) | |
while(scanf(...) != EOF) | -1 <=> EOF (hidden file descriptor) |
- Keyboard input file end symbol EOF:
-
- Unix: Ctrl+D
-
- Windows: Ctrl+Z
-
- You can also use Ctrl+C to interrupt the loop
C Language Documentation and Coding Standards#
-
Use Wikipedia to view functions
-
cpp reference authoritative documentation
- Refer to Pirate Treasure official website
-
Coding standards (more code, more bugs) Google is the father
- Baidu + Google
- Alibaba + Google
- Reduce bug rate
-
Reference books (one month is enough; this book is written across disciplines)
-
In-class Exercises#
-
- Without using the while function, you can directly use the return value of printf
- Nested printf
- Code

- %[^\n]s
// Purpose:
char str[100] ={0};
scanf("%s", str); // Enters the next parameter when encountering a separator such as a space
scanf("%[^\n]s", str); // Can read spaces as characters, [] after s is optional
|[]|Character matching set|
|:----:|:----|:----:|:----|
|^|Negation/except|
|\n|Newline|
- There will be a problem when reading in a loop
while (scanf("%[^\n]s", str) != EOF) {
...
}
- When inputting "Hello World", the output will not stop and will continue to output "Hello World" in a loop
- Because \n is treated as a separator and enters the next parameter assignment, but \n will not be swallowed
- Referring to the course QA--Question about scanf in the C language course on October 13: The reason why it gets stuck at '\n' is because %[] cannot consume leading white spaces
My own understanding is slightly different from the above
- If \n is negated in the regular expression, it cannot be read in, but it will be treated as a separator, so it enters the output statement but cannot be read in, so it gets stuck in the loop;
- Without the regular expression, \n is treated as a separator and is read in, and then it stops, but it will enter the next parameter assignment when encountering a space.
-
-
Use getchar() to swallow a character, such as \n
- Print the value of getchar(), and you can see that \n is swallowed, so it continues to read the next line
-
-
The red box is the swallowed character: \n, which can be seen in lines 15-20 of the code
-
Maybe you can also read strings containing spaces by reading char in a loop, using a two-dimensional character array, and entering the next line when encountering \n.
-
Another way to think about it, refer to Problem of \n in scanf-CSDN
-
Code
-
Key Notes#
- scanf uses separators (spaces, newlines, etc.) to separate input for multiple variables
- The assignment operator (=, -=, etc.) has a very low priority (associativity: right to left), lower than the == operator
- The value of the expression formed by the assignment operator is the value assigned
int ret;
printf("%d\n", ret = 3);
// Outputs 3
-
./a.out > output can redirect the output to the output file (overwrite the original file) without displaying it on the terminal
- The meaning of > is standard output redirection
-
Basic operations of vim, you can practice with vimtutor, see Shimo Document - Common Commands in Linux: Vim
-
In the format control string of printf(), if \n is not used, there is a % at the end
- This percent sign is added by the shell because you did not enter a newline.
Think about it, if you didn't enter a newline, shouldn't the shell prompt immediately follow your output (such as hello word), which looks messy
- Note: %c should be used with caution, as it will read anything
Code Demonstration#
printf Function#
Basics of the printf Family#
- printf: Prints to standard output (stdout)
- sprintf: Prints to a string
- Can be used for string concatenation
- sprintf(str, "", ...);
- fprintf: Prints to a file
- Open the file first, fopen: w for write, r for read, a for append
- Returns a pointer of type FILE
- fprintf(FILE pointer, "", ...);
- "..." reads the address of the variable in the variable argument list
- The string variable itself is passed as its starting address
- Open the file first, fopen: w for write, r for read, a for append

int main()
{
FILE* fp;
int i = 617;
char* s = "that is a good new";
fp = fopen("text.dat", "w");
fprintf(fp, "%d\n", i); // Just pass the variable name, no need to pass the address
fprintf(fp, "%s", s);
fclose(fp); // Remember to close the file corresponding to fclose
return 0;
}
Practice#
- Add different types of brackets to str based on the last three binary digits of n
-
-
n is defined at the beginning of printf
-
Define the swap function using the macro definition method
-
-
Use __typeof to get the variable type and directly use it to declare the type of the intermediate variable
-
There cannot be two parameters with the same name for sprintf?
- 💡The names cannot be the same?
- If both parameters are named str, see below:
- 💡The names cannot be the same?
-
-
The result is as follows: Among them, the length of the value in () is the same as the original value, and the content in () seems to overwrite the original content
- Do you have to pass the address in the pointer way?
- If you directly use the variable, it works, see below:
-
-
-
-
- But if there are multiple conditional judgments, is it easy to swap character arrays without using pointers??
- ❓In fact, the variable name of the character array represents the starting address of the character array, so you can directly swap addresses? It failed, it should not work, after all, how can an array be easily moved, but the pointer pointing to the array can be moved
- So still learn to use pointers~
- The FILE pointer of fprintf can also be stdout and stderr
- But if there are multiple conditional judgments, is it easy to swap character arrays without using pointers??
-
-
-
- The final output is as follows:
-
-
-
- What is the difference between stdout and stderr?
- You can use > to redirect standard output stdout, and stderr will not be redirected, see below:
- What is the difference between stdout and stderr?
-
⭐: You can view error information separately, while shielding a large amount of normal output
-
-
-
- Similarly, you can use 2> to redirect only the error output, see below:
-
-
Additional Knowledge#
- File descriptor
- stdin: Standard input
- stdout: Standard output
- stderr: Standard error output
- EOF: -1
- File permissions
- Everything is a file in Linux~
- ls -al .: Displays file permissions
- rwx Read, write, execute
- Three common methods for array initialization ({0}, memset, for loop assignment)-CSDN
Points to Consider#
- Why do scanf parameter lists need to pass addresses?
- scanf needs to modify the values of the parameters, and if passed by value, their values cannot be changed