Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-processor parsing on C++

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?

like image 868
Mario Zelic Avatar asked Jan 28 '17 15:01

Mario Zelic


People also ask

What is a preprocessor in C language?

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).

What are the preprocessor directives in C?

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. ...

What are the features offered by the C preprocessor?

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:

How to remove all comments in a C program using preprocessor?

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.


3 Answers

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;}
like image 95
πάντα ῥεῖ Avatar answered Oct 20 '22 12:10

πάντα ῥεῖ


You define PRINT_DEBUG_INFO but you don't use it so there is nothing for the compiler to compile or complain about.

like image 45
Ian Miller Avatar answered Oct 20 '22 13:10

Ian Miller


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.

like image 2
Anthony Vinay Avatar answered Oct 20 '22 13:10

Anthony Vinay