Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Generating compile time error in C

Status
Not open for further replies.

Vaughn

Junior Member level 2
Joined
Feb 8, 2013
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,520
How can I evaluate any expression during compile time and generate compile time error in C?
Say I have a function

Code C - [expand]
1
2
3
4
5
6
7
void foo(int number){
    if(number < 10){
        //Do this;
    }
    else
        #error "Number is greater than 9 and this is compiler generated error";
}

 

"number" is a function argument, and the compiler doesn't know the value at compile time.
Values known at compile time can be tested like this:

Code:
#include <stdio.h>

#define MY_NUMBER 11

#if MY_NUMBER >= 10
#error MY_NUMBER is out of range
#endif

int main(int argc, char *argv[]){
  printf("hello, world!\n");
}
 
  • Like
Reactions: Vaughn

    Vaughn

    Points: 2
    Helpful Answer Positive Rating
It depends on the compiler, which you have not told us.

Things to try:
#pragma message ("print this while compiling"); -also try without parentheses.
#warning ("print this while compiling");
#error This is a compiler error message

Brian.
 
  • Like
Reactions: Vaughn

    Vaughn

    Points: 2
    Helpful Answer Positive Rating
I am using AVR Studio 5, don't exactly know which compiler is used in it.Actually I am passing a value to a function which I want to put it in a header file later. So for the reliability, I just wanted to put some error message there so that I could get any error or warning during compile time for any unwanted value.
 

You are confusing compile-time and run-time error checking.
If you want to make sure that the parameter passed to a function is within a certain range then that can only be done at run-time.
However, if you pass a constant (for preprocessor value) as the parameter in the call to the function, then that might be checked at compile time.
The test comes as: at what time to you know what the actual value is given the context of the check. In the code in your first post, you don't know the value within the function at compile time - that is why you have the 'if' statement and the code that is generated by it that will be run when the function is called - but you do at run-time. Therefore this is an example of run-time error checking. If you want to tell the user that a bad value has been passed then you need to use something like a print statement (if the user can fix the problem) or a stack dump (if it is an internal coding error).
Susan
 
  • Like
Reactions: Vaughn

    Vaughn

    Points: 2
    Helpful Answer Positive Rating
You are confusing compile-time and run-time error checking.

Absolutely correct. Further, the error message will only add confusion.

Compile time errors are produced during compilation and the user will be confused to get a error during run time that says compiler error!!

This error cannot be generated by the compiler.
 
  • Like
Reactions: Vaughn

    Vaughn

    Points: 2
    Helpful Answer Positive Rating
What you want is not properly error generation, but rather something better known as debug messages during runtime, (as Aussie remarked) which can be either logged to a local file or sent via a serial console monitor, from whose outcomes you often classify them in various levels, such as INFO, WANRING and ERROR, taking different actions depending on each case, as for example to abort the program, or perform some specific treatment routine.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top