473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Divide code on parts in C

Hello,
I am trying to divide the following code on three modules. I am sure I did everything correctly, however I got an error. I was wondering if there is something specific about this code that I did not take into consideration. Thank you.


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. void function1(void);
  4.  
  5. void function2(char *str);
  6.  
  7. void function3(void);
  8.  
  9. void print_string(char *str);
  10.  
  11. int num;
  12.  
  13. int main (void)
  14.  
  15. {
  16.  
  17. num = 3;
  18.  
  19. function1();
  20.  
  21. print_string("DONE\n");
  22.  
  23. return 0;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. void function1()
  30.  
  31. {
  32.  
  33. char phrase[] = "The string passed from function 1";
  34.  
  35.  
  36. print_string("In function 1");
  37.  
  38. function2(phrase);
  39.  
  40. }
  41.  
  42. void function2(char *str)
  43.  
  44.  
  45. print_string("In function 2");
  46. printf("Printing -- %s\n", str);
  47.  
  48. function3();
  49. }
  50.  
  51. void function3()
  52. {
  53.  
  54. print_string("In function 3\n");
  55.  
  56. printf("num cubed = %i\n", num*num*num);
  57. }
  58.  
  59. void print_string(char *str)
  60. {
  61.  
  62. printf("%s\n",str);
  63. }
  64.  
  65.  
Mar 11 '15 #1
3 1409
weaknessforcats
9,208 Expert Mod 8TB
OK.

First, main() only calls function1(). Therefore, you only need the function prototype for function1() in the file with main().

Second, in the file with function1() you need the function prototype for function2().

Third, in the file with function2() you need the function prototype for function3().

Fourth, in the file with function3() you need the function prototype for print_string().

You now compile all the files and let the linker combine the separate object files into your executable. If you are using a tool like Visual Studio this is done by having one project with five source files. You then just build the project. If you try to do this manually, you will need to write a make file to do the five compiles and the final link. I hope this is not the case since writing make files is archaic.

Always follow the simple rule: You must declare a function before you call it. This declaration is the function prototype which is just the first line of the function followed by a semi-colon.
Mar 11 '15 #2
Okay. So, I created 3 different files:


file 1:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. void function1(void);
  4.  
  5. int num;
  6.  
  7. int main (void)
  8.  
  9. {
  10.  
  11. num = 3;
  12.  
  13. function1();
  14. print_string("DONE\n");
  15.  
  16. return 0;
  17.  
  18. }
file2:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3.  
  4.  
  5. void function2(char *str);
  6. void function3(void);
  7.  
  8.  
  9.  
  10.  
  11. void function1(void)
  12. {
  13.         char phrase[] = "The string passed from function 1";
  14.         print_string("In function 1");
  15.         function2(phrase);
  16. }
  17.  
  18.  
  19. void function2(char *str)
  20. }
  21.         print_string("In function 2");
  22.         printf("Printing -- %s\n", str);
  23.  
  24.         function3();
  25. }
file3:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3.  
  4. void function3()
  5. {
  6. print_string("In function 3\n");
  7. printf("num cubed = %i\n", num*num*num);
  8. }
  9.  
  10.  
  11. void print_string(char *str)
  12. {
  13. printf("%s\n",str);
  14. }
  15.  
  16.  
Get several errors..
Mar 11 '15 #3
donbock
2,426 Expert 2GB
File1 calls print_string but that function hasn't been declared.
File2 calls print_string but that function hasn't been declared.
File3 calls print_string on line 6, but that function isn't declared/defined until line 11.
File3 uses undeclared variable num.
Mar 12 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: wilsonidv | last post by:
Daer All: I have studied C language for just 2~3 months. I'd like to know the critical parts of C, focusing on these. Could anyone has many experiences tell me, please. Thanks and Regards.
2
by: Michael Braitmaier | last post by:
I have a quite severe problem. I am trying to compile a C++ library initially written for VC++ 6. To make the library available for .NET languages I want to compile the library as managed C++...
1
by: Sagaert Johan | last post by:
Hi I made a control that in its onload sets the dockstyle to Fill, I only want this in runmode not in the IDE forms designer. Is there some attribute or define i can rely or use to prevent...
17
by: Jeff | last post by:
Ok gang. Here is what I have. I am using asp with Access DB on a microsoft server. I don't know if a premade script exists out there, but I couldn't find it. I am looking for a script to use for...
5
by: SharpCoderMP | last post by:
is it possible to implement some safe way of performing two or more instructions in a kind of "atomic" way? consider this: we have two queues (implemented on top of an List<T>). now we need to...
10
by: saipathakota | last post by:
how the following two code parts same: #include <stdio.h> void main () { char i = NULL; char &q = i;
6
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
3
by: serdar | last post by:
Title looks unclear but what I need is fairly simple: Suppose we have a rope 100cm long, and we want to divide it into 10 equal parts. In this case each part would be 10cm long. Now, what if we...
5
by: KenpachiZ | last post by:
Ok, so here is the problem. I have a number, and I need to divide it into maximum unequal parts such that no part is less than 25 in value. Even if you cannot solve the problem, any help or ideas are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.