expected a ")" in IAR IDE - c

I got the error Error[Pe018]: expected a ")" at CPU_state == cpuStateOff.
When I change cpuStateOff to 0 it's OK. I don't know why.
In my PERIPHERAL_APP.h:
#ifndef __PERIPHERAL_APP_H
#define __PERIPHERAL_APP_H
// CPU state
#define CPU_STATE_OFF 0;
#define CPU_STATE_ON 1;
#endif
In my main.c:
#include "PERIPHERAL_APP.h"
void main( void )
{
initMSP430();
_EINT();
for (;;)
{
if (cpuState == CPU_STATE_OFF ) // The error is hear
{
__bis_SR_register(LPM3_bits);
}
else
{
__bis_SR_register(LPM0_bits);
}
}
}

Preprocessor macros are not C statements, and therefore doesn't need a statement terminator like ;.
What happens is that when the preprocessor replaces the macro cpuStateOff it uses the whole body of the macro, i.e. 0; which results in code like
if (CPU_state == 0; ) // Code after macro replacement
Most compilers and environments have options to stop after preprocessing, if you use it you can see exactly what code the compiler "proper" will see.

Related

How to use a global variable in #if conditional check?

#define TEST
int i = 1;
int main(int argc, char const *argv[]) {
#if (defined(TEST) && i)
printf("it is printed");
#endif
return 0;
}
Do I have to create another macro for i?
If you want the output if and only if both the macro and the condition on the global variable are true then do like this:
#if (defined(TEST))
if (i)
{ printf("it is printed");
}
#endif
Reason (agreeing with comment by Barmar):
Preprocessor directives are evaluated at compile time, variables are not available at that time.
As in the comments, macro definitions are expanded during the preprocessing stage, before the actual C code compilation.
But why this code compiles and what will be the result of it.
#if (defined(TEST) && i)
preprocessor tests for macro definition TEST. As it was defined, the result of this check is true
now the preprocessor is checking if macro definition (not the C variable!) i exists. As it does not, the whole expression in the #if is evaluated to false.
Generally :
#if a
checks if macro definition a exists and then if its value is != 0,

Conditional compilation error with macros

