#assert Directive

This page was translated by a robot.

The #assertdirective is used to define more or less standard predicates, such as specifying the processor type. However, since no standard ever prevailed, these predicates are insecure, since they are handled and named differently from system to system and sometimes completely omitted. The use of the #assertdirective is therefore NOT ADVISED for the inexperienced programmer.

Details

The #assertdirective defines predicates, where a predicate can be paraphrased with an answer to a question . The #assertdirective specifies what the only valid answer is for a predicate. The answer to a predicate is ()written in round brackets. To query the predicate using the #ifdirective# , the predicate must be preceded by a . The answer to be checked is again ()written in round brackets.








Hallo Welt!
#include <stdio.h>
#assert language(de)

int main(){
  #if #language(en)
    printf("Hello World!\n");
  #elif #language(de)
    printf("Hallo Welt!\n");
  #endif
  return 0;
}

To query whether any value was set for a predicate at all, the answer including the brackets can simply be omitted. To redefine a predicate, the information can be #assertoverwritten with a new directive. To delete the predicate, the #unassertdirective is needed.

An example: On certain systems, a predicate named endian is defined, which can have two answers: little and big . This could be used to automatically determine whether the processor supports the little or big endian format and to control the compilation accordingly. However, the author's system used revealed that this predicate was undefined:











Predicate undefined.
#include <stdio.h>

int main(){
  #if #endian(big)
    printf("Big Endian\n");
  #elif #endian(little)
    printf("Little Endian\n");
  #elif #endian
    printf("Some kind of Endian\n");
  #else
    printf("Predicate undefined.\n");
  #endif
  return 0;
}