While trying to include a custom header file, I am witnessing the following error in my C code - "warning: extra tokens at end of #include directive" although even after throwing the error, the program seems to work as intended.
But why is the warning message occurring? What's the significance?
The code:
#include <stdio.h>
#include "sumOfTwo.h";
int main() {
int a = 0;
printf("Enter a: ");
scanf("%d", &a);
int b = 0;
printf("Enter b: ");
scanf("%d", &b);
printf("%d %d = %d", a, b, sumOfTwoNumbers(a, b));
printf("\n");
return 0;
}
The header file:
int sumOfTwoNumbers(int a, int b) {
return a b;
}
What would be the reason behind the warning? The code and the header file is mentioned as above.
CodePudding user response:
Main Issue
The ;
is an extra token at the end of the #include
directive. Remove it.
The syntax of a #include
directive with the quote-mark form is, roughly, “#include
"
sequence-of-characters "
”. There is no semicolon at the end.
Supplement
… although even after throwing the error,…
This is not “throwing” an error. The compiler prints a message and continues. Throwing an error means generating an exception: Normal program control flow is interrupted and transferred to an exception handler, or, if there is no handler, the program is terminated.
… the program seems to work as intended.
Once the compiler recognized the unexpected token, it ignored it.
The header file:…
Do not define functions inside header files, except for very small or necessary static inline
functions. Header files should be used mostly just for declarations that are not definitions of functions or objects.
Functions and objects should be defined in source files, which are compiled and linked together. Header files should be used to make functions and objects defined in one source file known in other source files that need them.