c - Why does mingw-gcc allow getch() to be used unconditionally? -


i started porting ton of c programs windows environment, previous linux development pc. noticed bit off mingw's windows gcc implementation.

in windows, found lovely function called getch. it's easy, it's immediate... , it's non-standard.

i'd focus of "non-standard" part of it. specifically, want know why mingw-gcc allows me use it, without using standard libraries.

assume have program prints "hello, world!", nl , cr, , waits key , return:

#include <stdio.h> int main(void) {     char str[14] = "hello, world!"; //13 characters , terminator     printf("%s\n\r", str);     scanf("%c");     return 0; } 

now, let's change bit of program use getch:

#include <stdio.h> int main(void) {     char str[14] = "hello, world!"; //again, 13 characters , terminator     printf("%s\n\r", str);     getch(); //see? uses getch.     return 0; } 

the interesting part is, isn't getch call made conio.h library old dos/win32 environments? compiler doesn't give warning. why work?

here's find bit more unsettling:

int main(void) //literally nothing included {     getch();     return 0; } 

what on earth? know fact getch not exist on linux environments (natively, anyways). so, compiler getting call from?

my best guess (please correct me if wrong) decision link whatever has getch made @ link time, not compile time.

in case, seems little odd me. why implementation of gcc automatically include clearly non-standard capability on windows?

the compilation step works (though should produce warning) because, traditionally, c allows call functions haven't been declared, provided return int getch() does.

the linking step works because c runtime library mingw uses single library, i.e., provides visual c runtime library functions, including non-standard ones. mingw presumably links default because (apart rare edge cases) needed, if want main() function nothing. :-)


it should mentioned library in question not officially support third-party use, except applications built in visual studio 6. more modern runtimes have deprecated getch in favour of equivalent standards compliant _getch, vs6 predates change.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -