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 2 of 2.
Susmitha said:
1 decade ago
When should we suppose to write prototype declarations? is it compulsory?
Vivek said:
1 decade ago
Why gcc compiler will not give error ?
Swetha said:
1 decade ago
What's the difference between turboc and gcc compiler?
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.
Arya said:
1 decade ago
i run the program in the compiler but i did not get any error.why?
Sathish said:
1 decade ago
There is no need of prototyping in modern compilers, even though we define the function below the main function.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers