LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2003, 08:42 AM   #1
xailer
Member
 
Registered: Nov 2003
Posts: 77

Rep: Reputation: 15
Question about function prototypes


hi,Im learning c from a book written prior to today's ANSI standard and I noticed some things are quite different

1-Must a function which is called in a file before it is defined,be declared with function prototype?

2-Can a Function prototype be declared in each function that calls this particular function,instead of being declared in the beginning of the file?

3-How about function that is defined before it is declared?Is then function prototype not needed?

thank you
 
Old 12-13-2003, 10:17 AM   #2
hiteshmaisheri
Member
 
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231

Rep: Reputation: 30
The answer to it are
1. the function prototype is required only when it is called before it is defined during the run of the code....
2. yes the function prototype can be declared in each function that calls this particular function, instead of being declared in the beginning of the file...
if it is done so then this declaration would be local to the function... a better practise is to define all the functions at the begining of the file... before the main
3. it is immaterial to declare if the function is defined before it is first called....
 
Old 12-13-2003, 10:29 AM   #3
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

Quote:
Originally posted by hiteshmaisheri
The answer to it are
1. the function prototype is required only when it is called before it is defined during the run of the code....
But my program works even if I don't use function prototype(assuming function is called prior to its definition)

thank you so much for helping
 
Old 12-15-2003, 12:36 AM   #4
cetialphav
Member
 
Registered: Sep 2003
Location: Raleigh, NC, USA
Distribution: Fedora
Posts: 88

Rep: Reputation: 16
Just because it works, it doesn't mean it is correct. It may only work with your test case or it could fail on other compilers. The compiler uses the function prototype to determine if the call you are making is of the correct type, etc. If the compiler doesn't know, it guesses. I think it basically assumes that everything in the function call is an int. Depending on what you are doing, that may or may not work.

This is one of those cases where C doesn't do a lot to stop you from shooting yourself in the foot. It is up to the programmer to protect himself by using the prototypes.
 
Old 12-15-2003, 10:10 AM   #5
hiteshmaisheri
Member
 
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231

Rep: Reputation: 30
certain features if the work may be due to the non compliance with the ansi c.... just use the gcc.... to get better results
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about C - 'restrict' in function declarations? lowpro2k3 Programming 1 09-11-2005 12:50 AM
A question about the listen function Nerox Programming 3 07-31-2005 09:06 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM
extract prototypes from a shared library tieulent Linux - Software 1 08-27-2004 10:15 AM
A question about the gets() function in C OrganicX Programming 3 10-20-2003 01:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration