Search notes:

Predefined preprocessor macros __FUNCTION__, __FILE__, __LINE__

Note, __FUNCTION__ is not really a macro: the preprocessor has now concept of functions and thus doesn't not know in which function it is.

print_function.h

#include <stdio.h>
#define PRINT_FUNCTION(TXT) printf("%-10s %-10s: %4d: %s\n", __FUNCTION__, __FILE__, __LINE__, TXT);
Github repository about-cpp, path: /preprocessor/macros/predefined/__FUNCTION__FILE__LINE__/print_function.h

funcs.c

#include "print_function.h"
#include "funcs.h"

void func_a(const char* txt) {
  PRINT_FUNCTION(txt);
}

void func_b(const char* txt) {
  char buf[100]; // beware the buffer overflows…
  sprintf(buf, "txt = %s", txt);
  PRINT_FUNCTION(buf);
}
Github repository about-cpp, path: /preprocessor/macros/predefined/__FUNCTION__FILE__LINE__/funcs.c

main.c

#include "print_function.h"
#include "funcs.h"

int main() {

  PRINT_FUNCTION("starting up");

  func_a("abc");
  func_b("def");

}
Github repository about-cpp, path: /preprocessor/macros/predefined/__FUNCTION__FILE__LINE__/main.c

funcs.h

void func_a(const char* txt);
void func_b(const char* txt);
Github repository about-cpp, path: /preprocessor/macros/predefined/__FUNCTION__FILE__LINE__/funcs.h

Makefile

all: print-functions

main.o: main.c print_function.h funcs.h
	gcc -c main.c

funcs.o: funcs.c print_function.h funcs.h
	gcc -c funcs.c

print-functions: funcs.o main.o
	gcc funcs.o main.o -o print-functions

Github repository about-cpp, path: /preprocessor/macros/predefined/__FUNCTION__FILE__LINE__/Makefile

See also

Preprocessor: macros
https://github.com/ReneNyffenegger/about-cpp/tree/master/preprocessor/macros/predefined/__FUNCTION__FILE__LINE__
__LINE__, __FILE__ and __DIR__ in PHP.

Index