Reputation:
I know this question has been asked elsewhere but reading the already given answers is not helping me. My code is ridiculously simple yet I cannot compile it. I'm writing code to build a stack.
Here is my stack.h
:
#ifndef GUARD
#define GUARD
struct Stack {
struct Stack* next;
int data;
};
extern bool isempty (struct Stack*);
#endif
here is my stack.c
:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
bool isempty (struct Stack* st) {
return (st == NULL);
}
The compiler keeps on complaining about this line:
extern bool isempty (struct Stack*);
The contents of my main.c
are irrelevant (for now it just returns 0). Does anyone understand what I am doing wrong?
Upvotes: 0
Views: 970
Reputation: 255
If you are comparing St and NULL ... result always will be 0 or 1 ... which is not of boolean type in C .. You should write return type of isempty() as int ...
Upvotes: 0
Reputation: 399803
The type bool
is generally not available in C.
If your compiler supports C99, you can add
#include <stdbool.h>
to make it available, though.
UPDATE: Use of C99 is, in my opinion, not exactly pervasive in C programs "in the large", no. Most boolean values, such as function returns, are represented as int
, which is how it has been solved classically. For arrays, you'd be quite likely to find them represented as bits packed into e.g. unsigned int
, rather than e.g bool a[32];
.
But, in an interview context, I think it would be pretty nice for a candidate to write the above without flinching. It was standardized 13 years ago, after all.
Upvotes: 7