C/c
Expert: Kedar Desai - 10/30/2011
Questionhello Sir my doubt is about #error. #error is used to display user defined messages during compilation of a programme.
This is my knowledge about #error
Programme 1:
#include<stdio.h>
#include<conio.h>
#define b 1
Void main()
{
Clrscr();
#if !defined(a)
#error macro a is not defined
#else
Printf(“macro is defined\n”);
#endif
}
If I compile the programme the error “error directive macro b is not defined.”
Programme 2:
#include<stdio.h>
#include<conio.h>
#define b 1
Void main()
{
Int c=10;
Clrscr();
#error macro a is not defined
Printf(“%d”,c);
My question is1)what is the use of displaying messages during compilation using #error?
2)in programme 2 as long as #error in programme I can not print c
3)i want message using #error during compilation but the programme should be run.is it possible?
That means how can I print value of a in my second programme with out removing #error?
AnswerHello phaneendra,
#error produces compile time error messages.
Syntax:
#error token_string
The error messages include the argument token-string and are currently not subject to macro expansion. These directives are most useful for detecting programmer inconsistencies and violation of constraints during preprocessing.
When #error directives are encountered, compilation terminates.
So, using #error you can make your own error as per your rules and regulation in the program. for e.g. Validation of the data entered by the user.
As stated earlier when #error directive are encountered, compilation terminates. So, It is not possible to run program after using #error. Otherwise, there is no use of #error you also can use printf() statement instead of error directive. Because, terminating the program after using the error directive is its main feature.
Thank you,