0

In the code below, I don't understand the defined() function used inside #if; where is it defined?

Can anyone point me to a good resource in C language, where I could go deeper in these kinds of kinds of stuff?

#include <stdio.h>
#define Macro 7

void initMSP(void){
    printf("OKay with MSP platform\n");
}

void initKine(void){
    printf("Done with Kine\n");
}

//#define KINETICS  
#define MSP

int main(){

    printf("Hello world program\n");
    printf("%d\n",Macro);
    #if defined(KINETICS) && !defined(MSP) 
            initKine();
    #elif defined(MSP) && !defined(KINETICS)  
        initMSP();
    #else 
        #error "Please define a Platform "
    #endif  
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Electro Voyager
  • 107
  • 1
  • 8

1 Answers1

2

defined is not a function. It is a syntactic construct of the C preprocessor, just like #define, #ifdef, and so forth. The C language proper (to the extent that you can divorce C from its preprocessor) never directly interacts with defined. It exists during preprocessing and that's that.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks a lot to everybody. Can you suggest where you people learned all those things. I don't see a single resource which explains how C programming language works. So can you guys point me to resource where inner working of C language is explained in depths. – Electro Voyager Mar 30 '20 at 03:50
  • 2
    It's tough going in places, but the (draft) [C11 standard](http://port70.net/~nsz/c/c11/n1570.html) is one place ([§6.10 Preprocessing directives](http://port70.net/~nsz/c/c11/n1570.html#6.10) for the C preprocessor). You could look at the books in [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/15168) — the title is over-claiming its status, but it gives a number of good books to look at. – Jonathan Leffler Mar 30 '20 at 04:07
  • @ElectroVoyager: "*how C programming language works*" How would you even begin to explain that? It's like asking "how does chemistry work?" There are layers and levels to it, complexities that you don't just throw at a neophyte, yet will matter a great deal at some point. – Nicol Bolas Mar 30 '20 at 04:13
  • 1
    @ElectroVoyager: The [Gnu preprocessor manual](https://gcc.gnu.org/onlinedocs/cpp/index.html#SEC_Contents) is pretty readable, although it doesn't always clearly distinguish between standard C and GCC extensions. But that's less important than it used to be. Of course, it just covers the preprocessing phases. Here's what it says about [defined](https://gcc.gnu.org/onlinedocs/cpp/Defined.html#Defined) – rici Mar 30 '20 at 05:06