#include<stdio.h>
#define NUM 10
main()
{
#ifdef NUM
printf("compilation succesfull");
#endif
}
The above code compiles perfectly fine and upon execution prints
compilation successful, but if I remove a blank line before the first statement in main()
it gives an error. ie. "stray #" in the program and many other errors.
#include<stdio.h>
#define NUM 10
main()
{ #ifdef NUM
printf("compilation succesfull");
#endif
}
Can any one help me?
Preprocessor statements need to be on their own lines. So you can't put it on the same line as a { or anything else that's not part of the statement.
The preprocessor is unable to understand the line:
{ #ifdef NUM
The compiler doesn't know how to handle the statement #ifdef NUM, it'll say # is unknown and unknown type ifdef.
it'll reach the line
#endif
but didn't see ifdef before, thus you'll get #endif without #if error.
When you replace it in a new line, then the preprocessor knows what to do and the output will be:
main()
{
printf("compilation succesfull");
}
Validate this by running gcc -E yourprogram.
The Tag #ifdef you are using is correct. In the C language, lines that start with # in the code are called preprocessor directives. The preprocessor deletes them and uses them as commands to modify the code file prior to compilation.
For such statement for preprocessor, it should start in a new line, and only white space is allowed before the #. You should not put it after {.
For your code, compiler will like it, if it could be arranged as following.
#include<stdio.h>
#define NUM 10
main()
{
#ifdef NUM
printf("compilation succesfull");
#endif
}

String comparison in preprocessor conditions in C

I am passing a compiler option in makefile called DPATH, which is something like DPATH=/path/to/somefile. Based on this, I have to write a macro such that:-
#if "$(DPATH)"=="/path/to/x"
#error no x allowed
#endif
How do I compare DPATH with the string in a preprocessor conditional test?
It is not possible to do this in the preprocessor. #if can only evaluate integer expressions making no reference to functions or variables. All identifiers that survive macro expansion are replaced by zeroes, and a string constant triggers an automatic syntax error.
Without knowing more about your problem, I would suggest writing a tiny test program that is compiled and executed during the build, and Makefile goo to fail the build if the test doesn't pass.
#include <stdio.h>
#include <string.h>
int main(void)
{
if (!strcmp(DPATH, "/path/to/x") || some1 == 3 || some2 == 7 || ...)
{
fputs("bogus configuration\n", stderr);
return 1;
}
return 0;
}
and then
all : validate_configuration
validate_configuration: config_validator
if ./config_validator; then touch validate_configuration; else exit 1; fi
config_validator: config_validator.c
# etc

Correct #if functionality

If I define
#if SOMETHING
#endif
and I have not defined SOMETHING anywhere. Would the code inside #if compile ?
When the name used in the argument expression of #if is not defined as a macro (after all other macro replacement is finished), it is replaced with 0. This means that your #if SOMETHING will be interpreted as #if 0. The code under #if will be removed by preprocessor.
The rule applies to more complex expressions as well. You can do
#define A 42
#if A + B
which will evaluate to #if 42, since unknown name B is interpreted as 0
No, as long as SOMETHING is also not one of the predfined macros or a macro passed on the commandline to the compiler.
I think in C99 it defaults to 0 while in C89 it does not as here its giving me an error
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define A 42
#define B
int main(int argc, char *argv[]){
int c;
#if A
c = A + B;
#endif
printf("%d\n",c);
return 0;
}
Error :
error: expected expression before ‘;’ token
No, because as an undefined macro, it would not necessarily be replaced with anything... but that is probably compiler specifc, and not to be trusted.
You may want to use:
#ifdef DEFINED_WORD
#ifndef DEFINED_WORD
Where DEFINED_WORD is declared by a #define, like: #define DEFINED_WORD. The ifdef construct ONLY compiles the code between that and the #endif if DEFINED_WORD was defined. The opposite is true of #ifndef. You can nest them like so:
#ifdef DEFINED_WORD
printf ( "DEFINED_WORD is defined\n" );
#ifndef DEFINED_WORD
printf ( "This line will NEVER print\n" );
#endif
printf ( "This line will print if DEFINED_WORD is defined\n" );
#endif
printf ( "This line will ALWAYS print.\n" );
In my code, I have constructs like:
#ifdef DEBUG
printf ( "array[%d] = %d\n", i, array[i] );
#endif
To deactivate all of the DEBUG code, just comment out the line that you MUST have before calling that #ifdef (top of the source file, or in an included header which you included at the top of the source file, in good form). #define DEBUG
Hope that helps.

Return with Macro C programming

This code is always returning -1 even when the fopen() function has executed successfully. Is there something I am ignoring.
void nullCheck(FILE* checkVar) {
if(checkVar==NULL) {
#define _NULL_ERROR
}
}
int readFile(char* _name, char* storeArray) {
FILE* fp;
fp=fopen(_name,READ_ONLY_MODE);
nullCheck(fp);
#ifndef _NULL_ERROR
char c=0;
while((c=getc(fp))!=EOF) {
*(storeArray+i)=c;
i+=1;
}
#endif
#ifdef _NULL_ERROR
#undef _NULL_ERROR
return -1;
#endif
return 1;
}
Thanks!
Oy va voy! Macros are defined and undefined when your code is compiled, not when it runs! They are not affected by control flow statements like "if" and "then" -- they are all processed before compilation of those statements even begins!
You need to re-read the documentation on the C Preprocessor. The #define _NULL_ERROR doesn't get executed when nullCheck is called, it gets interpeted when the preprocessor processes the file before it is compiled. So you are always setting _NULL_ERROR and so you will always return -1.
#define is a preprocessor command, which means it's not calculated / processed in the function nullCheck() but before the compiling of the code. so _NULL_ERROR is always defined and therefore the condition
#ifdef _NULL_ERROR
#undef _NULL_ERROR
return -1;
#endif
will always cause the pre-compiler to add the return -1; to your code.
This is what your code looks like to the compiler, after the preprocessor runs:
void nullCheck(FILE* checkVar) {
if(checkVar==NULL) {
}
}
int readFile(char* _name, char* storeArray) {
FILE* fp;
fp=fopen(_name,READ_ONLY_MODE);
nullCheck(fp);
return -1;
return 1;
}
As has been stated above, the preprocessor deals with macros before the compiler runs.
#define, #ifdef, and friends don't work like you think they do. They're preprocessor directives that affect your code even before the compiler sees it. Get your compiler to show you the preprocessed source before it compiles (-E or -save_temps in gcc and clang) and you'll immediately see what's going on.

Resources