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");
}
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.
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.!
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.!
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
please clarify
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.
Correct me if I am wrong.
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.
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.
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.
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.
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.
And I got output in code blocks.
It differs from compiler to compiler. So please anyone help me.
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?
Also in "++" operator give different output from gcc and turbo C compiler why?
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.
There is no error when we run this code in code block it only gives a warning for declare prototype.
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.
Sathish said:
1 decade ago
There is no need of prototyping in modern compilers, even though we define the function below the main function.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers