2) Is there any special reason of- Why most C compilers include most of the header files implicitly, but in C++ we have to include them explicitly?
I don't think that is the case. You have to include header files in C also. Many (most) system headers include other headers.3) Can a global variable in C be declared anywhere in the program(outside all functions)?
ex-
int i;
main()
{
/*body*/
}
int j;
test()
{
/*body*/
}
int k;
Is this code valid according to C standards?
yes, but remember that the variable has to have been declared before it can be used. That means function main() cannot use variable k.4) Is the value of NULL=0 according to C/C++ standard?
c++ yes, C no. In C it can be (void *)0, and I've even seen several other variations on that depending on the compiler.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
1)
Just work your self out from the middle, if they are given in k&r they describe them in k&r ?
char(* (*f())[]) () *f() => f is function pointer taking zero arguments
returning (* ... [])() => returning array of pointer to functions
returning char ... => returning char...
(Ok, this was a pain in the ass, but I doubt youll ever see anything like that in real life)
2)
C does not include default headers, instead C has implicit function declarations...
if the compiler sees a function call it has not seen before it assumes it is declared as a function int function_name( function_args ) (ie it returns an int) leaving the linker to fail if there really is no such function. I think you can get a warning for this with -Wall or some other flag but I'm not sure. (Or compile it with a c++ compiler).
perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
>2) Is there any special reason of- Why most C compilers include
>most of the header files implicitly, but in C++ we have to include them explicitly?
A correct and portable program must include the headers it uses explicitly or otherwise provide a compatible declaration for everything it uses from that header. However, you may have just gotten lucky because traditionally, if a declaration isn't specified in C, a default declaration is used. That default declaration is often compatible enough to avoid irritating the compiler and then the linker gives you the correct function. A lot of people consider this to be a misfeature (it was removed in the latest standard) and if you rely on it, you're treading on very thin ice. This is one of those areas where if you choose to abuse it, you'd better know damn well how it works in all cases. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401