#error directive format

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

#error directive format

Jump to solution
2,296 Views
Doug_O
Contributor I
I am using the CWHCS12 toolset and am trying to compile some C-code that contains variaous #error directives.  The #error directives contain some phases enclosed by single quote characters.  When compiling this code I get errror C4422 from the compiler.
 
By trial and error I have narrowed it down to the formats of #error directive text strings that work and don't work.
 
// Strings accepted by the compiler include...

#if 0
#error "This 'test' works."
#endif

#if 0
#error This test works.
#endif


// Strings that don't work include...

#if 0
#error This 'test' doesn't work, it gives error C4422.
#endif
 
Does anyone know what the ANSI-C standard says the format of the text strings supported by the #error directive should be?
 
Is the behavior I described above consistent with this standard?
 
Thanks,
--Doug
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
752 Views
CompilerGuru
NXP Employee
NXP Employee
per ANSI C standard, after a #error directive there is an (optional) sequence of preprocessing tokens.
A string literal ("this is a string") is a legal preprocessing token, but also a identifier, a numbers or most characters are legal preprocessing tokens.
Single single quotes(') (or single double quotes (")) are explicitely excluded as legal preprocessing tokens, or to be more precise, if they occur the "behavior is undefined" (ANSI-C terminology).
Can you change your source code so that Mathworks wont issue single quotes?
"Don't" -> "Do not"?
Daniel

View solution in original post

0 Kudos
Reply
3 Replies
752 Views
BlackNight
NXP Employee
NXP Employee
Hi Doug,
the reason why the compiler does not accept
 #errror this 'test'
is because it treats everything after the #error as 'preprocessing' tokens.
If you double quote things after the #error, then it is a 'string' and you can have in it whatever is legal for a typical string in the C language.
If you do not double quote it, it get's a little bit tricky as your example contains single quotes. Typically, with single quotes you specify character constants like 'a'.
Now in your case the compiler rejects it because he does not accept a character constant with more than one character in it.
Checking the standard (ANSI/ISO 9899-1990) on this, in fact the standard would allow multi-character constants like 'test', but it says:
"The value of an integer character constant containing more than one character, or ....., is implementation defined."
So from this perspective, I would say that the compiler is consistent with the standard, but I would expect that the compiler would accept a character constant with more than one character in it (actually it accepts escape sequences and trigraphs in it).
But you still would run into problems if you would just use a single quote like
#error don't do this

I recommend that it would be the easiest thing for you if you double quote the message for the #error directive.

Erich
0 Kudos
Reply
752 Views
Doug_O
Contributor I
The code I am refering to is automatically generated by the Mathworks-RealtimeWorkshop-Embeddedcoder toolset.  So I can't easily change the code and I'm trying to sort out where the issue lies.
 
Per the standard should everything after an #error directive be
 
1) treated as 'preprocessing' tokens
 
or
 
2) be treated as a text string
0 Kudos
Reply
753 Views
CompilerGuru
NXP Employee
NXP Employee
per ANSI C standard, after a #error directive there is an (optional) sequence of preprocessing tokens.
A string literal ("this is a string") is a legal preprocessing token, but also a identifier, a numbers or most characters are legal preprocessing tokens.
Single single quotes(') (or single double quotes (")) are explicitely excluded as legal preprocessing tokens, or to be more precise, if they occur the "behavior is undefined" (ANSI-C terminology).
Can you change your source code so that Mathworks wont issue single quotes?
"Don't" -> "Do not"?
Daniel
0 Kudos
Reply