ANSWERS TO "HELLO WORLD" EXERCISES
1. It depends on what the definition of "is" is. The ANSI C99 standard
requires that we say "int main()" or "int main(void)". The ANSI
C89 standard allowed "main()" so you will see this in many textbooks.
However "void main()" is incorrect in both standards.
It is better style to end main() with "return 0;" although it is
not technically necessary.
2. #include <stdio.h>
int main(void) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", x + y);
return 0;
}
3. Brute-force solution:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
printf("Hello World\n");
printf("Hello World\n");
printf("Hello World\n");
printf("Hello World\n");
return 0;
}
Better solution:
#include <stdio.h>
int main(void) {
int i;
for (i = 0; i < 5; i++)
printf("Hello World\n");
return 0;
}
4. #include <stdio.h>
int main(void) {
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++)
printf("Hello World\n");
return 0;
}