8

I will have hundreds of functions such as this

void OrganOut() 
{       
    Title("OrganOut");

Where the first line puts the title of the function up on the LCD display (it's an embedded music system as you can probably guess by the name). As the function name is obviously known at compile time, is there any way to automate placing the name in Title to avoid entering the name twice ?

Mike Bryant
  • 310
  • 2
  • 12
  • Great answer below, now is your next question "how can I use my editor to replace "..." with `__func__` "? – Bathsheba Mar 14 '19 at 13:44
  • @Dupe spotters: that question was "wider" than this one in content, and didn't have the C++ tag. Let's stick more faithfully to "exactness". Disk is cheap. And when I research a topic in a library I like to look at more than one book. We wouldn't remove Mark, Luke, and John from the Bible simply because we have Matthew. – Bathsheba Mar 14 '19 at 13:46
  • @Bathsheba Still I dont see the problem with the question I used as dup. Leaving it in comment here: [Print the file name, line number and function name of a calling function - C Prog](https://stackoverflow.com/q/8884335/2173917) – Sourav Ghosh Mar 14 '19 at 13:49
  • @Bathsheba In all fairness this is probably a dupe. I couldn't find one covering C++ at first, but maybe this one? https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c – Lundin Mar 14 '19 at 13:49
  • 1
    @SouravGhosh: Because this one is asked on the C++ tag too. – Bathsheba Mar 14 '19 at 13:49
  • The OP is not asking about C/C++ he is asking the same question for the C and the C++ tag which looks OK to me. – Jabberwocky Mar 14 '19 at 13:50
  • @Bathsheba What if, it was tagged python, too? I thought we tend to limit question to "specific" tags, and `C/C++` is not one of them. – Sourav Ghosh Mar 14 '19 at 13:50
  • @Lundin: I don't understand why we can't have a Q & A site of *specific* questions. Yes, if you study the one you link you have the answer to this question, but then equally we could create a canonical C and C++ standards question and link all questions to that. Clearly that limit is absurd, but where do you draw the line? – Bathsheba Mar 14 '19 at 13:51
  • @SouravGhosh C and C++ are related, C is more or less a subset of C++. Python is not related at all. – Jabberwocky Mar 14 '19 at 13:51
  • 1
    @SouravGhosh: Some questions, such as this one, are an *excellent* fit for both the C and C++ tags. – Bathsheba Mar 14 '19 at 13:51
  • @Jabberwocky That probably indicates, OP does not have a clear idea that C and C++ are different languages and better treated so. – Sourav Ghosh Mar 14 '19 at 13:51
  • @SouravGhosh may be, maybe not. Many questions, including this one, are valid for C and C++ though. – Jabberwocky Mar 14 '19 at 13:53
  • @Bathsheba But the top-voted answer at https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c is good and covers both languages. We could easily make a canonical dupe of that one by slipping in a C tag there. I'm not going to moderate this though since I already posted an answer myself. – Lundin Mar 14 '19 at 13:54
  • @Bathsheba If that's the argument, why not re-tag the other question as C++, too? It'd still be useful, right? – Sourav Ghosh Mar 14 '19 at 13:54
  • @SouravGhosh: Then you risk invalidating all the answers. – Bathsheba Mar 14 '19 at 13:54
  • @Bathsheba How is that? – Sourav Ghosh Mar 14 '19 at 13:56
  • @Lundin: Yes, the accepted answer to that one would not be invalidated. But who cares really? If someone - from this moment onwards - Googles this topic, then multiple answers come up, all with slightly different tags and flavours, and that's helpful. You answer stands out from this universal crop, as does this question, which makes both useful in a Q & A context. Let's leave them all alone. Again, disk is cheap - the idea of consolidating pages to duplicates is, in my opinion, quixotically futile, and in many ways damaging. – Bathsheba Mar 14 '19 at 13:57
  • @SouravGhosh: Because the answers might not address the other language with sufficient explicitness. – Bathsheba Mar 14 '19 at 13:57
  • @Bathsheba Because 1 result of very good quality is better than getting 5 of mediocre quality. – Lundin Mar 14 '19 at 14:00
  • @Lundin: Which is why the vote counts are useful. How else can we really judge quality? – Bathsheba Mar 14 '19 at 14:00
  • That’s sort of like treating a question on callbacks differently in reference to JavaScript vs Typescript. I could understand if the questions required mentioning techniques that differ too greatly to consider an answer that fit both. The truth is, the comment distracts from the question and ultimately is a waste of people’s time. We have learned nothing from distinguishing a difference between the two languages, with respect to this question. Just seems like someone is showing a temper because of ‘c/c++’ in the question. – Mikejg101 Mar 14 '19 at 21:10

1 Answers1

15

You are looking for __func__.

void OrganOut() 
{       
    Title(__func__);
}

This feature is available from the C99 and C++11 standards respectively.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 3
    Yeah it took us C++ guys a few years to realise how useful this could be ;-) – Bathsheba Mar 14 '19 at 13:44
  • 2
    You also have `__PRETTY_FUNCTION__` as available as a gcc extension that includes more information – Clonk Mar 14 '19 at 13:46
  • about 40 years it seems. Thanks for the info ! – Mike Bryant Mar 14 '19 at 13:53
  • Gives "invalid conversion from 'const char*' to 'char*'" error. What do I need to change in Title void Title(char *str) { – Mike Bryant Mar 14 '19 at 13:56
  • 1
    @MikeBryant `__func__` is equivalent to having a local variable `static const char __func__[] = "function-name";`. So if `Title` isn't const-qualified you should change it to become const-correct. Same in C and C++. – Lundin Mar 14 '19 at 13:58
  • @Lundin: I thought it was `static char __func__ ...` in C? Or am I showing my age and inexperience again? – Bathsheba Mar 14 '19 at 13:58
  • 1
    @Bathsheba That code is copy/paste from the C99 standard. You are mixing it up with the type of string literals in C which is indeed `char[]`, unlike in C++. That is, in C `"function-name"` is of type `char[]` but `__func__` is of type `const char[]`. – Lundin Mar 14 '19 at 14:01
  • @Lundin: Interesting, thank you. Been a long time since I've programmed in C. No job is perfect ;-) – Bathsheba Mar 14 '19 at 14:02
  • Yes that works thanks. As for other comments, whilst some may write in pure C or pure C++, I can assure you that many of us real-time people write in a hybrid and so C/C++ seems to be a valid tag, even if it's looked down upon by purists. I hate to think what speed my DSP music generation code would run at using pure C++, or even it it's repeatable. Google for VST programming if you need more evidence of hybrid C/C++ in action. – Mike Bryant Mar 14 '19 at 14:05
  • 1
    @MikeBryant Real-time programmers avoid C++ like the plague. There is no such thing as "hybrid", if you mix the languages you are writing C++. And then you'll get stuff like static storage objects getting constructed during system boot-up and other such cases of C++ lag. – Lundin Mar 14 '19 at 14:09
  • @MikeBryant google says you can choose a language for VST programming and the suggested language is C++. – mch Mar 14 '19 at 14:09
  • Yes the suggested language is C++ because VST is itself a sort of hybrid. C++ makes programming the GUI more elegant, but the real time DSP code is almost always C using lots of things C++ purists would be horrified at to get that extra instruction in every 10.4 uS (96kHz) – Mike Bryant Mar 14 '19 at 14:16
  • @MikeBryant You might be writing some translation units in C++, and others in C, and linking the resulting object files, but you are only ever writing in one language at a time. – Caleth Mar 14 '19 at 14:58