In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to carry out C language multi-file programming", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to do C language multi-file programming"!
Table of contents:
1. For example (a main function writes to the end)
two。 So how do you split it into more organized multiple files?
Read me:
Many beginners, sometimes not even beginners, write code in a main file to the end.
If you have a large amount of code, you need to call a lot of functions and library files, and if you still have only one main file, the code is very difficult to read and bad for developers.
Then we need a well-organized sub-module design, each function written as a separate file cpp.
All of these files are then called in the main function, which makes the code clear.
Text:
1. For example (a main function writes to the end):
/ / A main function writes to the end # include int main () {int a = 2 _ int b = 1 _ int c = a + b _ bot / take the addition operation as an example, which actually represents a more complex operation with a large amount of code. Int d = a-b bank / take subtraction as an example, which actually represents a more complex operation, the one with a large amount of code. Printf ("c =% d\ n", c); printf ("d =% d\ n", d); printf ("ok~\ n"); return 0;}
two。 So how do you split it into more organized multiple files?
First of all, we make it clear:
A main function file main.cpp
Addition function file myadd.cpp
Subtraction function file mysub.cpp
In fact, here, we can put addition and subtraction in one file, that is, mycal.cpp. Put our function file in this file.
Is this over?
The answer is no, there is no problem with this method of operation in matlab, but the programming method of Cmax Cure + is different, we need to use the header file. H
We need the header file to declare, and then call the header file in the main function main.
The code is as follows:
1) / / main.cpp
/ / main.cpp#include # include "mycal.h" int main () {int a = 2int b = 1int c = myadd_func (a, b); int d = mysub_func (a, b); printf ("c =% d\ n", c); printf ("d =% d\ n", d); printf ("ok~\ n"); return 0;}
2) / / mycal.cpp
/ / mycal.cpp#includeint myadd_func (int a _ line int b) / / addition function {int c = a + b return c;} int mysub_func (int a _ line int b) / / subtraction function {int c = a-b return return c;}
3) / / mycal.h
/ / mycal.hint myadd_func (int a, int b); int mysub_func (int a, int b)
Note that the names of mycal.cpp and mycal.h are the same.
In vs2017, the file structure is shown as follows:
After compiling the connection, the running result is as follows:
Run successfully ~
Results OK~~
Congratulations, you have mastered the ideas and methods of C language multi-file programming.
At this point, I believe you have a deeper understanding of "how to carry out C language multi-file programming". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.