John Roberts
John Roberts

Reputation: 5986

C Program Not Recognizing Constant From Header File

I have the following really simple header file:

#ifndef __ZYNQ_CSORT_H__
#define __ZYNQ_CSORT_H__
#define CONSTANT    5
#endif

I am including this header file in another C file in the same folder. The preprocessor doesn't complain at all about the header file include, but when I try to print the value of the constant, it tells me that it is not defined. Anybody know what's up?

Upvotes: 0

Views: 1919

Answers (2)

Simon
Simon

Reputation: 10841

When I'm uncertain about what the preprocessor is up to, I find it is often revealing to run the C preprocessor by itself. For example, given test1.h:

#ifndef TEST1_H
#define TEST1_H
/* In TEST1_H */
#define CONSTANT 5
#endif

... and test1.c:

#include "test1.h"
#include "test1.h"

int main(int argc, char **argv) {
    return CONSTANT;
}

... running cpp -C test1.c test1.c.out (the -C argument makes the preprocessor retain comments) gives test1.c.out as follows:

# 1 "test1.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test1.c"
# 1 "test1.h" 1


/* In TEST1_H */
# 2 "test1.c" 2


int main(int argc, char **argv) {
 return 5;
}

Thus, for my case I can be confident that the right header file is being included.

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

CONSTANT would be undefined if __ZYNC_CSORT_H were already defined when this file was parsed.

Upvotes: 0

Related Questions