1

What i exactly need to know is what characters are allowed before the start of a directive as we all know we can have new line characters and whitespace characters before the start of a directive before ( # ) now i read the C standard about this and found out the following definition explaining this:

A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a # preprocessing token that (at the start of translation phase 4) is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character. "C standard - read here the definition"

now what i exactly need to know is: what do they mean by

(optionally after whitespace containing no "new-line" characters) or that follows whitespace containing at least one "new-line" character

the

containing no new-line characters

and

containing at least one new-line character

is what i don't understand in the above definition i need to know what that exactly means and i need to know

where new-line characters can occur is it before # token or after # token the C standard haven't stated where new-line characters can occur (it only states "containing no new-line characters" and "containing atleast one new-line character") (it haven't stated whether where new-line characters can occur in this case before # token or after # token) even though it have stated where whitespace characters can occur(before # token) in the above situation

  • 3
    That is C-speak for first non-whitespace character in file or first non-whitespace character in a line. Which boils down to the `#` has the be the first non-whtiespace character on any line in the file to provide a macro definition or pre-processing directive. – David C. Rankin Jun 29 '20 at 00:10
  • 1
    @DavidC.Rankin thank you. can you tell me what they mean from that i mean where actually a **new-line character can occur** – user13104441 Jun 29 '20 at 00:18
  • 1
    In C and `'\n'` is whitespace and largely ignored. However, when you are talking about pre-processing tokens or macro definition, they have to appear as the first non-whitespace character in the line. So that part of the standard is just saying if it's not the first line, you have to have a newline character before the `'#'`. Otherwise, the are largely ignored. – David C. Rankin Jun 29 '20 at 00:29

1 Answers1

3

What this all basically means is that either # is at the start of a line or there is only whitespace before # on a given line.

This part:

either the first character in the source file (optionally after white space containing no new-line characters)

Allows for whitespace before # if it's on the first line of the file, while this part:

or that follows white space containing at least one new-line character.

Allows for whitespace before # on any subsequent line.

For example:

  #include <stdio.h>   // spaces before the first line
  #include <stdlib.h>  // spaces before another line, i.e, spaces and newline before a token
int x;   #include <string.h> // not allowed, other tokens preceed on same line
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Thank You N can you explain me where actually a new line character can occur according to the description of the C standard before # or after # and its not clear what they have stated in the C standard – user13104441 Jun 29 '20 at 00:23
  • 1
    @user13104441 Newlines can appear before a `#`. They can also appear after a complete preprocessor directive. – dbush Jun 29 '20 at 00:29