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.

Sabyasachi said:   5 years ago
I agree with @Md Wasim Akram.

There is no error when we run this code in code block it only gives a warning for declare prototype.

Md Wasim Akram said:   7 years ago
There is no any error in this code while we compiled and run this code in CODEBLOCK (17.12) 32 bit. So according to me, the correct answer will be option A not C.

Correct me if I am wrong.

Leela said:   7 years ago
The function was declared and defined so why its showing an error?

And I got output in code blocks.

It differs from compiler to compiler. So please anyone help me.

Multimeter said:   8 years ago
If we remove void In calling function, we will get the output.

Avinash said:   9 years ago
Guys stay away from turbo C compiler.

Sourav said:   1 decade ago
What's the difference between turbo C and gcc compiler?

Also in "++" operator give different output from gcc and turbo C compiler why?

Shreya said:   1 decade ago
Hey please tell me that if the printf and scanf gives "should have a prototype" kind of errors then what to do.

Ganesh singh said:   1 decade ago
Its best solution is we know compiler by defult assign return type is integer but later it gets void so mismatch occurs.

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.

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


Post your comments here:

Your comments will be displayed after verification.