c++ – What is the difference between include and declaration

Question: Question:

For example, in the sentence below

#include<iostream>
#include<cmath>

float Myabs(float x);

int main(){
    float a=2.5;
    std::cout<<Myabs(a)<<std::abs(a);
    a=-5.25;
    std::cout<<Myabs(a)<<std::abs(a);
    return 0;
}

float Myabs(float x){return x>0?x:-x;}

If you change this sentence like the two examples below, Example 1

#include<iostream>

float Myabs(float x);

int main(){
    float a=2.5;
    std::cout<<Myabs(a)<<std::abs(a);
    a=-5.25;
    std::cout<<Myabs(a)<<std::abs(a);
    return 0;
}
#include<cmath>
float Myabs(float x){return x>0?x:-x;}

Example 2

#include<iostream>
#include<cmath>

int main(){
    float a=2.5;
    std::cout<<Myabs(a)<<std::abs(a);
    a=-5.25;
    std::cout<<Myabs(a)<<std::abs(a);
    return 0;
}
float Myabs(float x);
float Myabs(float x){return x>0?x:-x;}

Both give an error. An error that occurs because there is no declaration and an error that occurs because there is no include.
Does this indicate that the include statement and the declaration behave the same from the compiler's perspective?

Answer: Answer:

Try compile option -E . The source code processed by the preprocessor is output. The source code output by this is the source code of C language or C ++ language. You can see that preprocessor instructions such as #include and #define #if #endif have been processed and disappeared.

Scroll to Top