1-1
Quest: page 8
Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get:
hello world program
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("hello world\n");
return EXIT_SUCCESS;
}
If i compile the program and run it. It will print me hello world.
gcc main.c && ./a.out
omit the include:
main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
3 | printf("hello world\n");
| ^~~~~~
main.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include <stdio.h>
1 |
main.c:3:5: warning: incompatible implicit declaration of built-in function ‘printf’ [-Wbuiltin-declaration-mismatch]
3 | printf("hello world\n");
| ^~~~~~
main.c:3:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
main.c:4:12: error: ‘EXIT_SUCCESS’ undeclared (first use in this function)
4 | return EXIT_SUCCESS;
| ^~~~~~~~~~~~
main.c:1:1: note: ‘EXIT_SUCCESS’ is defined in header ‘<stdlib.h>’; did you forget to ‘#include <stdlib.h>’?
+++ |+#include <stdlib.h>
1 |
main.c:4:12: note: each undeclared identifier is reported only once for each function it appears in
4 | return EXIT_SUCCESS;
| ^~~~~~~~~~~~
omit one semicolon:
main.c: In function ‘main’:
main.c:4:37: error: expected ‘;’ before ‘return’
4 | int main() { printf("hello world\n") return EXIT_SUCCESS; }
| ^~~~~~~
|
omit curly braces at the end:
main.c: In function ‘main’:
main.c:6:3: error: expected declaration or statement at end of input
6 | return EXIT_SUCCESS;
| ^~~~~~
note
A compiler will give good details on what might be wrong.