Greetings!
I'm doing a C function that allows to obtain a more detailed record of what happens in the program, something similar to a debug , but trying to summarize things for the programmer.
I found the Preprocessor documentation CPP (GNU / Linux) and there indicated the different macros and constants that can be used in our program so that it is also can use functions and use these macros .
Thanks to this, carry out the following program:
#include <stdio.h>
#include <stdlib.h>
/* Dump para variables */
#define dump(variable) { \
printf("La variable '%s' ocupa %d bytes\n", #variable, sizeof(variable)); \
}
/* Dump para funciones */
#define dumpf(){ \
printf("Se llama a funcion %s()\n", __FUNCTION__); \
}
void funcion(int argumento){
dumpf();
printf("%d\n", argumento + argumento);
}
void sumar(int a, int b){
dumpf();
printf("%d + %d = %d\n", a, b, (a+b));
}
int main(int argc, char *argv[]){
/* Variables */
int variableINT = 45;
char variableCHAR = 'A';
char *variablePTR = "Hola Mundo";
/* Debug de Variables*/
dump(variableINT);
dump(variableCHAR);
dump(variablePTR);
/* Debug de Funcion*/
funcion(10);
sumar(5, 5);
return EXIT_SUCCESS;
}
This program allows us to know the weight in bytes of a variable thanks to the dump () function, it also lets us know what function we are calling by means of the < em> dumpf () .
Up to this point, the result of the console program is:
The variable 'variableINT' occupies 4 bytes
The variable 'variableCHAR' occupies 1 bytes
The variable 'variablePTR' occupies 4 bytes
Function is called function ()
20
It is called to add function ()
5 + 5 = 10
After that, to make a little more readable and easier to understand our "debug" , I wanted to add something that would indicate the line where the call to the function was being made, so So I made the following modification in the dumpf () :
/* Dump para funciones */
#define dumpf(){ \
printf("Se llama a funcion %s() en la linea %d\n", __FUNCTION__, __LINE__); \
}
I added the macro LINE , which in theory tells us the line where the instruction is running.
But the result of the program is:
Function () is called on line 15
20
It is called to add function () in line 20
That is, it indicates the line where the function was originally called within each of the functions funcion () and sumar () , it does not indicate the line where It was really called the function, which is actually lines 37 and 38.
How would it be possible to obtain the line where the function call was made? Is there any problem with the LINE macro when included in a function? Since the Preprocessor is supposed to replace these values before compiling.
I thank you in advance for your help.