seriypshick said:
The souce file (Ex. test.c)
Basically what i want to do is to print line number plus some other info for every line, or after each ; (semicolon).
Code:
#define ; {printf("Line # is %i", line_number_function()}
This will not work since ";" is not a valid identifier for a #define.
Also, ";" is not a line delimiter it is a statement seperator so you will find that it is difficult to follow which particular line is outputing this statement.
I'd like to echo another posters comment that this is a very odd thing to be trying. I suspect you are hoping that this will make it easier to do debugging since your code would output line comments with every line. Unfortunately this stort of thing would be confusing to follow and output too much information. You are much better off trying to reason about what your program is doing, if necessary starting from the first line of the main method and stepping forward slowly. Getting familiar with a good debugger is also helpful. DDD although an X11 application is an excellent C debugger.
Having said all that here are two files that do almost what you want: (compile the line_number.c file first)
Code:
/*
* File: line_number.c
* Compile this file with gcc -c line_number.c
*/
int linenum = 0;
extern int line_number() {
return linenum++;
}
Code:
/*
* File: test.c
* Compile this file with gcc -o linenum test.c line_number.o
*/
#define LINNUM printf("Line # is %i\n", line_number());
extern int line_number();
main() {
int foo=0; LINNUM
while(foo<10) { LINNUM
foo++; LINNUM
}
}
Notice how normally you would not get output from the while statement line since there is no semi-colon used on the while statement. This is one example of how altering the meaning of ";" would not help you, even if it was a valid #define identifier.