Hi ,

I read that c is not a block structured language.
means it does not allow defining functions inside other functions.

but this code is not generating any error not even warnings, though we nest the functions.

whats the story.

#include<stdio.h>
int main()      {
        int num;
        num = 10;
        int fun(int n)  {
                printf("The local function");
                return 0;
        }
        fun(num);
        printf("Main function");
        return 0;
        }

>whats the story.
The story is that the code you posted is not valid C. You're likely relying on a compiler extension that allows nested function definitions. What compiler are you using?

Hi ,

I read that c is not a block structured language.
means it does not allow defining functions inside other functions.

but this code is not generating any error not even warnings, though we nest the functions.

whats the story.

#include<stdio.h>
int main()      {
        int num;
        num = 10;
        int fun(int n)  {
                printf("The local function");
                return 0;
        }
        fun(num);
        printf("Main function");
        return 0;
        }

I tried compiling this with the GCC compiler and its works...so I tried compiling with the -S switch(Stop after the stage of compilation proper; do not assemble) and found it create a function fun.2047(huh name mangling in C)..Any ways this doesn't answer your questions as to why this is allowed but I'm interested enough to see what someone else comes up with...

>I tried compiling this with the GCC compiler and its works...
Try again with the -ansi, -pedantic, and -Wall switches.

>I tried compiling this with the GCC compiler and its works...
Try again with the -ansi, -pedantic, and -Wall switches.

I did use -ansi -Wall

When I tried -ansi -Wall -pedantic it generated a warning but still compiled and ran without incident..

Edited 7 Years Ago by gerard4143: n/a

>but still compiled and ran without incident..
Yet another ding against gcc for not conforming to the standard, I suppose. Those switches put the compiler in strict conformance mode, which means a constraint violation like this would be rejected outright.

>but still compiled and ran without incident..
Yet another ding against gcc for not conforming to the standard

I'm sure GCC will survive...dings and all

I compiled the program on both Dev C++ and GCC compiler.
it worked without any errors.

but how the compilers are implemented with out following the standards.( ignoring c is not a block structured language).

>but still compiled and ran without incident..
Yet another ding against gcc for not conforming to the standard, I suppose. Those switches put the compiler in strict conformance mode, which means a constraint violation like this would be rejected outright.

-ansi switch, all that it does is to support the C89 features and disable the GNU extensions that conflict with C89, but it doesn't mean that it will make gcc compiler to behave as a strict ANSI compiler. Nor, will gcc produce all the errors required by the standard, just because of it.
To obtain all the diagnosis errors required by the standard the -pedantic switch must be included. Nevertheless, it doesn't stop the code from compiling, but rather displays the errors as warnings. To act upon those warnings and make them errors that would stop from compiling the switch -pedantic-errors should be included.
The -Wall switch for a rather extra set of popular warnings to the minuscule detail

Nothing to ding the said compiler, rather an unsupported expectation of the behavior of the switches.

Edited 7 Years Ago by Aia: n/a

This article has been dead for over six months. Start a new discussion instead.