There are several problems in your code that could in principle cause some kind of run-time error, but there's nothing that I'd expect to cause such an error.
#include <stdio.h>
int array[10];
void main() {
As others have mentioned, this is not one of the standard ways to define the main
function. For your purposes, since you're not using command-line arguments, the correct definition would be:
int main(void) {
The C standard (1990, 1999, and 2011 editions) does permit a a hosted implementation to permit other forms for main
; for example, Microsoft's compiler specifically permits void main(void)
(and void main()
would probably be acceptable as well). But there is no good reason to define main
with a return type of void
, since int main(void)
is guaranteed to be correct.
If you define main
using something other than one of the two standard forms (the other is int main(int argc, char *argv[])
or in a manner that your implementation documents, then your program's behavior is undefined.
On the other hand, the worst consequence I've seen for using void main()
is a compile-time warning -- and the standard doesn't even require that. A compiler that accepts void main()
without complaint and then generates code that causes a run-time error has, I would say, a serious bug, though it wouldn't actually violate the C standard. I'd be surprised if this were the cause of the error you're seeing.
int i;
for(i = 0; i < 10; i++){
array[i] = i;
printf("%i", array[i]);
This is perfectly ok; it should print 0123456789
. Printing multiple int
values with nothing separating them is a bit odd, but it's not an error as such. ("%i"
is equivalent to "%d"
, and "%d"
is more common.)
But your output is not terminated by a newline. On many systems, the output will just be printed without an end-of-line marker, which might mess up your terminal display. On others, the trailing newline might be required, and failing to provide one causes undefined behavior. (You don't need a newline on all your output, just at the very end.)
}
I'd add something like
putchar('\n');
here, just to be sure.
return;
A return;
with no expression is perfectly valid in a void
function -- though it's unnecessary at the very end of the function, since reaching the closing }
does an implicit return
anyway. But if you define main
correctly as int main(void)
, then this should be return 0;
. Starting in C99, reaching the closing }
of main
is equivalent to executing a return 0;
, so it's not strictly necessary -- but there are still compilers that don't fully support C99 (much less C11), so the adding the return 0;
isn't a bad idea.
}
Here's a version of your program that corrects these issues, and should be 100% portable to all conforming hosted C implementations:
#include <stdio.h>
int array[10];
int main(void) {
int i;
for(i = 0; i < 10; i++){
array[i] = i;
printf("%d", array[i]);
}
putchar('\n');
return 0;
}
The expected output is:
0123456789
Try this and see if it corrects the run-time error. If it does, try starting with your original program and changing just one thing at a time (the definition of main
along with the return
statement, and the trailing newline) to see exactly what it is that corrects the problem.
It would be very helpful to know what compiler and operating system you're using.
(I've mentioned "hosted implementations". The other kind of C implementation is "freestanding"; it refers to implementations for embedded systems, where code may run directly on the hardware with no operating system. On such a system, most of the standard library is optional, and the correct definition of the program entry point is entirely implementation-defined; it might not even be called main
. You're probably not using such an implementation.)