If we want to use user input to do something in a program, or print a result we need to
#include <iostream>
otherwise, cout
and cin
will not be acknowledged by the compiler.However the command #include
is a pre-processor command. And when I was writing my program the following happened. I wrote the following code :
#define PRINT_DEBUG_INFO(a) {cout << “Info: ” << a << endl;}
#include <iostream>
And no errors popped up.How is it possible to use cout
before including iostream
? Even if I declare the PRINT_DEBUG_INFO(a)
without including iostream
, I don't get a compiling error.
Can somebody explain me why this happens?
In C, the preprocessor is not a part of the compiler instead which is used to transform the code before its compiled. This is also called a macro processor because it helps you to define the code in short names called as a macro. In C, preprocessors provide few commands which begin with # (hash symbol).
Preprocessors in C 1 Macros in C. Programmers use macros to execute pieces of code inside the macro. ... 2 File Inclusion. This preprocessor directive tells the compiler to include a file in the program code. ... 3 Conditional Compilation in C. ... 4 Other directives in C. ...
The features offered by the C preprocessor are also known as ‘Preprocessor Directives’. These directives are preceded by a ‘#’ symbol. Here we have the following Preprocessor Directives:
So, preprocessor removes all of them as they are not required in the execution and won’t be executed as well. Write a C code (let the file name be prog.c ). Preprocess it using the command gcc -E prog.c You will see the output with the code having no comments. This file is saved with a ‘.i’ extension (prog.i) which will be input to the compiler.
The preprocessor doesn't require any C++ declared symbols to be evaluated to do its work.
It's pure text processing, so defining a macro like
#define PRINT_DEBUG_INFO(a) {cout << “Info: ” << a << endl;}
and expanding it like
#include <iostream>
void foo {
int a = 5;
PRINT_DEBUG_INFO(a);
}
will become
// All the literal stuff appearing in <iostream>
void foo {
int a = 5;
{cout << “Info: ” << a << endl;};
}
So there's nothing checked regarding proper C++ syntax during definition or expansion of the macro.
These statements will be processed further by the C++ compiler, which will complain about cout
isn't declared in the global scope.
To fix this, declare your macro like
#define PRINT_DEBUG_INFO(a) {std::cout << “Info: ” << a << std::endl;}
You define PRINT_DEBUG_INFO but you don't use it so there is nothing for the compiler to compile or complain about.
You are just defining PRINT_DEBUG_INFO(a)
and not using it. When you actually use it inside your program you will get the error that cout
is not defined.
When you are not actually using it, the compiler finds no place to substitute the defined constant. When you actually use it, the program gets expanded during compilation and shows you the error.
And moreover there is a bracket in your macro which gets expanded with brackets and may lead to error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With