C/C++ line number

You should use the preprocessor macro __LINE__ and __FILE__. They are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding an integer representing the current line number and by the current file name. Others preprocessor variables : __func__ : function name (this is part of … Read more

Including one C source file in another?

Used properly, this can be a useful technique. Say you have a complex, performance critical subsystem with a fairly small public interface and a lot of non-reusable implementation code. The code runs to several thousand lines, a hundred or so private functions and quite a bit of private data. If you work with non-trivial embedded … Read more

What is the purpose of a single pound/hash sign (#) on its own line in the C/C++ preprocessor?

A # on its own on a line has no effect at all. I assume it’s being used for aesthetic value. The C standard says: 6.10.7 Null directive Semantics A preprocessing directive of the form # new-line has no effect. The C++ standard says the same thing: 16.7 Null directive [cpp.null] A preprocessing directive of … Read more

Standard alternative to GCC’s ##__VA_ARGS__ trick?

There is an argument counting trick that you can use. Here is one standard-compliant way to implement the second BAR() example in jwd’s question: #include <stdio.h> #define BAR(…) printf(FIRST(__VA_ARGS__) “\n” REST(__VA_ARGS__)) /* expands to the first argument */ #define FIRST(…) FIRST_HELPER(__VA_ARGS__, throwaway) #define FIRST_HELPER(first, …) first /* * if there’s only one argument, expands to … Read more

C/C++ macro string concatenation

If they’re both strings you can just do: #define STR3 STR1 STR2 This then expands to: #define STR3 “s” “1” and in the C language, separating two strings with space as in “s” “1” is exactly equivalent to having a single string “s1”.

How can I concatenate twice with the C preprocessor and expand a macro as in “arg ## _ ## MACRO”?

Standard C Preprocessor $ cat xx.c #define VARIABLE 3 #define PASTER(x,y) x ## _ ## y #define EVALUATOR(x,y) PASTER(x,y) #define NAME(fun) EVALUATOR(fun, VARIABLE) extern void NAME(mine)(char *x); $ gcc -E xx.c # 1 “xx.c” # 1 “<built-in>” # 1 “<command-line>” # 1 “xx.c” extern void mine_3(char *x); $ Two levels of indirection In a comment … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)