Here is a small example that uses between/3.
#include <stdio.h>
#include <SWI-Prolog.h>
int main()
{
char *argv[] = {"hello", "-q"};
PL_initialise(2, argv);
term_t a = PL_new_term_refs(3);
PL_put_integer(a, 11); // First argument to between/3
PL_put_integer(a+1, 17); // Second argument to between/3
static predicate_t p;
if(!p)
p = PL_predicate("between", 3, NULL) ; // predicate between/3
qid_t q = PL_open_query(NULL, PL_Q_NORMAL, p, a);
int tmp;
while(PL_next_solution(q)==TRUE){
PL_get_integer(a+2, &tmp); // Third argument to between/3
printf("%d\n", tmp);
}
PL_close_query(q);
return 0;
}
Compile it using swipl-ld and execute
swipl-ld -o between between.c
./between
11
12
13
14
15
16
17
Another example if you want to load a file and then do a query.
#include <stdio.h>
#include <SWI-Prolog.h>
void consult_file(const char* filename){
/* PL_initialse before calling. */
predicate_t p = PL_predicate("consult", 1, NULL);
term_t a = PL_new_term_ref();
PL_put_atom_chars(a, filename);
PL_call_predicate(NULL, PL_Q_NORMAL, p, a);
}
int main()
{
char* argv[] = {"consult_file", "-q"};
PL_initialise(2, argv);
consult_file("digits.pl"); /* Load prolog file */
static predicate_t p;
/* third argument is module name if the predicate is in one.*/
if(!p) p= PL_predicate("digit", 1, NULL);
int tmp; term_t a = PL_new_term_ref();
qid_t q = PL_open_query(NULL, PL_Q_NORMAL, p, a);
while(PL_next_solution(q)==TRUE){ /* All solutions */
PL_get_integer(a, &tmp);
printf("%d ", tmp);
}
return PL_halt(0);
}
With digits.pl file as follows we get the three numbers 1 2 3 as output.
digit(1).
digit(2).
digit(3).