cancel
Showing results for 
Search instead for 
Did you mean: 

Nested function and comparison like (0 < myVar < 100) don't give compile errors

Hi,

In CubeIDE it does not generate any compile error,

if function is defined inside function 

int function_out(int i)
{
  int function_in(int y)
  {
  }
}

and comparisons is written in format (0 < myVar < 100)

if(0 < myVar < 100)
{
}

As I know these are not allowed in C.


Edited to apply source code formatting - please see How to insert source code for future reference.

2 REPLIES 2
KnarfB
Super User

Nested functions are gnu bs accepted by gcc, not ISO C.

Probably inherited from pascal type languages.

The chained comparison is syntactically legal, but gives misleading results.

You may try options like -std=c11 -pedantic -Wall -Wextra -Werror for taming the gnu.

Or switch to clang (STM32Cube further facilitates code development wit... - STMicroelectronics Community)

C:\tmp>clang gnu.c
gnu.c:9:1: error: function definition is not allowed here
    9 | {
      | ^
gnu.c:13:14: warning: result of comparison of constant 100 with boolean expression
      is always true [-Wtautological-constant-out-of-range-compare]
   13 | if(0 < myVar < 100)
      |    ~~~~~~~~~ ^ ~~~
1 warning and 1 error generated.

hth

KnarfB

 

Andrew Neil
Super User

@ThusithaSamarasekara wrote:

As I know these are not allowed in C.


Not in Standard C but, as @KnarfB said, GCC allows nested functions as an extension:

https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.