C Compiler errors
Malcolm Hussain-Gambles (1596) 811 posts |
I’ve got to know and love the random C compiler errors. Error missing newline before EOF – inserted I always wondered why you need a newline at the end of a C source file, does anyone know why – or is it just there purely for my amusement? |
Rick Murray (539) 13850 posts |
Surely if the compiler notices that there was no newline and inserts one, this should become a warning and not an error? Technically every line of input must end with a newline – it is part of the C spec – perhaps because some systems have difficulty reading the last line of data from files (treated as text not binary) if the newline isn’t present. |
Rick Murray (539) 13850 posts |
Update: It is part of the standard, here: http://c0x.coding-guidelines.com/5.1.1.2.html#123 The reasoning is as follows: File “one.h” is this, it does not end with a newline: // this file is now unused File “two.h” is this, it ends with a newline: #include <stdio.h> File “meh.c” is this (ending with a newline): #include "one.h" #include "two.h" int main(void) { printf("Meh!\n"); return 0; } It would probably fail to build, complaining about printf not being recognised. How is this possible? Simple – if the compiler did not raise an error for missing newlines, after preprocessing, the compiler would see this: // this file is now unused#include <stdio.h> int main(void) { printf("Meh!\n"); return 0; } Do you see what happened there? |
Steve Pampling (1551) 8172 posts |
Shame that objasm doesn’t auto insert the newline when it throws up an error in similar circumstances |
Malcolm Hussain-Gambles (1596) 811 posts |
Thanks Rick, that explains it! “ERROR: Detestable language detected” ;-) |