Are constant C expressions evaluated at compile time or at runtime?

Macros are simply textual substitution, so in your example writing TIMER_100_MS in a program is a fancy way of writing 32768 / 10. Therefore, the question is when the compiler would evaluate 32768 / 10, which is a constant integral expression. I don’t think the standard requires any particular behavior here (since run-time and compile-time … Read more

Predefined Macros for function name __func__

You’re assuming __func__ is a macro, but it’s not. It’s a conditionally-supported predefined identifier, so you can’t check it with #if defined or #ifdef. If the compilers have no way of telling you whether this is supported (they could via a _FUNC_SUPPORTED or something, I’m not saying they actually are doing this), you’ll have to … Read more

Can you #define a comment in C?

No, you can’t. Comments are removed from the code before any processing of preprocessing directives begin. For this reason you can’t include comment into a macro. Also, any attempts to “form” a comment later by using any macro trickery are not guaranteed to work. The compiler is not required to recognize “late” comments as comments. … Read more

What is the difference between a preprocessor macro with no arguments, and one with zero arguments

Replacement only occurrs for a function-like macro if the macro name is followed by a left parenthesis. So, the following all invoke the function-like macro MY_MACRO(): MY_MACRO() MY_MACRO ( ) MY_MACRO ( ) But this would not: MY_MACRO SomethingElse It depends on how you are using the macro and what it is used for as … Read more

Is there a way to both check a macro is defined and it equals a certain value at the same time

This may not work for the general case (I don’t think there’s a general solution to what you’re asking for), but for your specific example you might consider changing this sequence of code: #if(DEBUG_PRINT == 1) printf(“%s”, “Testing”); #endif to: if (DEBUG_PRINT == 1) { printf(“%s”, “Testing”); } It’s no more verbose and will fail … Read more

strlen in the C preprocessor?

It doesn’t use the preprocessor, but sizeof is resolved at compile time. If your string is in an array, you can use that to determine its length at compile time: static const char string[] = “bob”; #define STRLEN(s) (sizeof(s)/sizeof(s[0])) Keep in mind the fact that STRLEN above will include the null terminator, unlike strlen().

C# preprocessor differentiate between operating systems

What you are asking for is possible but needs a bit of work. Define a preprocessor variable in your csproj <PropertyGroup Condition=” ‘$(OS)’ == ‘Windows_NT’ “> <DefineConstants>_WINDOWS</DefineConstants> </PropertyGroup> Use that in your code #if _WINDOWS // your windows stuff #else // your *nix stuff #endif I find this technique useful when you have constants that … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)