
Ma Xiaobian HundredMillionFans CertifiedAccount
By clicking follow, you not only gain a tool for finding resources but also an interesting soul ▶ ▶ ▶
Question Description:
A certain function fun_1() is in the library and cannot be modified. It is used extensively in the program. Now, I want to make the original fun_1 ineffective (the current method to make it ineffective is #define fun_1(..)), and use another function fun_2(). However, fun_2 also needs to call fun_1 in the end. The above method to make it ineffective doesn’t seem to work. What should I do?

Fan question, must be arranged!
Let me summarize the question simply:

- We have a function in our library file called read()
- We now want to define our own function with the same name read()
- The main() function first calls our defined function read()
- Our defined function needs to define the library’s read() function.
The problem lies in how to make our defined read() function only call the library’s read function and not itself.
Solution Approach – static
If we want to use a function with the same name as a library function, we need to use the static keyword.
By adding static before the return type of the function, it becomes a static function. Its characteristics are as follows:
- A static function is only visible in the file where it is declared; other files cannot reference this function.
- Different files can use static functions with the same name without affecting each other.
- If other libraries have functions with the same name, the static function in this file will be prioritized.
Example
The system call function read() is defined as follows:

Now we want to define our own function, which is also named read. How should we proceed?
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static void read()
{
printf("my read func()\n");
}
int main()
{
read();
}

We can see that although we included the header file for the system call read(), it calls our defined read() function.
Next, let’s see how to make our defined read function call the system call read(). We must add another file to place the related functionality; it cannot be achieved in the same file.
Here is the code; knowledge points without evidence are just nonsense. [Most of my articles are supported by example code]
//test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void test()
{
int fd;
char buf[128]={0};
fd = open("123.c",O_RDWR);
if(fd<0)
{
perror("open fail\n");
return;
}
read(fd,buf,16);
printf("enter test():%s\n",buf);
}
//123.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extern void test();
static void read()
{
printf("my read func()\n");
test();
}
int main()
{
read();
}
Execution Result

From the execution result, we can see that the program calls both our defined read() function and the system call read().
The function call sequence is as follows:

Problem solved, did you learn something?
High-quality original review
The most difficult IT company to get into in China…
Elon Musk sent a private message asking a beautiful internet celebrity to have his child, but after being rejected, he cut off the internet celebrity’s $21,000 advertising income every two weeks?
A colleague from outsourcing attended a dinner, and just as he arrived at the door of the private room, he heard the supervisor say: We are all insiders, in the future, any dirty or tiring work will be given to outsourcing, they are here to serve us, don’t feel bad about it.
After leaving the job, a bug appeared online, and the interface was developed by myself; the n+1 compensation was reclaimed.
Heard that the internet doesn’t care about age 35 anymore?
Programmers start “defensive programming” to protect their jobs…..
This move by ByteDance directly changed everyone’s habits?
Huawei employees revealed: Most people in OD are at their peak upon joining! Level D4, except for a 3k monthly salary increase when promoted to D5, the salary has never increased at other times.

Don’t forget to share, bookmark, look, and like!