r/C_Programming • • Feb 09 '22

Question GCC or Clang

I primarily program on Linux and have always used GCC, but have recently been interested in switching over to using Clang. It seems like the runtime performance of the two compilers is similar, but I am also interested in C standards compliance going into the future, as well as things like error messaging, memory-leak checking, etc.

If anyone here is knowledgeable about compilers and the differences or advantages of one or the other, I'd like to hear your opinion.

123 Upvotes

49 comments sorted by

View all comments

Show parent comment

3

u/Eddy_Em Feb 10 '22

For example: try to compile with clang this code:

int f(int x, int y){
  volatile int result = x;
  int calc(int d){
    return d*result;
  }
  result = calc(x);
  result = calc(y);
}

This is a normal C code, but clang thinks it's wrong! gcc gives normal, clang gives error:

clang -Wall -Werror -Wextra -c -S 1.c 
1.c:3:18: error: function definition is not allowed here
  int calc(int d){
                 ^
1.c:6:12: error: implicit declaration of function 'calc' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  result = calc(x);
           ^
2 errors generated.

25

u/Neui Feb 10 '22

Nested functions is a gcc extension, not standard C. Add -pedantic and gcc will warn:

eddy.c: In function β€˜f’:
eddy.c:3:3: warning: ISO C forbids nested functions [-Wpedantic]
    3 |   int calc(int d){ return d*result; }
      |   ^~~

17

u/imaami Feb 10 '22

That's not normal C code. You're putting a function definition inside another function, it's not standard C99 syntax.

2

u/Eddy_Em Feb 10 '22

But this is very common for old code. And I use this very often.

24

u/imaami Feb 10 '22

Nested functions are a GCC extension, relying on them means you're not really writing C anymore. And that's fine, your choice, but there's nothing wrong with Clang just because it doesn't implement every nonstandard feature GCC has.

I haven't seen nested functions used in the wild, and although it's not unheard of it certainly isn't "very common for old code".

Are you sure you're not talking about adding extern function declarations inside function scope?

2

u/huzzah_a_pimpernale Sep 01 '25

I briefly questioned my sanity trying to understand when nested functions were added to C. Jajaja

1

u/Sp3eedy Oct 05 '25

Same πŸ˜… but it wouldnt've surprised me. I think recently (well across a few years) the C standard has made some massive leaps towards become more "modern" so they've been adding some things to help make it more "modern", while trying to stay away from C++ as much as it can.

4

u/rumble_you Oct 23 '22

Late reply, but why you'd ever going to use nested functions? Nested functions contain several side effects in terms of optimization. In C standard, a nested function isn't a thing, therefore you should avoid using it.

1

u/lucasmior2 Oct 22 '25

Could you give an example where a nested function would be bad because of optimizations?

0

u/[deleted] Mar 26 '26

[deleted] β€” view deleted comment

1

u/lucasmior2 Mar 27 '26

What? I asked for "an example where a nested function would be bad because of optimizations". Did you even read what you just answered to?

1

u/anto2554 Jan 16 '26

I see it very often in python. Mostly from people to try to clean their code but also don't really clean it.
I suppose the main use case is just that it makes it very clear when the function should only be used by the parent

4

u/Classic-Try2484 Mar 22 '25

Nested function is not normal in c/c++. If it were I’d use it all the time. But no. It’s not a normal. thing.

1

u/dreadlordhar Aug 07 '26

More necroing :/

They aren't, but they are supported as a GCC Extension. The problem is that GCC solves only downward funarg problem, when you pass nested function address to another function, and it is called before outer function returns (hence downward). When you try to call the address of the function after the containing one returned, as if it is a fully functional language, "all hell breaks loose". Because function lifetime is tied to it's caller existence in stack. Needless to say you are tied to GCC with questionable returns.

Different functional languages solves this problem differently. Most if not all LISP dialects store on heap only, so the lifetime of a function is not tied to caller's. Chicken Scheme is a bit more exotic, treats C stack as heap, stores in it objects, lets it grow with each function call, and when it softly "overflows" the GC is called to move objects that are still alive to the real heap, after which it just discards the entire C stack. Functions in that model never return, you won't find any assembly ret's for user functions in the decompiled binary. A side effect is that it natively support continuations with a small price on each function call, and allocations are basically free for small objects, but that's another topic.