C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Point Out Errors (Q.No. 1)
1.
Point out the error in the following program (if it is compiled with Turbo C compiler).
#include<stdio.h>
int main()
{
    display();
    return 0;
}
void display()
{
    printf("IndiaBIX.com");
}
No error
display() doesn't get invoked
display() is called before it is defined
None of these
Answer: Option
Explanation:

In this program the compiler will not know that the function display() exists. So, the compiler will generate "Type mismatch in redeclaration of function display()".

To over come this error, we have to add function prototype of function display().
Another way to overcome this error is to define the function display() before the int main(); function.


#include<stdio.h>
void display(); /* function prototype */

int main()
{
    display();
    return 0;
}
void display()
{
    printf("IndiaBIX.com");
}

Output: IndiaBIX.com

Note: This problem will not occur in modern compilers (this problem occurs in TurboC but not in GCC).

Discussion:
18 comments Page 1 of 2.

Yogesh said:   2 decades ago
i do need clarification in this naturally the display fn is called and it is defined also then why cant the "IndiaBIX.com" cant be printed is that mandatory that function prototype need to be given

please clarify

Sundar said:   2 decades ago
Hi Yogesh,

This kind of problem will not occur in modern compilers but in C.

There is no need of prototype declaration if we define the function display() above the main function.

To understand it clearly, kindly go through the compiler principles (different passes of compiler).

Have a nice day.!

Sathish said:   1 decade ago
There is no need of prototyping in modern compilers, even though we define the function below the main function.

Arya said:   1 decade ago
i run the program in the compiler but i did not get any error.why?

Sundar said:   1 decade ago
@Arya:

Because, the online compiler given in this site is an advanced compiler (GCC - 32 Bit Linux platform) while comparing to Turbo-C.

So, it gives no error in GCC but in Turbo-C.

Swetha said:   1 decade ago
What's the difference between turboc and gcc compiler?

Vivek said:   1 decade ago
Why gcc compiler will not give error ?

Susmitha said:   1 decade ago
When should we suppose to write prototype declarations? is it compulsory?

Siva rama krishna said:   1 decade ago
What is the use of display(), return 0;

Naveen said:   1 decade ago
@Susmitha.

When using a function before defining.

@Siva.

display(); --> calling a display function.
return 0; --> return type of main function is int so return 0 is used.


Post your comments here:

Your comments will be displayed after verification.