LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Beginning C programming- exceptions to whitespace irrelevant? include directive (https://www.linuxquestions.org/questions/programming-9/beginning-c-programming-exceptions-to-whitespace-irrelevant-include-directive-926974/)

keithostertag 02-01-2012 12:06 PM

Beginning C programming- exceptions to whitespace irrelevant? include directive
 
New to programming. In several texts and online resources I have read that whitespace is irrelevant in C (except of course with strings).

Today I ran across the fact that my compiler choked on the following form of an include directive:

Code:

#include < stdio.h>
A space before or after the header filename produces a fatal compile error. I have never seen this mentioned (yet).

Any other exceptions to "whitespace irrelevant" you know of? I'm using Debian with current gcc.

Thanks,
Keith Ostertag

Doc CPU 02-01-2012 12:33 PM

Hi there,

Quote:

Originally Posted by keithostertag (Post 4590717)
New to programming. In several texts and online resources I have read that whitespace is irrelevant in C (except of course with strings).

that's true, AFAIS.

Quote:

Originally Posted by keithostertag (Post 4590717)
Code:

#include < stdio.h>
A space before or after the header filename produces a fatal compile error. I have never seen this mentioned (yet).

This is -strictly speaking- not a C issue. The directives starting with a hash sign are parsed by the preprocessor, which used to be a separate tool in earlier compiler designs. Today, it's usually integrated in the compiler. Yet the preprocessing is a pass of its own, formally.

Anyway ... In your example, the < and > characters are considered string delimiters, and so the contents they enclose is a string. Note that you may use quotes instead of the < > and then you wouldn't have seen this as an exeption, would you?
However, there is a difference between using quotes and angle brackets with #include. If you use angle brackets, the compiler searches for the included file in its pre-configured include directories. If you use quotes, it searches in the source file's directory first, and then in the include directories.

[X] Doc CPU

keithostertag 02-01-2012 12:41 PM

Thank you for an informative response!

jthill 02-01-2012 01:06 PM

You might like the GNU C Preprocessor and the GNU C Reference Manual. For actual mastery you can get final draft standards free at www.open-std.org. Wrapping your head around the structure and detail of tho standards demands the kind of thinking that will serve you _very_ well professionally, but don't try them until you feel you have a real idea of what compilers and linkers and loaders do.


All times are GMT -5. The time now is 10:23 AM.