C/c

Advertisement


Question
hello 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?

Answer
Hello 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,

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Kedar Desai

Expertise

You can ask me any questions about C and c++

Experience

I don't have practical experience. But i have knowledge about C as I belong to IT field since last 5 years.

Education/Credentials
I an studying in MCA from Maharashtra (Mumbai University)

©2012 About.com, a part of The New York Times Company. All rights reserved.