14478

Why Function declaration needed in this? [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/9182763/implicit-function-declarations-in-c" dir="ltr" rel="nofollow">Implicit function declarations in C</a> <span class="question-originals-answer-count"> 6 answers </span> </li> </ul>

<strong>Code:</strong>

#include <stdio.h> int navin(); //function declaration int main() { int i = navin(); //function call } int navin() //function definition { printf("Hello"); }

<strong>Output:</strong> Hello

<strong>Question:</strong>

If we remove the function declaration line int navin(); from the above code, it prints the same. Then why it is used?

Answer1:

The function declaration here is called the forward declaration. This is used if the function has been called [used] <em>before</em> it is defined.

In your code, see, when navin() is called inside main(), at that point, the function is not yet defined. By using the forward declaration, you're telling compiler,

<blockquote>

"This is the function signature. This function definition is present later in the code, continue the compilation."

</blockquote>

Without this forward declaration, most of the c compilers will show you a warning [termed as implicit declaration] that they are not able to find the function navin(), so, the compiler will <em>assume</em> the function prototype as

<ul><li>any number of input arguments</li> <li>return type as integer.</li> </ul>

In most of the cases, this ends up in a disaster.

You can find more information in <a href="https://stackoverflow.com/q/8440816/2173917" rel="nofollow">this</a> related question.

Profound usage: The header files.

<hr />

NOTE:

In older c standards, c89, this was supposed to be only an warning, but the newer versions, c99 and c11 defines this [missing forward declaration] to be an error. However, by default gcc [c complier on linux] doesn't implement these standards. If you tell gcc explicitly to follow c99 and higher, it'll produce the <em>correct</em> error message.

Answer2:

The function prototype is shown at the top of the file. This allows the compiler to check the code more thoroughly.

My experience is that if you did not declare the function at the top of your program, you also could define this function before you use it (before main function). for example in your program:

#include <stdio.h> int navin() //function definition { printf("Hello");<br /> } int main() { int i = navin(); //function call }

Recommend

  • what happens to finally block in the following cases?
  • Python Floating Point Formatting
  • Outlook Com not registered
  • Compact framework voice recognize API or library
  • With one EDMX file use multiple connection strings that relate to multiple databases
  • AutoHotKey - how to send control and same key multiple times
  • Using Paypal Pro in Omnipay
  • Recursion Control flow
  • Passing information to server-side function in a Google Docs Add On
  • How to use animated gif in Firemonkey?
  • What's the syntax to inherit documentation from another indexer?
  • In-place sed command not working
  • Appending Character to Character Array In C
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • FileReader+canvas image loading problem
  • Why value captured by reference in lambda is broken? [duplicate]
  • Using $this when not in object context
  • Is there any way to access browser form field suggestions from JavaScript?
  • Android screen density dpi vs ppi
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Perl system calls when running as another user using sudo
  • Statically linking a C++ library to a C# process using CLI or any other way
  • jQuery tmpl and DataLink beta
  • How to pass list parameters for each object using Spring MVC?
  • KeystoneJS: Relationships in Admin UI not updating
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF