Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there C/C++ compilers that do not require standard library includes?

All applicants to our company must pass a simple quiz using C as part of early screening process.

It consists of a C source file that must be modified to provide the desired functionality. We clearly state that we will attempt to compile the file as-is, with no changes.

Almost all applicants user "strlen" but half of them do not include "string.h", so it does not compile until I include it.

Are they just lazy or are there compilers that do not require you to include standard library files, such as "string.h"?

like image 967
Kyle Heironimus Avatar asked Nov 29 '22 11:11

Kyle Heironimus


2 Answers

GCC will happily compile the following code as is:

main()
{
   printf("%u\n",strlen("Hello world"));
}

It will complain about incompatible implicit declaration of built-in function ‘printf’ and strlen(), but it will still produce an executable.

If you compile with -Werror it won't compile.

like image 66
Wernsey Avatar answered Dec 06 '22 19:12

Wernsey


I'm pretty sure it's non-conformant for a compiler to include headers that aren't asked for. The reason for this is that the C standard says that various names are reserved, if the relevant header is included. I think this implies they aren't reserved if they aren't included, since compilers aren't allowed to reserve names the standard doesn't say are reserved (unless of course they include a non-standard header which happens to be provided by the compiler, and is documented elsewhere to reserve extra names. Which is what happens when you use POSIX).

This doesn't fully answer your question - there do exist non-conformant compilers. As for your applicants, maybe they're just used to including "windows.h", and so have never thought before about what header strlen might be defined in by the C standard. I assume without testing that MSVC does in principle require you to include "string.h". But since "windows.h" does that for you, for the vast majority of practical Windows programs you don't need to know that you have to include "string.h".

like image 37
Steve Jessop Avatar answered Dec 06 '22 19:12

Steve Jessop