473,408 Members | 2,813 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,408 software developers and data experts.

Why have prototypes...am I understanding this correctly?

I am learning code from scratch, so bear with me.

Using the following code as an example:

---
int add(int,int);

int add(int i, int j)
{
return i+j;
}

int main()
{
printf("%d\n",add(3,3));
}
----

The above code works without the first line. Why have it there?
May 4 '11 #1

✓ answered by weaknessforcats

Not all of your source code will be in the same file.

If this code:
Expand|Select|Wrap|Line Numbers
  1. int add(int i, int j)
  2. {
  3. return i+j;
  4. }
is in a separate file then how does the compiler know that this is correct:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. printf("%d\n",add(3,3));
  4. }
?

The answer is that you tell the compiler that there is a function named addthat takes two int arguments and returns an int. You do this using a prototype:

Expand|Select|Wrap|Line Numbers
  1. int add(int, int);
The compiler takes your word for that
and marks "int add(int,int)" as an unresolved external reference. The linker will locate the function and fix up the addresses so that the call works.

Note that your function prototypes are exactly the first line of the function. The function body between the {} is replaced with a semi-colon:


Alos, try moving the code to the end of the file with main() and then compile with and without your prototype. See what happens.

3 1480
weaknessforcats
9,208 Expert Mod 8TB
Not all of your source code will be in the same file.

If this code:
Expand|Select|Wrap|Line Numbers
  1. int add(int i, int j)
  2. {
  3. return i+j;
  4. }
is in a separate file then how does the compiler know that this is correct:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. printf("%d\n",add(3,3));
  4. }
?

The answer is that you tell the compiler that there is a function named addthat takes two int arguments and returns an int. You do this using a prototype:

Expand|Select|Wrap|Line Numbers
  1. int add(int, int);
The compiler takes your word for that
and marks "int add(int,int)" as an unresolved external reference. The linker will locate the function and fix up the addresses so that the call works.

Note that your function prototypes are exactly the first line of the function. The function body between the {} is replaced with a semi-colon:


Alos, try moving the code to the end of the file with main() and then compile with and without your prototype. See what happens.
May 4 '11 #2
Thanks for this. I'm using a free online source http://www.cprogramming.com/

It's great, but sometimes it's not enough. There is a lot you have to take for granted.
May 5 '11 #3
donbock
2,426 Expert 2GB
C made its appearance sometime around 1972. Function prototypes were codified in the ANSI C Standard in 1989. Function prototypes were added to the language because they would eliminate a certain class of widespread and pernicious bugs.

I want to emphasize that weaknessforcats described a very real and pervasive problem -- not some academic and abstract possibility.
May 5 '11 #4

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

Similar topics

7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
7
by: laura | last post by:
I'm trying to understand fully Server.MapPath. I am writing an intranet for a small company and want to be able to put some files accessible to all, hyperlinked from the intranet and therefore,...
14
by: fb | last post by:
Does the C language require you to prototype functions? If it's not required, is it recommended?
9
by: Grumble | last post by:
Hello everyone, I've come across some strange code. Here it is, stripped down: int main(void) { int *foo; int *bar(); foo = bar(0); return 0;
19
by: elzacho | last post by:
It is my understanding that prototypes in C are purely optional. To my experience, until today I guess, this has been the case (other than eliminating compiler warning messages). However, today...
7
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
5
by: goldfita | last post by:
I have the following two prototypes in separate source files. Both programs compile and work correctly. (1) void general_foo(void (*foo)(), int opt, ...); (2) static int...
1
by: petermichaux | last post by:
Hi, I have searched the archives but didn't find the questions and answers I am looking for. I have been looking at Prototype.js quite a bit lately as I need to create a very small library of...
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.