#Defining functions below main().
126 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Put the function declaration above main but the implementation below
How can I put a function declaration above?
I tried it with int add() {} but that does not work.
@mellow island
#include <stdio.h>
int add() {}
int main()
{
printf( "%d", add(2,2) );
return 0;
}
add(int a, int b)
{
return a + b;
}
Ah okay so I have to predefine the arguments...
Anyway thanks, works now!
Does it matter if I put int before add below?
Nevermind. Seems to be required.
Yes
Otherwise it is a different function
You have to put the return type
Functions have arguments and a return type, all have to be identicle
I see, good to know.
I am starting to learn C and right now I am building some basic blocks so I can combine them later to create working code.
_ _
So since I really dislike having anything above my main() function (distasteful style in my opinion) it might be better to just import the code.
So next up is to have a file that contains all the nasty imports and only import this so I can have a clean main function that I am working on.
This is the way
Just use header files
Ah! Heard about these.
Where are you learning about c?
Or how rather?
I recommend Beej's guide to c
It's free online
So far I use online resources such as complete cources CS50 e.g. on youtube.
As long as you have a single cohesive resource you should be good
Yes. My first programming language was Python and I feel like I can do a lot with it quickly enough now.
However performance of Python is slow.
That is why I want to learn C. I will continue to use Python for quick scripting.
And C to do fast calculations.
Cool yeah
I'm writing a program in c now to run on a supercomputer
Using MPI
OpenMPI
I've told my housemate I will teach him c tomorrow so he can do the same
Why is it important to stick to one cohesive resource?
I learned Python by tutorials trying to implement everything I learned in projects that I think are useful for myself. So mostly I learn by just trying things.
That's a good way to learn
That sounds awesome!
But using multiple sources can make it easier to confuse yourself early on.
For a lot of things
Different people have different ways of doing things
Tbh with c it's probably not too bad
But for other things (like maths and physics) it's important
So I'm a little stricter on thay front
I think since I already learned one language I have some sort of pattern in mind. First of all I focus to learn to do what I already can do in Python.
(I'm a physics student)
Oh I am doing a lot of maths to. So I know what you mean.
Currently doing information entropy.
Not quite sure if that is the correct term. Well part of it is for example Huffman Coding.
@vital shoal Has your question been resolved? If so, run !solved :)
Teach me C as well. 😎🍀
Lol I could
And for what are you using that supercomputer?
Simulations of many gravitationally interacting bodies
Like galaxy collision simulations
Honestly just get comfortable with pointers and your 90% there
Sounds cool.
I looked for the guide of Beej.
Am I overlooking something?
I have found a weird html website.
Can you provide the link?
Oh wow, that's nice.
Can you tell me how I can import a header?
Probably: #include "header.h"
why not <header.h>
Yep
The angle brackets are for standard library things
BTW include literally tells the preprocessor to copy and paste from header.h to the file
Awesome. It works.
Before compilation starts
Better than having to deal with ugly code while production.
Yep
How do I know if I should use a header.h or a main2.c
Oh hm...
Well my header.h contains functions such as:
int add(int a, int b)
{
return a + b;
}
OH...
Yea no I continue convention for now. Probably there is good reason.
sub.c:(.text+0x0): multiple definition of `add'; /tmp/cc69VYR2.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
I renamed it to "sub.c"
Yeah the linker is confused. You have two definitions of add, which one should it use? It doesn't know and throws an error
Show me the code of both files?
// main.c
#include "sub.c"
int main()
{
printf( "%d", add(2,2) );
return 0;
}
// sub.c
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
Change add to myadd
Still get the error?
sub.c:(.text+0x0): multiple definition of `myadd'; /tmp/ccVnv5MD.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
Yep..
How are you compiling?
I think your compiler is being dumb
If you change it back to sub.h it works?
I will try.
If so it's your compiler being too clever for it's own good
I think what is happening is when it is called sub.c instead of sub.h your compiler is compiling both sub.c and main.c into object files then trying to link them, but becuase main.c also contains add (becuase of the include) there are two definitions
#include "header.h"
#include "myadd.c"
int main()
{
printf( "%d", add22(2,2) );
return 0;
}```
```c
#include <stdio.h>
int add22(int a, int b)
{
return a + b;
}
int add(int a, int b);```
myadd.c:(.text+0x0): multiple definition of `add22'; /tmp/cclVVaDU.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
I need to see how you are compiling
Online GDB is online compiler and debugger for C/C++. You can compile, run and debug code with gdb online. Using gcc/g++ as compiler and gdb as debugger. Currently C and C++ languages are supported.
I just use an online compiler for quick results.
Can I get back to you tomorrow? I need to head to bed. Header files are basically a promise to the compiler that you know what a function takes as arguments and outputs, but you will provide it later (by linking). So you either use header files or include sub.c
By using both the compiler is getting confused.
Set it up on your own system and have a play around with it.
Use either msvc, gcc, or clang.
Compiling c programs is the hardest part
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.