-2

I Have included all the files in the codeblocks while making simple cosole C Application but this Error come when i define the macro. i guess linker is not able to link all files in project .i have two files included in project test1.c and test1.h code in files is as shown below..

file :::::test1.c

#include<stdio.h>
void m();
#include "test1.h"
#define DS==1

int main(){




return 0;
}

file::::test1.h

#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED

#if DS ==1
void m(){

printf("hello DS==1");

}

#eliif DS==2
void main(){
printf("hello DS==2");

}
#endif


#endif // TEST1_H_INCLUDED

Error is that

**> E:\Documents\Myprojects\My C PRoj\Te\test1.c||In function 'main':|

E:\Documents\Myprojects\My C PRoj\Te\test1.c|8|warning: implicit declaration of function 'm'|
obj\Debug\test1.o||In function `main':|
E:\Documents\Myprojects\My C PRoj\Te\test1.c|8|undefined reference to `m'|
||=== Build finished: 1 errors, 1 warnings ===|**

if i remove the conditional macro an compile simply with following code:

file:::::test1.c

#include<stdio.h>
#include "test1.h"


int main(){

m();


return 0;
}

file:::::test1.h

#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED

void m(){

printf("uncoditional macro");

}

#endif // TEST1_H_INCLUDED

every thing works fine .what is reason for that?

  • What don't you understand ? – nouney Jul 01 '13 at 13:34
  • 1
    possible duplicate of [Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...)](http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc) – Pascal Cuoq Jul 01 '13 at 13:36
  • `c=a++ + (a+b);` will result in 4 as a+b=3 and that is added to the value a++ i.e 1( post increment will assign first and then increment. `c=arr[++b]` is like arr[3] which is equal to 4 so 4 gets printed and the first statement is described in one of the answers. – 0decimal0 Jul 01 '13 at 13:49
  • @PHIfounder No, `a++ + (a+b)` invokes undefined behavior too. – Pascal Cuoq Jul 01 '13 at 14:00
  • @PascalCuoq oh yeah yeah .... I got it, I didn't notice.sorry! OP should use a debugger. – 0decimal0 Jul 01 '13 at 14:04

1 Answers1

3

The expression ++a+( a+b) is undefined behavior because a sequence point is missing between the update of a and its use. The output can be anything.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • It is correct, but is not an explanation. – Elazar Jul 01 '13 at 13:31
  • @Elazar This was only intended as a quick answer while the question would get closed as a duplicate. I am disappointed to see voters have so far voted to close the question as off-topic, which doesn't make sense. It is a programming question, and it has been asked and answered countless times on SO. – Pascal Cuoq Jul 01 '13 at 13:34
  • @PascalCuoq okay i apologize to put duplicate question but plzz tell mee the a[++b] is also shows undefined behaviour becoz [] has more precedence than ++. i mean a[b] should de evaluated first then ++b – Durgesh K. Singh Jul 01 '13 at 14:17
  • @DurgeshK.Singh `arr[++b]` is defined and should return `4`. “Precedence” does not come into the evaluation of `arr[++b]`, which there is only one way to parse. `++b` evaluates to `3` and `arr[3]` is `4`. – Pascal Cuoq Jul 01 '13 at 14:42
  • @DurgeshK.Singh Precedence determines how the linear program is transformed into a tree (Abstract Syntax Tree). The order of evaluation of sub-expressions is determined by sequence points. The two are mostly unrelated. In `f()+g()*h()`, precedence determines that the expression is parsed as if it was `f()+(g()*h())` but it says nothing on the order in which `f`, `g` and `h` are called. – Pascal Cuoq Jul 02 '13 at 07:31