No, it is not guaranteed by the standard. The standard says this about "string literals" (6.4.5p6):
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values.
These arrays refers to the array of char
created from a literal string in translation phase 7.
Since you are using a macro, the code seen by the compiler is:
if( strstr("plaintext", optarg) == "plaintext" )
When optarg
is "plaintext"
, the code reduces to
if("plaintext" == "plaintext")
As mentioned above, this is not guaranteed to be true in C.
So, you must use strcmp()
instead of checking the pointers for equality, or, as in the other answer, define a char *
pointer to use instead of the macro.